Connect to terminal in raw mode

Able to use VIM
This commit is contained in:
wancat committed 2023-07-15 15:04:58 +08:00
1 parent 69ef435b77
commit e3d66709e4
2 files changed
+18 -11

No files matched your search

+16 -9
View File
@@ -1,11 +1,13 @@
import readline from 'readline' import { stdin, stdout, exit } from 'process'
import { exit } from 'process'
import { io } from 'socket.io-client' import { io } from 'socket.io-client'
import { Writable } from 'stream'
import { pipeline } from 'stream/promises'
const rl = readline.createInterface({ console.log(stdin)
input: process.stdin, console.log(process.stdin.isTTY)
output: process.stdout, stdin.setRawMode(true)
}) stdin.resume()
stdin.setEncoding('utf8')
const socket = io('ws://localhost:8080') const socket = io('ws://localhost:8080')
@@ -14,7 +16,7 @@ socket.on('open', function open() {
}) })
socket.on('message', function incoming(data) { socket.on('message', function incoming(data) {
console.log(`${data}`) stdout.write(data)
}) })
socket.on('close', function close() { socket.on('close', function close() {
@@ -22,6 +24,11 @@ socket.on('close', function close() {
exit() exit()
}) })
rl.on('line', (input) => { stdin.on('data', (key) => {
socket.send(input) if (key === '\u0003') {
socket.close()
exit()
} else {
socket.send(key)
}
}) })
+2 -2
View File
@@ -13,12 +13,12 @@ io.on('connection', async (socket) => {
const stream = await attachContainer(container) const stream = await attachContainer(container)
stream.on('data', (chunk) => { stream.on('data', (chunk) => {
socket.send(chunk.toString()) socket.send(chunk)
}) })
socket.on('message', function incoming(message) { socket.on('message', function incoming(message) {
console.log(`From client: ${message}`) console.log(`From client: ${message}`)
stream.write(message + '\n') stream.write(message)
}) })
socket.on('close', () => { socket.on('close', () => {