[英]Readline in NodeJS is drawing unwanted lines
我有以下問題,我在終端窗口中繪制ASCII字符,並將光標移動到另一個位置,並使用以下代碼重復該過程。
const readline = require('readline');
//
// Set the direction of the cursor
//
let dirrection_y = true;
let dirrection_x = true;
//
// Set the initial position of the cursor
//
let position_x = 0;
let position_y = 0;
//
// Get the terminal window size
//
let window_x = process.stdout.columns;
let window_y = process.stdout.rows;
//
// Set the cursor to the top left corner of the terminal window so we can clear
// the terminal screen
//
readline.cursorTo(process.stdout, position_x, position_y)
//
// Clear everything on the screen so we have a clean template to draw on.
//
readline.clearScreenDown(process.stdout)
//
// Create the interface so we can use it to for example write on the console.
//
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
//
// React to CTR+C so we can close the app, and potentially do something before
// closing the app.
//
rl.on('close', function() {
process.exit(0);
});
//
// Start the main loop
//
draw();
//
// The main loop that moves the cursor around the screen.
//
function draw()
{
setTimeout(function() {
//
// 1. Move the cursor up or down
//
dirrection_y ? position_y++ : position_y--
//
// 2. When we reach the bottom of the terminal window, we switch
// direction from down, to up.
//
if(position_y == window_y)
{
//
// 1. Switch the direction to go up
//
dirrection_y = false
//
// 2. Move the next column or previous one depending on the
// direction.
//
dirrection_x ? position_x++ : position_x--
}
//
// 3. When we reach the top of the terminal screen, switch direction
// again
//
if(position_y < 0)
{
//
// 1. Switch the direction to go down
//
dirrection_y = true
//
// 2. Move the next column or previous one depending on the
// direction.
//
dirrection_x ? position_x++ : position_x--
}
//
// 4. When we reach the far right of the terminal screen we switch
// direction from 'to right', to 'to left'
//
if(position_x == window_x) { dirrection_x = false }
//
// 5. When we reach the far left (beginning) of the terminal window
// we switch direction again.
//
if(position_x == 0) { dirrection_x = true }
//
// 6. Write on char on the terminal screen.
//
rl.write('█');
//
// 7. Move the cursor to the next position
//
readline.cursorTo(process.stdout, position_x, position_y)
//
// 8. Restart the loop.
//
draw();
}, 100)
}
一切順利,直到我達到一個點,屏幕上會顯示一條完整的線條,我沒有畫出來,因為下面的圖像顯示
如果我最終保持應用程序運行,整個屏幕將填滿覆蓋我正在繪制的內容的線條。
我不相信我正在繪制那些線,如果這是真的終端窗口發生了什么?
在查看readline的源代碼時,我相信他們為糾正某些選項卡行為而添加的舊hack仍然會導致當前行的問題。 每當光標位置cols被檢測到0(可能是源中的另一個bug)時,它會發出一個refreshLine ---它會拋出一個提示。 文檔說“rl.write()方法會將數據寫入readline接口的輸入,就好像它是由用戶提供的那樣。”,因此提示會將所有輸入輸出給您。
我找不到源內解決方法,但您可以修改Interface的源代碼。 在你的const readline = require('readline');
之后添加這個片段const readline = require('readline');
解決問題。
readline.Interface.prototype._insertString = function(c) {
if (this.cursor < this.line.length) {
var beg = this.line.slice(0, this.cursor);
var end = this.line.slice(this.cursor, this.line.length);
this.line = beg + c + end;
this.cursor += c.length;
this._refreshLine();
} else {
this.line += c;
this.cursor += c.length;
this.output.write(c);
this._moveCursor(0);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.