Ninja
line_printer.h
Go to the documentation of this file.
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  bool supports_color() const { return supports_color_; }
30 
31  enum LineType {
33  ELIDE
34  };
35  /// Overprints the current line. If type is ELIDE, elides to_print to fit on
36  /// one line.
37  void Print(std::string to_print, LineType type);
38 
39  /// Prints a string on a new line, not overprinting previous output.
40  void PrintOnNewLine(const std::string& to_print);
41 
42  /// Lock or unlock the console. Any output sent to the LinePrinter while the
43  /// console is locked will not be printed until it is unlocked.
44  void SetConsoleLocked(bool locked);
45 
46  private:
47  /// Whether we can do fancy terminal control codes.
49 
50  /// Whether we can use ISO 6429 (ANSI) color sequences.
52 
53  /// Whether the caret is at the beginning of a blank line.
55 
56  /// Whether console is locked.
58 
59  /// Buffered current line while console is locked.
60  std::string line_buffer_;
61 
62  /// Buffered line type while console is locked.
64 
65  /// Buffered console output while console is locked.
66  std::string output_buffer_;
67 
68 #ifdef _WIN32
69  void* console_;
70 #endif
71 
72  /// Print the given data to the console, or buffer it if it is locked.
73  void PrintOrBuffer(const char *data, size_t size);
74 };
75 
76 #endif // NINJA_LINE_PRINTER_H_
Prints lines of text, possibly overprinting previously printed lines if the terminal supports it.
Definition: line_printer.h:23
void PrintOrBuffer(const char *data, size_t size)
Print the given data to the console, or buffer it if it is locked.
void set_smart_terminal(bool smart)
Definition: line_printer.h:27
bool smart_terminal_
Whether we can do fancy terminal control codes.
Definition: line_printer.h:48
void Print(std::string to_print, LineType type)
Overprints the current line.
Definition: line_printer.cc:67
bool console_locked_
Whether console is locked.
Definition: line_printer.h:57
bool have_blank_line_
Whether the caret is at the beginning of a blank line.
Definition: line_printer.h:54
void SetConsoleLocked(bool locked)
Lock or unlock the console.
std::string output_buffer_
Buffered console output while console is locked.
Definition: line_printer.h:66
bool supports_color_
Whether we can use ISO 6429 (ANSI) color sequences.
Definition: line_printer.h:51
std::string line_buffer_
Buffered current line while console is locked.
Definition: line_printer.h:60
bool supports_color() const
Definition: line_printer.h:29
bool is_smart_terminal() const
Definition: line_printer.h:26
void PrintOnNewLine(const std::string &to_print)
Prints a string on a new line, not overprinting previous output.
LineType line_type_
Buffered line type while console is locked.
Definition: line_printer.h:63