14 #include <sys/ioctl.h> 15 #include <sys/types.h> 17 #include <boost/lexical_cast.hpp> 18 #include <boost/regex.hpp> 19 #include <boost/tokenizer.hpp> 21 #include <fmt/format.h> 27 : m_state(STATE_ESCAPE)
36 using Tokenizer = boost::tokenizer<boost::char_separator<char>>;
38 boost::char_separator<char> sep(
";");
39 Tokenizer tok(str, sep);
41 for(Tokenizer::iterator it = tok.begin(); it != tok.end(); ++it)
44 auto endptr =
const_cast<char*
>(it->c_str());
45 int code = strtoul(it->c_str(), &endptr, 10);
47 if(errno != 0 || *endptr != 0)
60 else if(code >= 30 && code <= 37)
62 else if(code >= 40 && code <= 47)
95 if(
m_buf.length() >= 16)
117 else if(m_bgColor >= 0)
128 char* overrideMode = getenv(
"ROSMON_COLOR_MODE");
129 const char* termOverride =
nullptr;
132 if(strcasecmp(overrideMode,
"truecolor") == 0)
134 termOverride =
"xterm-256color";
138 else if(strcasecmp(overrideMode,
"256colors") == 0)
140 termOverride =
"xterm-256color";
144 else if(strcasecmp(overrideMode,
"ansi") == 0)
151 fmt::print(stderr,
"Warning: Unknown ROSMON_COLOR_MODE value: '{}'\n", overrideMode);
157 if(getenv(
"KONSOLE_DBUS_SESSION"))
166 char* vte_version = getenv(
"VTE_VERSION");
167 if(vte_version && boost::lexical_cast<unsigned int>(vte_version) >= 3600)
175 if(setupterm(termOverride, STDOUT_FILENO, &ret) != OK)
177 fmt::print(
"Could not setup the terminal. Disabling all colors...\n");
186 int num_colors = tigetnum(
"colors");
191 const char* bgColor = tigetstr(
"setab");
192 if(bgColor && bgColor != (
char*)-1)
195 fmt::print(
"Your terminal does not support ANSI background!\n");
198 const char* fgColor = tigetstr(
"setaf");
199 if(fgColor && fgColor != (
char*)-1)
202 fmt::print(
"Your terminal does not support ANSI foreground!\n");
213 for(
int i = 0; i < 12; ++i)
215 char* code = tigetstr(fmt::format(
"kf{}", i+1).c_str());
218 if(code && code != reinterpret_cast<char*>(-1))
233 putp(tigetstr(
"civis"));
241 putp(tigetstr(
"cnorm"));
246 int r = (rgb & 0xFF);
247 int g = (rgb >> 8) & 0xFF;
248 int b = (rgb >> 16) & 0xFF;
254 return 16 + 36 * r + 6 * g +
b;
265 snprintf(buf,
sizeof(buf),
"\033[48;2;%d;%d;%dm", color & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF);
283 snprintf(buf,
sizeof(buf),
"\033[38;2;%d;%d;%dm", color & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF);
297 if(tcgetattr(STDIN_FILENO, &ios) == 0)
302 ios.c_lflag |= ICANON;
306 ios.c_lflag &= ~ECHO;
307 ios.c_lflag &= ~ICANON;
310 tcsetattr(STDIN_FILENO, TCSANOW, &ios);
372 putp(tparm(
m_upStr.c_str(), numLines));
383 if(ioctl(STDIN_FILENO, TIOCGWINSZ, &w) == 0)
385 *outColumns = w.ws_col;
398 snprintf(buf,
sizeof(buf),
"\033]30;%s\007", title.c_str());
402 snprintf(buf,
sizeof(buf),
"\033k%s\033\\", title.c_str());
408 fputs(
"\033]30;%d : %n\007", stdout);
411 fmt::print(
"\033k{}\033\\", backup);
417 if(read(STDIN_FILENO, &c, 1) != 1)
428 std::size_t matches = 0;
430 bool completeMatch =
false;
440 lastMatch = pair.second;
444 completeMatch =
true;
455 else if(completeMatch)
static int ansiColor(uint32_t rgb)
SimpleColor
Simple colors.
bool getSize(int *columns, int *rows)
Get current window size.
void parse(char c)
parse single character c
void setForegroundColor(uint32_t color)
Set 24-bit foreground color.
void setSimpleBackground(SimpleColor color)
std::map< std::string, SpecialKey > m_specialKeys
void moveCursorToStartOfLine()
Move cursor to start of the line.
void setBackgroundColor(uint32_t color)
Set 24-bit background color.
void parseSetAttributes(const std::string &str)
void setSimplePair(SimpleColor fg, SimpleColor bg)
std::string m_currentEscapeStr
void clearToEndOfLine()
Clear characters from cursor to end of line.
void setStandardColors()
Reset fg + bg to standard terminal colors.
void setCursorVisible()
restore cursor
void setSimpleForeground(SimpleColor color)
Encapsulates terminal control.
void moveCursorUp(int numLines)
Move cursor up by numLines.
bool has256Colors() const
void setWindowTitle(const std::string &title)
void setCursorInvisible()
hide cursor
void apply(Terminal *term)
Apply the current internal state (colors) on the terminal.
void clearWindowTitle(const std::string &backup)