gpt4 book ai didi

node.js - 从终端读取 ANSI 转义

转载 作者:搜寻专家 更新时间:2023-10-31 23:11:51 34 4
gpt4 key购买 nike

Wikipedia article在终端上 ANSI 转义代码显示了一些可以发送到终端的代码 AND 然后一些数据返回给应用程序。请举例说明如何发送代码,然后在 Node.js 应用程序中读取结果。

例如这个转义序列:

CSI 6n | DSR – Device Status Report

Reports the cursor position (CPR) to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column.)

我花了几个小时尝试使用 process.stdoutprocess.stdin、各种 fs.* 函数,甚至尝试从 /dev/tty 读取。一切都是徒劳,完全迷路了。

最佳答案

这是一种方法:

var util = require("util");

function dsr(callback) {
process.stdin.setRawMode(true);
process.stdin.once("data", function(data) {
process.stdin.setRawMode(false);
process.stdin.pause();
callback(data.toString());
});
process.stdout.write("\x1b[6n");
}

dsr(function(data) {
console.log(util.inspect(data));
});

输出:

'\u001b[30;1R'

我正在让 stdin 进入原始模式,这样结果就不会打印在终端上,用户无需按回车键就可以读取结果。

关于node.js - 从终端读取 ANSI 转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36929209/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com