terminal.h
Go to the documentation of this file.
1 // Encapsulates terminal control (colors, cursor, ...)
2 // Author: Max Schwarz <max.schwarz@uni-bonn.de>
3 
4 #ifndef TERMINAL_H
5 #define TERMINAL_H
6 
7 #include <stdint.h>
8 #include <chrono>
9 #include <string>
10 #include <map>
11 #include <vector>
12 
13 namespace rosmon
14 {
15 
22 class Terminal
23 {
24 public:
32  {
34  Red,
49 
51  Grayscale = 0xe8
52  };
53 
54  class Color
55  {
56  public:
58  {}
59 
60  void foreground() { fputs(m_fgString.c_str(), stdout); }
61  void background() { fputs(m_bgString.c_str(), stdout); }
62 
63  const std::string& foregroundCode() const { return m_fgString; }
64  const std::string& backgroundCode() const { return m_bgString; }
65  private:
66  friend class Terminal;
67 
68  Color(const std::string& fg, const std::string& bg)
69  : m_fgString{fg}, m_bgString{bg}
70  {}
71 
72  std::string m_fgString;
73  std::string m_bgString;
74  };
75 
76  class Style
77  {
78  public:
80  {}
81 
82  Style(Color fg, Color bg)
83  : m_fg(std::move(fg)), m_bg(std::move(bg))
84  {}
85 
86  void use()
87  {
88  m_fg.foreground();
89  m_bg.background();
90  }
91  private:
94  };
95 
102  class Parser
103  {
104  public:
105  Parser() {}
106  Parser(Terminal* terminal);
107 
109  bool parse(char c);
110 
112  void parse(const std::string& str);
113 
115  void apply();
116 
123  std::vector<std::string> wrap(const std::string& str, unsigned int columns);
124  private:
125  void parseSetAttributes(const std::string& str);
126 
127  enum State
128  {
131  STATE_CSI
132  };
133 
134  Terminal* m_term = nullptr;
135 
136  State m_state = STATE_ESCAPE;
137  std::string m_buf;
138 
141  bool m_bold = false;
142  };
143 
144  Terminal();
145 
146  Color color(SimpleColor code);
147  Color color(uint32_t truecolor, SimpleColor fallback);
148 
155  void setForegroundColor(uint32_t color);
156 
163  void setBackgroundColor(uint32_t color);
164 
166  void setCursorInvisible();
167 
169  void setCursorVisible();
170 
174  void setEcho(bool on);
175 
176  void setBold(bool on);
177 
179 
184 
186  void setStandardColors();
187 
189  std::string standardColorCode();
190 
192  void clearToEndOfLine();
193 
195  void moveCursorUp(int numLines);
196 
199 
205  bool getSize(int* columns, int* rows);
206 
212  bool has256Colors() const;
213 
217  bool interactive() const
218  { return m_valid; }
219 
220  void setWindowTitle(const std::string& title);
221  void clearWindowTitle(const std::string& backup);
222 
223 
225  {
226  SK_F1 = 0x100, SK_F2, SK_F3, SK_F4,
229 
231 
233  };
234 
235  int readKey();
236  int readLeftover();
237 
241  void setLineWrap(bool on);
242 
243 private:
244  bool m_valid;
247 
248  std::string m_bgColorStr;
249  std::string m_fgColorStr;
250  std::string m_opStr;
251  std::string m_sgr0Str;
252  std::string m_elStr;
253  std::string m_upStr;
254  std::string m_boldStr;
255  std::string m_lineWrapOffStr;
256  std::string m_lineWrapOnStr;
257 
258  std::map<std::string, SpecialKey> m_specialKeys;
259 
260  std::string m_currentEscapeStr;
261  std::chrono::steady_clock::time_point m_escapeStartTime;
263  unsigned int m_currentEscapeAbortIdx = 0;
264 };
265 
266 }
267 
268 #endif
std::string m_lineWrapOnStr
Definition: terminal.h:256
24-step grayscale starts here
Definition: terminal.h:51
std::string standardColorCode()
Escape codes for standard colors.
Definition: terminal.cpp:443
SimpleColor
Simple colors.
Definition: terminal.h:31
bool interactive() const
Definition: terminal.h:217
bool getSize(int *columns, int *rows)
Get current window size.
Definition: terminal.cpp:480
void setForegroundColor(uint32_t color)
Set 24-bit foreground color.
Definition: terminal.cpp:359
void setSimpleBackground(SimpleColor color)
Definition: terminal.cpp:416
std::map< std::string, SpecialKey > m_specialKeys
Definition: terminal.h:258
void moveCursorToStartOfLine()
Move cursor to start of the line.
Definition: terminal.cpp:467
Color color(SimpleColor code)
Definition: terminal.cpp:616
void setBackgroundColor(uint32_t color)
Set 24-bit background color.
Definition: terminal.cpp:341
std::chrono::steady_clock::time_point m_escapeStartTime
Definition: terminal.h:261
std::string m_lineWrapOffStr
Definition: terminal.h:255
std::string m_fgColorStr
Definition: terminal.h:249
friend class Terminal
Definition: terminal.h:66
void setEcho(bool on)
Definition: terminal.cpp:377
void setSimplePair(SimpleColor fg, SimpleColor bg)
Definition: terminal.cpp:425
std::string m_currentEscapeStr
Definition: terminal.h:260
const std::string & backgroundCode() const
Definition: terminal.h:64
std::string m_opStr
Definition: terminal.h:250
void setLineWrap(bool on)
Definition: terminal.cpp:472
std::string m_boldStr
Definition: terminal.h:254
void clearToEndOfLine()
Clear characters from cursor to end of line.
Definition: terminal.cpp:451
void setStandardColors()
Reset fg + bg to standard terminal colors.
Definition: terminal.cpp:434
std::string m_bgColorStr
Definition: terminal.h:248
std::string m_elStr
Definition: terminal.h:252
std::string m_upStr
Definition: terminal.h:253
void setCursorVisible()
restore cursor
Definition: terminal.cpp:320
void setSimpleForeground(SimpleColor color)
Definition: terminal.cpp:407
Color(const std::string &fg, const std::string &bg)
Definition: terminal.h:68
Encapsulates terminal control.
Definition: terminal.h:22
void moveCursorUp(int numLines)
Move cursor up by numLines.
Definition: terminal.cpp:459
bool has256Colors() const
Definition: terminal.cpp:307
void setWindowTitle(const std::string &title)
Definition: terminal.cpp:493
std::string m_sgr0Str
Definition: terminal.h:251
void setCursorInvisible()
hide cursor
Definition: terminal.cpp:312
const std::string & foregroundCode() const
Definition: terminal.h:63
Terminal escape sequence parser.
Definition: terminal.h:102
Style(Color fg, Color bg)
Definition: terminal.h:82
unsigned int m_currentEscapeAbortIdx
Definition: terminal.h:263
void clearWindowTitle(const std::string &backup)
Definition: terminal.cpp:506
std::string m_bgString
Definition: terminal.h:73
void setBold(bool on)
Definition: terminal.cpp:398
bool m_currentEscapeAborted
Definition: terminal.h:262
std::string m_fgString
Definition: terminal.h:72


rosmon_core
Author(s): Max Schwarz
autogenerated on Sat Jan 9 2021 03:35:43