Programing

콘솔에 인쇄 된 문자를 지우는 방법

crosscheck 2020. 11. 4. 07:41
반응형

콘솔에 인쇄 된 문자를 지우는 방법


다른 언어로하는 방법을 찾고 있는데 마지막 문자를 제거하려면 특수 문자 \ b를 사용해야한다는 것을 발견했습니다. ( How-do-i-erase-printed-characters-in-a-console-applicationlinux )

console.log ()를 여러 번 호출 할 때 node.js에서는 작동하지 않습니다.

단일 로그를 작성하는 경우 :

console.log ("abc\bd");

나는 결과를 얻는다 : abd

하지만 내가 쓰면 :

console.log ("abc");
console.log ("\bd");

결과를 얻습니다.

abc
d

내 목표는 다음과 같은 대기 메시지를 인쇄하는 것입니다.

기다리고
기다리고 있습니다.
기다리는 중 ..
기다리는 중 ...

다시 한번:

기다리고
기다리고 있습니다.
기타

모두 같은 줄에 있습니다.


다음에 사용할 수있는 기능이 있습니다 process.stdout.

var i = 0;  // dots counter
setInterval(function() {
  process.stdout.clearLine();  // clear current text
  process.stdout.cursorTo(0);  // move cursor to beginning of line
  i = (i + 1) % 4;
  var dots = new Array(i + 1).join(".");
  process.stdout.write("Waiting" + dots);  // write text
}, 300);

2015 년 12 월 13 일 업데이트 : 위 코드는 작동하지만 더 이상 process.stdin. 이동했습니다readline


이제 readline라이브러리와 API사용 하여이 작업을 수행 할 수 있습니다 .


같은 줄을 덮어 쓰는 가장 쉬운 방법은

var dots = ...
process.stdout.write('Progress: '+dots+'\r');

이것이 \r핵심입니다. 커서를 줄의 시작 부분으로 다시 이동합니다.


이것은 나를 위해 작동합니다.

process.stdout.write('\033c');
process.stdout.write('Your text here');

문자열의 시작 부분에서 \ r을 움직여보십시오. 이것은 Windows에서 작동했습니다.

for (var i = 0; i < 10000; i+=1) {
    setTimeout(function() {
        console.log(`\r ${i}`);
    }, i);
}

참고 URL : https://stackoverflow.com/questions/11600890/how-to-erase-characters-printed-in-console

반응형