libzypp 17.37.10

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
16 namespace ztui {
17
18 namespace env
19{
20 inline bool NO_COLOR()
21 { return ::getenv("NO_COLOR"); }
22} // namespace env
23
25// from ansi.h
26
27 bool do_ttyout()
28{
29 return Application::instance().config().do_ttyout;
30}
31
32 bool do_colors()
33{
34 return Application::instance().config().do_colors;
35}
36
37 bool mayUseANSIEscapes()
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
53 bool hasANSIColor()
54{ return mayUseANSIEscapes() && not env::NO_COLOR(); }
55
56 namespace 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
87 ansi::Color ret = ansi::Color::nocolor();
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 );
96 ret <= ansi::Color::Attr::Bright;
97
98 } else if ( zypp::str::hasPrefix( name_r, "light" ) ) {
99 name_r.erase( 0, 5 );
100 ret <= ansi::Color::Attr::Bright;
101
102 } else if ( zypp::str::hasPrefix( name_r, "bright" ) ) {
103 name_r.erase( 0, 6 );
104 ret <= ansi::Color::Attr::Bright;
105 }
106
107 auto && it = _def.find( name_r );
108 if ( it == _def.end() )
109 {
110 ERR << "Unknown color name '" << name_r << "'" << std::endl;
111 ret = ansi::Color::Default;
112 }
113 else
114 {
115 ret = ( it->second < ret );
116 }
117 return ret;
118 }
119
120
121} // namespace tty
122// from ansi.h
124
125 ansi::Color customColorCtor( ColorContext ctxt_r )
126{
127 const ztui::Config & conf( Application::instance().config() );
128 switch ( ctxt_r )
129 {
130 case ColorContext::RESULT: return conf.color_result;
131 case ColorContext::MSG_STATUS: return conf.color_msgStatus;
132 case ColorContext::MSG_WARNING: return conf.color_msgWarning;
133 case ColorContext::MSG_ERROR: return conf.color_msgError;
134 case ColorContext::PROMPT: return conf.color_prompt;
135 case ColorContext::PROMPT_OPTION: return conf.color_promptOption;
136 case ColorContext::POSITIVE: return conf.color_positive;
137 case ColorContext::CHANGE: return conf.color_change;
138 case ColorContext::NEGATIVE: return conf.color_negative;
139 case ColorContext::HIGHLIGHT: return conf.color_highlight;
140 case ColorContext::LOWLIGHT: return conf.color_lowlight;
141 case ColorContext::OSDEBUG: return conf.color_osdebug;
142
143 case ColorContext::DEFAULT:
144 break; // use default...
145 }
146 return ansi::Color::Default; // default
147}
148
149}
ztui::Application::instance
static Application & instance()
Definition application.cc:45
ztui::Config
Definition config.h:21
ztui::Config::color_osdebug
ansi::Color color_osdebug
Definition config.h:50
ztui::Config::color_promptOption
ansi::Color color_promptOption
Definition config.h:44
ztui::Config::color_msgWarning
ansi::Color color_msgWarning
Definition config.h:42
ztui::Config::color_highlight
ansi::Color color_highlight
Definition config.h:48
ztui::Config::color_negative
ansi::Color color_negative
Definition config.h:47
ztui::Config::color_positive
ansi::Color color_positive
Definition config.h:45
ztui::Config::color_change
ansi::Color color_change
Definition config.h:46
ztui::Config::color_lowlight
ansi::Color color_lowlight
Definition config.h:49
ztui::Config::color_msgStatus
ansi::Color color_msgStatus
Definition config.h:40
ztui::Config::color_prompt
ansi::Color color_prompt
Definition config.h:43
ztui::Config::color_msgError
ansi::Color color_msgError
Definition config.h:41
ztui::Config::color_result
ansi::Color color_result
Definition config.h:39
ztui::ansi::Color
Various ways to define ansi SGR sequences.
Definition ansi.h:173
ztui::ansi::Color::Color
Color()
Default ctor: terminal default color.
Definition ansi.h:209
ztui::ansi::Color::fromString
static Color fromString(const std::string &colorName)
Definition colors.cc:68
ztui::ansi::Color::nocolor
static Color nocolor()
Leave everything unchanged.
Definition ansi.h:265
ztui::ansi::Color::Black
@ Black
Definition ansi.h:196
ztui::ansi::Color::Green
@ Green
Definition ansi.h:198
ztui::ansi::Color::Magenta
@ Magenta
Definition ansi.h:201
ztui::ansi::Color::BrightYellow
@ BrightYellow
Definition ansi.h:199
ztui::ansi::Color::White
@ White
Definition ansi.h:203
ztui::ansi::Color::Red
@ Red
Definition ansi.h:197
ztui::ansi::Color::BrightBlack
@ BrightBlack
Definition ansi.h:196
ztui::ansi::Color::Yellow
@ Yellow
Definition ansi.h:199
ztui::ansi::Color::BrightWhite
@ BrightWhite
Definition ansi.h:203
ztui::ansi::Color::Blue
@ Blue
Definition ansi.h:200
ztui::ansi::Color::Default
@ Default
Definition ansi.h:204
ztui::ansi::Color::Cyan
@ Cyan
Definition ansi.h:202
ztui::ansi::Color::Attr::Bright
@ Bright
Definition ansi.h:177
colors.h
ztui::ansi::tty::cursorUP
const EscapeSequence cursorUP
Cursor up 1 line.
ztui::ansi::tty::clearLN
const EscapeSequence clearLN
Clear entire line.
ztui::ansi::tty::cursorDOWN
const EscapeSequence cursorDOWN
Cursor down 1 line.
ztui::ansi::tty::cursorRIGHT
const EscapeSequence cursorRIGHT
Cursor right 1 char.
ztui::ansi::tty::cursorLEFT
const EscapeSequence cursorLEFT
Cursor left 1 char.
ztui::env::NO_COLOR
bool NO_COLOR()
Definition colors.cc:20
ztui
Definition application.cc:13
ztui::mayUseANSIEscapes
bool mayUseANSIEscapes()
Simple check whether stdout is a (not dumb) tty.
Definition colors.cc:37
ztui::customColorCtor
ansi::Color customColorCtor(ColorContext ctxt_r)
Definition colors.cc:125
ztui::ColorContext
ColorContext
Definition colors.h:35
ztui::ColorContext::MSG_WARNING
@ MSG_WARNING
Definition colors.h:41
ztui::ColorContext::PROMPT
@ PROMPT
Definition colors.h:42
ztui::ColorContext::CHANGE
@ CHANGE
Definition colors.h:45
ztui::ColorContext::NEGATIVE
@ NEGATIVE
Definition colors.h:46
ztui::ColorContext::HIGHLIGHT
@ HIGHLIGHT
Definition colors.h:47
ztui::ColorContext::DEFAULT
@ DEFAULT
Definition colors.h:36
ztui::ColorContext::MSG_ERROR
@ MSG_ERROR
Definition colors.h:40
ztui::ColorContext::OSDEBUG
@ OSDEBUG
Definition colors.h:50
ztui::ColorContext::RESULT
@ RESULT
Definition colors.h:38
ztui::ColorContext::POSITIVE
@ POSITIVE
Definition colors.h:44
ztui::ColorContext::LOWLIGHT
@ LOWLIGHT
Definition colors.h:48
ztui::ColorContext::PROMPT_OPTION
@ PROMPT_OPTION
Definition colors.h:43
ztui::ColorContext::MSG_STATUS
@ MSG_STATUS
Definition colors.h:39
ztui::do_ttyout
bool do_ttyout()
True unless output is a dumb tty or file.
Definition colors.cc:27
ztui::hasANSIColor
bool hasANSIColor()
Simple check whether stdout can handle colors.
Definition colors.cc:53
ztui::do_colors
bool do_colors()
If output is done in colors (depends on config)
Definition colors.cc:32
zypp::env
Namespace intended to collect all environment variables we use.
Definition Env.h:25
zypp::str::hasPrefix
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition String.h:1097
zypp::str::toLower
std::string toLower(const std::string &s)
Return lowercase version of s.
Definition String.cc:180
ERR
#define ERR
Definition Logger.h:102
Logger.h