Elliott Hughes | b708d16 | 2015-10-27 16:03:15 -0700 | [diff] [blame^] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef NINJA_LINE_PRINTER_H_ |
| 16 | #define NINJA_LINE_PRINTER_H_ |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <string> |
| 20 | |
| 21 | /// Prints lines of text, possibly overprinting previously printed lines |
| 22 | /// if the terminal supports it. |
| 23 | struct LinePrinter { |
| 24 | LinePrinter(); |
| 25 | |
| 26 | bool is_smart_terminal() const { return smart_terminal_; } |
| 27 | void set_smart_terminal(bool smart) { smart_terminal_ = smart; } |
| 28 | |
| 29 | enum LineType { |
| 30 | FULL, |
| 31 | ELIDE |
| 32 | }; |
| 33 | /// Overprints the current line. If type is ELIDE, elides to_print to fit on |
| 34 | /// one line. |
| 35 | void Print(std::string to_print, LineType type); |
| 36 | |
| 37 | /// Prints a string on a new line, not overprinting previous output. |
| 38 | void PrintOnNewLine(const std::string& to_print); |
| 39 | |
| 40 | /// Lock or unlock the console. Any output sent to the LinePrinter while the |
| 41 | /// console is locked will not be printed until it is unlocked. |
| 42 | void SetConsoleLocked(bool locked); |
| 43 | |
| 44 | private: |
| 45 | /// Whether we can do fancy terminal control codes. |
| 46 | bool smart_terminal_; |
| 47 | |
| 48 | /// Whether the caret is at the beginning of a blank line. |
| 49 | bool have_blank_line_; |
| 50 | |
| 51 | /// Whether console is locked. |
| 52 | bool console_locked_; |
| 53 | |
| 54 | /// Buffered current line while console is locked. |
| 55 | std::string line_buffer_; |
| 56 | |
| 57 | /// Buffered line type while console is locked. |
| 58 | LineType line_type_; |
| 59 | |
| 60 | /// Buffered console output while console is locked. |
| 61 | std::string output_buffer_; |
| 62 | |
| 63 | #ifdef _WIN32 |
| 64 | void* console_; |
| 65 | #endif |
| 66 | |
| 67 | /// Print the given data to the console, or buffer it if it is locked. |
| 68 | void PrintOrBuffer(const char *data, size_t size); |
| 69 | }; |
| 70 | |
| 71 | #endif // NINJA_LINE_PRINTER_H_ |