狐狸.fox
2011-05-27 20:42
采纳率: 0%
浏览 1.3k

打印到控制台而不拖尾换行?

Is there a method for printing to the console without a trailing newline? The console object documentation doesn't say anything regarding that:

console.log()

Prints to stdout with newline. This function can take multiple arguments in a printf()-like way. Example:

console.log('count: %d', count);

If formating elements are not found in the first string then util.inspect is used on each argument.

转载于:https://stackoverflow.com/questions/6157497/node-js-printing-to-console-without-a-trailing-newline

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • 已采纳

    You can use process.stdout.write():

    process.stdout.write("hello: ");
    

    See the docs for details.

    点赞 评论
  • elliott.david 2012-03-09 04:26

    Also, if you want to overwrite messages in the same line, for instance in a countdown, you could add '\r' at the end of the string.

    process.stdout.write("Downloading " + data.length + " bytes\r");
    
    点赞 评论
  • derek5. 2012-11-15 13:02

    util.print can be used also. Read: http://nodejs.org/api/util.html#util_util_print

    util.print([...])# A synchronous output function. Will block the process, cast each argument to a string then output to stdout. Does not place newlines after each argument.

    An example:

    // get total length
    var len = parseInt(response.headers['content-length'], 10);
    var cur = 0;
    
    // handle the response
    response.on('data', function(chunk) {
      cur += chunk.length;
      util.print("Downloading " + (100.0 * cur / len).toFixed(2) + "% " + cur + " bytes\r");
    });
    
    点赞 评论
  • ℡Wang Yan 2014-01-28 16:52

    In Windows console (Linux, too), you should replace '\r' with its equivalent code \033[0G:

    process.stdout.write('ok\033[0G');
    

    This uses a VT220 terminal escape sequence to send the cursor to the first column.

    点赞 评论
  • perhaps? 2014-04-04 18:44

    None of these solutions work for me. process.stdout.write('ok\033[0G') and just using '\r' just create a new line, do not overwrite, Mac OSX 10.9.2

    EDIT: I had to use this to replace the current line

    process.stdout.write('\033[0G'); process.stdout.write('newstuff');

    点赞 评论
  • hurriedly% 2014-09-30 08:48

    I got an error when using strict mode.

    Node error: "Octal literals are not allowed in strict mode."

    I found the answer here: https://github.com/SBoudrias/Inquirer.js/issues/111

    process.stdout.write("received: " + bytesReceived + "\x1B[0G");

    点赞 评论
  • 乱世@小熊 2015-08-18 01:40

    There seem to be many answers suggesting process.stdout.write. Error logs should be emitted on process.stderr instead (Use console.error). For anyone who is wonder why process.stdout.write('\033[0G'); wasn't doing anything it's because stdout is buffered and you need to wait for drain event (See Stdout flush for NodeJS?). If write returns false it will fire a drain event.

    点赞 评论
  • larry*wei 2017-12-27 00:33

    As an expansion/enhancement to the brilliant addition made by @rodowi above regarding being able to overwrite a row:

    process.stdout.write("Downloading " + data.length + " bytes\r");
    

    Should you not want the terminal cursor to be located at the first character, as I saw in my code, the consider doing the following:

    let dots = ''
    process.stdout.write(`Loading `)
    
    let tmrID = setInterval(() => {
      dots += '.'
      process.stdout.write(`\rLoading ${dots}`)
    }, 1000)
    
    setTimeout(() => {
      clearInterval(tmrID)
      console.log(`\rLoaded in [3500 ms]`)
    }, 3500)
    

    By placing the \r in front of the next print statement the cursor is reset just before the replacing string overwrites the previous.

    点赞 评论

相关推荐 更多相似问题