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