14 #ifndef ZYPP_TUI_UTILS_ANSI_H 15 #define ZYPP_TUI_UTILS_ANSI_H 20 #include <type_traits> 62 const char *
str()
const 80 {
return str << obj.
str(); }
95 #define ZYPPER_TRACE_SGR 0 97 #if ( ZYPPER_TRACE_SGR < 2 ) 151 template <
typename CCC_>
155 template <
typename CCC_>
180 enum class Fg : std::uint8_t
184 enum class Bg : std::uint8_t
214 :
_comp( attr_r, fg_r, bg_r )
218 :
_comp( attr_r,
Fg::Unchanged, bg_r )
258 template<
class CCC_,
typename = EnableIfCustomColorCtor<CCC_>>
293 #if ( ZYPPER_TRACE_SGR ) 294 static const std::string & _reset( *(
new std::string(
ESC"[0m[!]" )) );
296 static const std::string & _reset( *(
new std::string(
ESC"[0m" )) );
298 static const std::string & _noreset( *(
new std::string(
"" )) );
326 {
return Color(*
this) <= rhs; }
329 {
return Color(*
this) <= rhs; }
332 {
return Color(*
this) <= rhs; }
335 {
return Color(*
this) <= rhs; }
350 {
_comp.
fg = fg_r;
return *
this; }
356 {
_comp.
bg = bg_r;
return *
this; }
363 const std::string &
str()
const 364 {
return genSGR( *
this ); }
368 {
return genSGR( *this ).c_str()+1; }
373 {
return( lhs.
uid() == rhs.
uid() ); }
377 {
return ! ( lhs == rhs ); }
383 static std::map<UidType,std::string> & _def( *(
new std::map<UidType,std::string>) );
387 #if ( ZYPPER_TRACE_SGR ) 388 std::string & ret( _def[0] );
397 std::string & ret( _def[color_r.
_comp.
uid] );
412 case Fg::Red: ret +=
"31;";
break;
426 case Bg::Red: ret +=
"41;";
break;
438 #if ( ZYPPER_TRACE_SGR ) 439 ret += ( color_r ==
Color() ?
"[*]" :
"[@]" );
455 :
attr( attr_r ),
fg( fg_r ),
bg( bg_r ),
_f( 0 )
478 {
return str << obj.
str(); }
544 {
_str = str_r;
return *
this; }
547 {
_str = std::move(str_r);
return *
this; }
564 {
_str += rhs.
str();
return *
this; }
568 {
_str += str_r;
return *
this; }
573 {
_color = color_r;
return *
this; }
577 {
_color <= color_r;
return *
this; }
614 #if ( ZYPPER_TRACE_SGR ) 615 return "[\"<]" + ret +
"[>\"]";
644 {
return str << obj.
str(); }
696 { (*_directP) <<
_color; }
710 explicit operator std::ostream &()
763 #if ( ZYPPER_TRACE_SGR ) 770 template<
class Tp_,
typename = DisableIfCustomColorCtor<Tp_>>
772 {
stream() << val_r;
return *
this; }
776 {
stream() << omanip;
return *
this; }
781 #if ( ZYPPER_TRACE_SGR ) 800 _bufferP.reset(
new std::ostringstream );
820 std::unique_ptr<std::ostream,nullDeleter>
_directP;
821 mutable std::unique_ptr<std::ostringstream>
_bufferP;
826 #undef ZYPPER_TRACE_SGR 837 {
return std::move(
ansi::ColorStream( color_r ) << std::forward<Tp_>(val_r) ); }
843 template<
class CCC_,
class Tp_,
typename = ansi::EnableIfCustomColorCtor<CCC_> >
844 inline ansi::ColorStream
operator<<( CCC_ && color_r, Tp_ && val_r )
845 {
return std::move( ansi::ColorStream( std::forward<CCC_>(color_r) ) << std::forward<Tp_>(val_r) ); }
847 template<
class CCC_,
typename = ansi::EnableIfCustomColorCtor<CCC_> >
849 {
return std::move(
ansi::ColorStream( std::forward<CCC_>(color_r) ) << omanip ); }
859 template<
class CCC_,
typename = ztui::ansi::EnableIfCustomColorCtor<CCC_>>
860 inline ostream &
operator<<( ostream &
str, CCC_ && color_r )
861 {
return str << ztui::ansi::Color( forward<CCC_>(color_r) ); }
864 #endif // ZYPP_TUI_UTILS_ANSI_H Comp(Attr attr_r, Fg fg_r, Bg bg_r)
ColorString & operator=(const std::string &str_r)
Assign new string.
ColorStream(Color color_r)
Ctor taking a Color.
ColorString & operator<=(Color color_r)
Update Color.
static Color nocolor()
Leave everything unchanged.
Colored string if do_colors.
ColorStream()
Default Ctor (Color::Default)
EscapeSequence(const char *ansi_r, const char *fallback_r="")
Color & attr(Attr attr_r)
Color & operator<=(Color::Fg rhs)
ColorStream & operator=(const ColorStream &)=delete
UidType uid() const
Each color has a unique numeric id.
void operator()(void const *) const
ansi::Color customColorCtor(ColorContext ctxt_r)
Color()
Default ctor: terminal default color.
ColorString & operator=(std::string &&str_r)
ANSI Escape sequences and their fallback if no tty.
bool do_colors()
If output is done in colors (depends on config)
std::unique_ptr< std::ostream, nullDeleter > _directP
const std::string & str() const
The colors SGRsequence if do_colors is true.
const EscapeSequence cursorRIGHT
Cursor right 1 char.
String related utilities and Regular expression matching.
std::string::size_type size() const
Size of the underlying string.
std::enable_if_t< !ansi::ColorTraits< std::decay_t< CCC_ > >::customColorCtor > DisableIfCustomColorCtor
std::string & operator*()
Access the underlying plain string via operator*.
static Color fromString(const std::string &colorName)
Request the standard behavior (as defined in zypp.conf or 'Job')
friend bool operator==(Color lhs, Color rhs)
ColorString operator()(Color color_r) const
Return a copy with different color.
bool empty() const
Whether the underlying string is empty.
Color color() const
Return streams Color.
ColorStream(std::ostream &direct_r)
Ctor directly printing to a std::ostream (Color::Default)
ColorStream(std::ostream &direct_r, Color color_r)
Ctor directly printing to a std::ostream in Color.
ColorString(std::string &&str_r, Color color_r)
Color operator<(Color::Fg rhs) const
union ztui::ansi::Color::Comp _comp
std::ostream & stream() const
Reference to the underlying ostream (direct or auto-created buffer)
std::string str() const
Return the colored string if do_colors.
std::string & replaceAll(std::string &str_r, const std::string &from_r, const std::string &to_r)
Replace all occurrences of from_r with to_r in str_r (inplace).
ColorStream & operator<<(std::ostream &(*omanip)(std::ostream &))
typename enable_if< B, T >::type enable_if_t
Various ways to define ansi SGR sequences.
std::uint32_t UidType
Color unique id type.
std::string str() const
Return a buffered streams content as (colored) string.
Color(CCC_ &&color_r)
Custom ctor from ColorTraits enabled type.
bool hasStream() const
Direct or non-empty buffer.
ColorStream & operator<<(Color color_r)
Printing a Color (also via enum) updates the streams Color.
Color operator<(Color::Bg rhs) const
ColorString & operator=(Color color_r)
Assign Color.
bool do_ttyout()
True unless output is a dumb tty or file.
TableRow && operator<<(TableRow &&tr, Tp_ &&val)
bool hasANSIColor()
Simple check whether stdout can handle colors.
std::string & plainstr()
Return the underlying plain string.
ColorStream & operator<<(const ColorStream &val_r)
Printing another ColorStream using his Color.
std::string str(Color color_r) const
Return the string rendered in a differernt color if do_colors.
const EscapeSequence cursorUP
Cursor up 1 line.
bool hasContent() const
Non-empty buffer (implies direct)
Colored stream output if do_colors.
ColorString(const std::string &str_r)
Ctor from string.
Color(UidType)
ctor nocolor, all ::Unchanged, uid 0
Color(Constant color_r, Bg bg_r=Bg::Default)
Color constant combined with background (Bg::Default)
Color operator<(Color::Attr rhs) const
ColorStream & operator<<(const Tp_ &val_r)
All other types are printed via std::ostream.
static std::string & genSGR(Color color_r)
Return a colors SGRsequence if do_colors retruns true.
std::ostream & operator<<(std::ostream &str, const zypp::shared_ptr< void > &obj)
const std::string & plainstr() const
Return the underlying plain string.
bool mayUseANSIEscapes()
Simple check whether stdout is a (not dumb) tty.
ColorString(Color color_r)
Ctor from color.
Color operator<(Color rhs) const
Return updated color.
Color(Fg fg_r, Bg bg_r=Bg::Unchanged)
std::ostream & operator<<(std::ostream &str, Color obj)
const EscapeSequence cursorLEFT
Cursor left 1 char.
std::ostream & operator<<(std::ostream &str, const EscapeSequence &obj)
std::ostream & operator<<(std::ostream &str, const ColorString &obj)
Color & operator<=(Color rhs)
Update Color (assign components which are not Unchanged in rhs )
ColorString(Color color_r, std::string &&str_r)
std::string content() const
Content of a non-empty buffered stream or empty.
ColorString & operator<<(const ColorString &rhs)
Append a ColorString.
const EscapeSequence cursorDOWN
Cursor down 1 line.
Color(Attr attr_r, Fg fg_r=Fg::Unchanged, Bg bg_r=Bg::Unchanged)
ColorString(Color color_r, const std::string &str_r)
Ctor from color and string.
ColorString(std::string &&str_r)
static const std::string & SGRReset()
ANSI SGR sesquence to reset all attributes.
ColorStream & operator<=(Color color_r)
Update the streams Color.
const std::string & operator*() const
Access the underlying plain string via operator*.
std::string asString() const
Color & operator<=(Color::Attr rhs)
ColorString & operator<<(const std::string &str_r)
Append a string.
ColorString(const std::string &str_r, Color color_r)
Ctor from string and color.
Constant
Predefined (foregreound) color contants Intentionally not an enum class, so it can be used as Color::...
friend std::ostream & operator<<(std::ostream &str, const ColorStream &obj)
Color & operator<=(Color::Bg rhs)
std::string debugstr() const
The colors SGRsequence human readable.
const EscapeSequence clearLN
Clear entire line.
Color color() const
Return strings Color.
ColorStream & operator<<(const ColorString &val_r)
Printing a ColorString using his Color.
friend bool operator!=(Color lhs, Color rhs)
ColorStream & operator=(Color color_r)
Change the streams Color.
std::enable_if_t< ansi::ColorTraits< std::decay_t< CCC_ > >::customColorCtor > EnableIfCustomColorCtor
Color(Attr attr_r, Bg bg_r)
std::unique_ptr< std::ostringstream > _bufferP