2016-12-24 36 views
9

我遇到以下问题,我在终端窗口中绘制了一个ASCII字符,并将光标移动到另一个位置并使用以下代码重复该过程。NodeJS中的Readline正在绘制不需要的行

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) 
} 

一切顺利,直到我达到一个点,就会有显示,我没有画作为图像波纹管在屏幕上全线展示

enter image description here

如果我保持应用程序最终整个屏幕将填满我画的线条。

问题

我不相信我画的线,如果这是真的什么是与终端窗口发生了什么?

科技二段

  • MACOS
  • 码头及的iTerm有同样的问题
  • 的NodeJS v6.40

回答

1

在看源代码一行readline,我相信他们an old hack加入纠正一些标签行为仍然导致this current line的问题。每当光标位置cols在0处被检测到(可能是源代码中的另一个bug)时,它会发出一个refreshLine--它将引发一个提示。 docs表示“rl.write()方法会将数据写入readline Interface的输入,就好像它是由用户提供的一样。”,因此提示会将所有输入都输出回给您。

我找不到源代码中的解决方法,但可以修改接口的源代码。在您的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); 
    } 
}; 
+0

太棒了,您的解释是关于点和代码的工作!非常感谢:) –

+0

出于好奇,你为什么不用一个拉请求来解决这个问题,并用你在这里写的内容替换'_insertString'? –

+1

@DavidGatti是一个好主意 - 我正在研究它 - 我只需要为选项卡修复案例和案例都值得。 –