Rewinding std::cout to go back to the beginning of a line

前端 未结 4 1057
迷失自我
迷失自我 2020-11-29 13:05

I\'m writing a command-line tool for Mac OS X that processes a bunch of files. I would like to show the user the current file being processed, but do not want a bazillion fi

相关标签:
4条回答
  • 2020-11-29 13:27

    std::cout interpretes "\r" as return to the beguining of the line, if you dont whant to be adding "<< endl" each time, use "\n"

    std::cout << "this will work!\nSee... a new line!" << std::endl;

    0 讨论(0)
  • 2020-11-29 13:35

    "\r" should work for both windows and Mac OS X.

    Something like:

    std::cout << "will not see this\rwill see this" << std::flush;
    std::cout << std::endl; // all done
    
    0 讨论(0)
  • 2020-11-29 13:40

    As Nathan Ernst's answer says, if you want a robust, proper way to do this, use curses - specifically ncurses.

    If you want a low-effort hackish way that tends to work, carry on...

    Command-line terminals for Linux, UNIX, MacOS, Windows etc. tend to support a small set of basic ASCII control characters, including character 13 decimal - known as a Carriage Return and encoded in C++ as '\r' or equivalently in octal '\015' or hex '\x0D' - instructing the terminal to return to the start of the line.

    What you generally want to do is...

    int line_width = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 80;
    std::string spaces{line_width - 1, ' '};
    for (const auto& filename : filenames) {
        std::cout << '\r' << spaces << '\r' << filename << std::flush;
        process_file(filename);
    }
    std::cout << std::endl; // move past last filename...
    

    This uses a string of spaces to overwrite the old filename before writing the next one, so if you have a shorter filename you don't see trailing characters from the earlier longer filename(s).

    The std::flush ensures the C++ program calls the OS write() function to send the text to the terminal before starting to process the file. Without that, the text needed for the update - \r, spaces, \r and a filename - will be appended to a buffer and only written to the OS - in e.g. 4k chunks - when the buffer is full, so the filename displayed would lag dozens of files behind the actual file being processing. Further, say the buffer is 4k - 4096 bytes - and at some point you have 4080 bytes buffered, then output text for the next filename: you'll end up with \r and 15 spaces fitting in the buffer, which when auto-flushed will end up wiping out the first 15 characters on the line on-screen and leaving the rest of the previous filename (if it was longer than 15 characters), then waiting until the buffer is full again before updating the screen (still haphazardly).

    The final std::endl just moves the cursor on from the line where you've been printing filenames so you can write "all done", or just leave main() and have the shell prompt display on a nice clean line, instead of potentially overwriting part of your last filename (great shells like zsh check for this).

    0 讨论(0)
  • 2020-11-29 13:53

    I don't have access to a mac, but from a pure console standpoint, this is going to be largely dependent on how it treats the carriage return and line-feed characters. If you can literally send one or the other to the console, you want to send just a carriage return.

    I'm pretty sure Mac treats both carriage returns and line-feeds differently than *nix & windows.

    If you're looking for in-place updates (e.g. overwrite the current line), I'd recommend looking at the curses lib. This should provide a platform independent means of doing what you're looking for. (because, even using standard C++, there is no platform independent means of what you're asking for).

    0 讨论(0)
提交回复
热议问题