libzypp 17.37.17
colors.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8----------------------------------------------------------------------*/
9
10#include <iostream>
11
12#include <zypp/base/Logger.h>
13#include <zypp-tui/Application>
14#include "colors.h"
15
16namespace ztui {
17
18namespace env
19{
20 inline bool NO_COLOR()
21 { return ::getenv("NO_COLOR"); }
22} // namespace env
23
25// from ansi.h
26
28{
29 return Application::instance().config().do_ttyout;
30}
31
33{
34 return Application::instance().config().do_colors;
35}
36
38{
39 constexpr auto detectAnsiEscapes = [](){
40 if ( ::isatty(STDOUT_FILENO) )
41 {
42 char *term = ::getenv("TERM");
43 if ( term && ::strcmp( term, "dumb" ) )
44 return true;
45 }
46 return false;
47 };
48
49 static bool mayUse = detectAnsiEscapes();
50 return mayUse;
51}
52
54{ return mayUseANSIEscapes() && not env::NO_COLOR(); }
55
56namespace ansi
57{
58 namespace tty
59 {
60 const EscapeSequence clearLN ( "\033[2K\r", "\n" );
61 const EscapeSequence cursorUP ( "\033[1A" );
62 const EscapeSequence cursorDOWN ( "\033[1B" );
63 const EscapeSequence cursorRIGHT ( "\033[1C" );
64 const EscapeSequence cursorLEFT ( "\033[1D" );
65 } // namespace tty
66
67
68 Color Color::fromString(const std::string &colorName)
69 {
70 static const std::map<std::string, ansi::Color> _def = {
71 { "black", ansi::Color::Black },
72 { "darkgrey", ansi::Color::BrightBlack },
73 { "red", ansi::Color::Red },
74 { "green", ansi::Color::Green },
75 { "brown", ansi::Color::Yellow },
76 { "yellow", ansi::Color::BrightYellow },
77 { "blue", ansi::Color::Blue },
78 { "magenta", ansi::Color::Magenta },
79 { "purple", ansi::Color::Magenta },
80 { "cyan", ansi::Color::Cyan },
81 { "grey", ansi::Color::White },
82 { "white", ansi::Color::BrightWhite },
83 { "default", ansi::Color::Default },
84 { "", ansi::Color::Default }, // matches "bold" "light" "bright" NOT ""
85 };
86
88
89 if ( colorName.empty() ) // "" when undefined in config file
90 return ret;
91
92 std::string name_r = zypp::str::toLower( colorName );
93
94 if ( zypp::str::hasPrefix( name_r, "bold" ) ) {
95 name_r.erase( 0, 4 );
97
98 } else if ( zypp::str::hasPrefix( name_r, "light" ) ) {
99 name_r.erase( 0, 5 );
101
102 } else if ( zypp::str::hasPrefix( name_r, "bright" ) ) {
103 name_r.erase( 0, 6 );
105 }
106
107 auto && it = _def.find( name_r );
108 if ( it == _def.end() )
109 {
110 ERR << "Unknown color name '" << name_r << "'" << std::endl;
112 }
113 else
114 {
115 ret = ( it->second < ret );
116 }
117 return ret;
118 }
119
120
121} // namespace tty
122// from ansi.h
124
126{
127 const ztui::Config & conf( Application::instance().config() );
128 switch ( ctxt_r )
129 {
130 case ColorContext::RESULT: return conf.color_result;
133 case ColorContext::MSG_ERROR: return conf.color_msgError;
134 case ColorContext::PROMPT: return conf.color_prompt;
136 case ColorContext::POSITIVE: return conf.color_positive;
137 case ColorContext::CHANGE: return conf.color_change;
138 case ColorContext::NEGATIVE: return conf.color_negative;
140 case ColorContext::LOWLIGHT: return conf.color_lowlight;
141 case ColorContext::OSDEBUG: return conf.color_osdebug;
142
144 break; // use default...
145 }
146 return ansi::Color::Default; // default
147}
148
149}
static Application & instance()
ansi::Color color_osdebug
Definition config.h:50
ansi::Color color_promptOption
Definition config.h:44
ansi::Color color_msgWarning
Definition config.h:42
ansi::Color color_highlight
Definition config.h:48
ansi::Color color_negative
Definition config.h:47
ansi::Color color_positive
Definition config.h:45
ansi::Color color_change
Definition config.h:46
ansi::Color color_lowlight
Definition config.h:49
ansi::Color color_msgStatus
Definition config.h:40
ansi::Color color_prompt
Definition config.h:43
ansi::Color color_msgError
Definition config.h:41
ansi::Color color_result
Definition config.h:39
Various ways to define ansi SGR sequences.
Definition ansi.h:173
Color()
Default ctor: terminal default color.
Definition ansi.h:209
static Color fromString(const std::string &colorName)
Definition colors.cc:68
static Color nocolor()
Leave everything unchanged.
Definition ansi.h:265
const EscapeSequence cursorUP
Cursor up 1 line.
const EscapeSequence clearLN
Clear entire line.
const EscapeSequence cursorDOWN
Cursor down 1 line.
const EscapeSequence cursorRIGHT
Cursor right 1 char.
const EscapeSequence cursorLEFT
Cursor left 1 char.
bool NO_COLOR()
Definition colors.cc:20
bool mayUseANSIEscapes()
Simple check whether stdout is a (not dumb) tty.
Definition colors.cc:37
ansi::Color customColorCtor(ColorContext ctxt_r)
Definition colors.cc:125
ColorContext
Definition colors.h:35
bool do_ttyout()
True unless output is a dumb tty or file.
Definition colors.cc:27
bool hasANSIColor()
Simple check whether stdout can handle colors.
Definition colors.cc:53
bool do_colors()
If output is done in colors (depends on config)
Definition colors.cc:32
Namespace intended to collect all environment variables we use.
Definition Env.h:25
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition String.h:1097
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition String.cc:180
#define ERR
Definition Logger.h:102