mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: change to use Zsh
* show message from server response * send output and error to server
This commit is contained in:
1 parent
0153eae673
commit
6204bd0b17
10 files changed
+96
-104
No files matched your search
+42
-14
@@ -1,5 +1,10 @@
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
const fs = require('fs').promises
|
||||
const { exit } = require('process')
|
||||
const readline = require('readline')
|
||||
|
||||
const RETURN_SYMBOL = '\u21B5'
|
||||
|
||||
process.stdin.setRawMode(true)
|
||||
|
||||
const rl = readline.createInterface({
|
||||
@@ -8,13 +13,6 @@ const rl = readline.createInterface({
|
||||
terminal: false,
|
||||
})
|
||||
|
||||
async function printResponses(responses, delay = 100) {
|
||||
for (const response of responses) {
|
||||
await printResponse(response, delay)
|
||||
}
|
||||
rl.close()
|
||||
}
|
||||
|
||||
function waitForEnter() {
|
||||
return new Promise((resolve) => {
|
||||
rl.once('line', resolve)
|
||||
@@ -35,18 +33,48 @@ function printResponse(response, delay) {
|
||||
|
||||
waitForEnter().then(() => {
|
||||
clearInterval(timer)
|
||||
process.stdout.write(response.slice(i) + '\n')
|
||||
process.stdout.write(response.slice(i))
|
||||
process.stdout.write('\n')
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function request() {
|
||||
const { API_ENDPOINT, TOKEN, PWD, COMMAND, OUTPUT, EXIT_CODE } = process.env
|
||||
async function printResponses(responses, delay = 100) {
|
||||
for (const response of responses) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await printResponse(response + RETURN_SYMBOL, delay)
|
||||
}
|
||||
rl.close()
|
||||
}
|
||||
|
||||
async function readOrNone(file) {
|
||||
try {
|
||||
return await fs.readFile(file, 'utf8')
|
||||
} catch (err) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const {
|
||||
API_ENDPOINT,
|
||||
TOKEN,
|
||||
PWD,
|
||||
CMD_NAME,
|
||||
CMD_OUTPUT_FILE,
|
||||
CMD_ERROR_FILE,
|
||||
CMD_EXIT_CODE,
|
||||
} = process.env
|
||||
if (!CMD_NAME) exit(0)
|
||||
|
||||
const output = await readOrNone(CMD_OUTPUT_FILE)
|
||||
const error = await readOrNone(CMD_ERROR_FILE)
|
||||
const payload = {
|
||||
command: COMMAND,
|
||||
output: OUTPUT,
|
||||
exit_code: EXIT_CODE,
|
||||
command: CMD_NAME,
|
||||
output,
|
||||
error,
|
||||
exit_code: CMD_EXIT_CODE,
|
||||
token: TOKEN,
|
||||
pwd: PWD,
|
||||
}
|
||||
@@ -67,4 +95,4 @@ async function request() {
|
||||
}
|
||||
}
|
||||
|
||||
request()
|
||||
main()
|
||||
@@ -1,50 +0,0 @@
|
||||
import { stdin, stdout, exit } from 'process'
|
||||
import os from 'os'
|
||||
import pty from 'node-pty'
|
||||
|
||||
stdin.setRawMode(true)
|
||||
stdin.resume()
|
||||
stdin.setEncoding('utf8')
|
||||
|
||||
// Create a new pseudoterminal
|
||||
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'
|
||||
const ptyProcess = pty.spawn(shell, [], {
|
||||
name: 'xterm-color',
|
||||
cols: 80,
|
||||
rows: 30,
|
||||
cwd: process.env.HOME,
|
||||
env: process.env,
|
||||
})
|
||||
|
||||
// Listen for output data
|
||||
ptyProcess.on('data', function (data) {
|
||||
// Intercept and process the output here
|
||||
stdout.write(data)
|
||||
})
|
||||
|
||||
// Write a command to the pseudoterminal
|
||||
ptyProcess.write('ls\r')
|
||||
|
||||
let commandBuffer = ''
|
||||
|
||||
stdin.on('data', (key) => {
|
||||
if (key === '\u0004') {
|
||||
exit()
|
||||
} else if (key === '\r') {
|
||||
// User pressed enter, log and execute the command
|
||||
console.log('User command: ', commandBuffer)
|
||||
commandBuffer = ''
|
||||
} else if (key === '\u007F') {
|
||||
// User pressed backspace, remove the last character from the buffer
|
||||
commandBuffer = commandBuffer.slice(0, -1)
|
||||
} else {
|
||||
// Add the typed character to the command buffer
|
||||
commandBuffer += key
|
||||
}
|
||||
ptyProcess.write(key)
|
||||
})
|
||||
|
||||
// Listen for exit event
|
||||
ptyProcess.on('exit', function (exitCode) {
|
||||
console.log('Process exited with code: ', exitCode)
|
||||
})
|
||||
@@ -3,8 +3,5 @@
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"node-pty": "^1.0.0"
|
||||
}
|
||||
"dependencies": {}
|
||||
}
|
||||
Reference in new issue
Block a user