2016-12-24 22 views
9

Aşağıdaki pencerede bir ASCII karakteri çiziyorum ve imleci başka bir konuma taşıyorum ve işlemi aşağıdaki kodla yineleyin.NodeJS'deki Readline, istenmeyen çizgiler çiziyor

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

Ben resim feryat olarak çizmedim ekranda gösteren tam bir çizgi olacak bir noktaya ulaşana kadar her şey iyi gider

enter image description here

Eğer gösterir Uygulamayı sürekli olarak devam ettiririm, tüm ekran, çizdiğim şeyi kapsayan çizgilerle dolduracaktır.

Sorular

bu terminal penceresi ile neler olduğu doğruysa, ben bu satırları çiziyorum inanmıyorum?

Tech Sec

  • MacOS
  • Terminali ve iTerm taleb için kaynak kodu baktığımızda aynı konuyu
  • NodeJS v6.40

cevap

1

var, onlar eklenen an old hack inanıyoruz Bazı sekme davranışlarını düzeltmek hala this current line için sorunlara neden oluyor. İmleç pozisyonu kodları 0'da (muhtemelen kaynakta başka bir hata) algılandığında, bir bilgi satırı atar. docs, "rl.write() yönteminin, verileri, kullanıcı tarafından sağlanmış olduğu gibi, Interface arabiriminin girişine yazacağını" söyler, böylece komut istemi, tüm girişinizi size geri gönderir.

Kaynakta bir geçici çözüm bulamadım, ancak Arabirimin kaynak kodunu değiştirebilirsiniz. Sorunu gidermek için bu snippet'i const readline = require('readline');'dan sonra ekleyin.

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

Fantastik, açıklamanız yeterli ve kod işe yaradı! Çok teşekkür ederim :) –

+0

Merak etme, neden bu soruyu bir çekme isteğiyle düzeltir ve '_insertString' yerine yeni yazdığınız ile değiştirmiyorsunuz? –

+1

@DavidGatti evet iyi bir fikir - Ben bunun üzerinde çalışıyorum - Ben sadece hem sekme düzeltme durumu hem de sizin durumunuza değer bulmam gerekiyor. –