ghostty-web: Terminal Emulator บนเว็บด้วย Ghostty + API แบบ xterm.js
นำ Ghostty (terminal emulator ตัวเดียวกับ native app) มาใช้บนเว็บผ่าน WASM พร้อม API ที่เข้ากันได้กับ xterm.js ทำให้ย้ายโค้ดได้ง่ายและได้ VT100 implementation ที่แม่นยำกว่า รองรับ complex scripts และ escape sequences ที่ xterm.js ยังไม่มี
คัดมาจาก GitHub · อัปเดต 6 พ.ย. 2569
ดูโค้ดต้นทางส่วนประกอบ
wasm · ghostty · xterm · web-terminal · terminal
ghostty-web คืออะไร
ghostty-web เป็น terminal emulator สำหรับเว็บที่คอมไพล์จาก Ghostty (native terminal app) เป็น WASM โดยออกแบบ API ให้เข้ากันได้กับ xterm.js ทำให้ย้ายจาก xterm ได้ง่ายแค่เปลี่ยน import
ทำไมถึงมีประโยชน์
เปรียบเทียบ ghostty-web กับ xterm.js
ghostty-web
- รองรับ complex scripts (Devanagari, Arabic) อย่างถูกต้อง
- รองรับ XTPUSHSGR/XTPOPSGR
- ใช้ parser เดียวกับ native Ghostty (battle-tested)
- ไม่มี runtime dependencies (~400KB WASM)
- ขนาดไฟล์ใหญ่กว่า (WASM bundle)
- ต้อง init() ก่อนใช้งาน
xterm.js
- ใช้กันแพร่หลาย (VS Code, Hyper)
- เป็น JavaScript ทำงานได้ทันที
- Community และ ecosystem ใหญ่
- มีปัญหากับ complex scripts
- ไม่รองรับ XTPUSHSGR/XTPOPSGR
- เป็น JavaScript reimplementation ของ terminal emulation
เหมาะสำหรับ web terminal ที่ต้องการความแม่นยำสูงและรองรับ Unicode ซับซ้อน
เริ่มใช้ยังไง
ติดตั้ง:
npm install ghostty-web
ใช้งานแบบ drop-in replacement:
import { init, Terminal } from 'ghostty-web';
await init();
const term = new Terminal({
fontSize: 14,
theme: {
background: '#1a1b26',
foreground: '#a9b1d6',
},
});
term.open(document.getElementById('terminal'));
term.onData((data) => websocket.send(data));
websocket.onmessage = (e) => term.write(e.data);
ทดลองใช้ demo:
npx @ghostty-web/demo@next
เปิดเว็บที่ http://127.0.0.1:8080 จะได้ terminal จริงที่รันบน local (ใช้งานได้ดีบน Linux/macOS)
⚠️ คำเตือนด้านความปลอดภัย: Demo เปิด shell จริงบนเครื่อง protected ด้วย same-origin token ต่อรอบการรัน อย่า expose ออก internet ถ้าไม่เข้าใจความเสี่ยง หากต้องการ bind ที่อื่น ให้ตั้ง
HOST=<host>และGHOSTTY_ALLOWED_HOSTS=host1,host2
การใช้งานขั้นสูง
API เข้ากันได้กับ xterm.js ส่วนใหญ่ ดูตัวอย่างเต็มได้ใน demo source โดย ghostty-web ถูกสร้างขึ้นมาสำหรับ Mux (desktop app สำหรับ isolated, parallel agentic development) แต่ออกแบบให้ใช้งานได้กับทุก project

