14#ifndef ZYPP_TUI_UTILS_ANSI_H 
   15#define ZYPP_TUI_UTILS_ANSI_H 
   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 )
 
 
  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; }
 
 
  341    { 
return _comp.attr; }
 
 
  344    { 
_comp.attr = attr_r; 
return *
this; }
 
 
  350    { 
_comp.fg = fg_r; 
return *
this; }
 
 
  356    { 
_comp.bg = bg_r; 
return *
this; }
 
 
  360    { 
return _comp.uid; }
 
 
  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; }
 
 
  594    std::string::size_type 
size()
 const 
 
  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 
 
  843template<
class CCC_, 
class Tp_, 
typename = ansi::EnableIfCustomColorCtor<CCC_> >
 
  844inline 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) ); }
 
  847template<
class CCC_, 
typename = ansi::EnableIfCustomColorCtor<CCC_> >
 
  849{ 
return std::move( 
ansi::ColorStream( std::forward<CCC_>(color_r) ) << omanip ); }
 
 
  859template<
class CCC_, 
typename = ztui::ansi::EnableIfCustomColorCtor<CCC_>>
 
  860inline ostream & 
operator<<( ostream & str, CCC_ && color_r )
 
  861{ 
return str << ztui::ansi::Color( forward<CCC_>(color_r) ); }
 
Colored stream output if do_colors.
ColorStream & operator<<(const ColorStream &val_r)
Printing another ColorStream using his Color.
std::ostream & stream() const
Reference to the underlying ostream (direct or auto-created buffer)
ColorStream(std::ostream &direct_r)
Ctor directly printing to a std::ostream (Color::Default)
std::string str() const
Return a buffered streams content as (colored) string.
ColorStream(Color color_r)
Ctor taking a Color.
std::string content() const
Content of a non-empty buffered stream or empty.
bool hasStream() const
Direct or non-empty buffer.
ColorStream & operator<<(Color color_r)
Printing a Color (also via enum) updates the streams Color.
Color color() const
Return streams Color.
ColorStream & operator<<(const ColorString &val_r)
Printing a ColorString using his Color.
std::unique_ptr< std::ostringstream > _bufferP
ColorStream & operator=(Color color_r)
Change the streams Color.
ColorStream(std::ostream &direct_r, Color color_r)
Ctor directly printing to a std::ostream in Color.
ColorStream & operator<<(std::ostream &(*omanip)(std::ostream &))
ColorStream & operator<<(const Tp_ &val_r)
All other types are printed via std::ostream.
friend std::ostream & operator<<(std::ostream &str, const ColorStream &obj)
Print colored on ostream.
ColorStream & operator=(const ColorStream &)=delete
bool hasContent() const
Non-empty buffer (implies direct)
ColorStream & operator=(ColorStream &&)=default
ColorStream()
Default Ctor (Color::Default)
std::unique_ptr< std::ostream, nullDeleter > _directP
ColorStream(const ColorStream &)=delete
non copyable
ColorStream & operator<=(Color color_r)
Update the streams Color.
ColorStream(ColorStream &&)=default
movable
Colored string if do_colors.
ColorString operator()(Color color_r) const
Return a copy with different color.
Color color() const
Return strings Color.
ColorString & operator=(const std::string &str_r)
Assign new string.
ColorString(Color color_r)
Ctor from color.
bool empty() const
Whether the underlying string is empty.
ColorString & operator=(Color color_r)
Assign Color.
ColorString(std::string &&str_r)
std::string str(Color color_r) const
Return the string rendered in a differernt color if do_colors.
const std::string & plainstr() const
Return the underlying plain string.
ColorString & operator=(std::string &&str_r)
std::ostream & operator<<(std::ostream &str, const ColorString &obj)
Print colored on ostream.
const std::string & operator*() const
Access the underlying plain string via operator*.
std::string & plainstr()
Return the underlying plain string.
ColorString & operator<<(const ColorString &rhs)
Append a \Ref ColorString.
std::string::size_type size() const
Size of the underlying string.
std::string str() const
Return the colored string if do_colors.
std::string & operator*()
Access the underlying plain string via operator*.
ColorString(const std::string &str_r, Color color_r)
Ctor from string and color.
ColorString & operator<=(Color color_r)
Update Color.
ColorString(Color color_r, const std::string &str_r)
Ctor from color and string.
ColorString & operator<<(const std::string &str_r)
Append a string.
ColorString(std::string &&str_r, Color color_r)
std::string asString() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
ColorString(Color color_r, std::string &&str_r)
ColorString(const std::string &str_r)
Ctor from string.
Various ways to define ansi SGR sequences.
Color(CCC_ &&color_r)
Custom ctor from ColorTraits enabled type.
Color(Constant color_r, Bg bg_r=Bg::Default)
Color constant combined with background (Bg::Default)
Color()
Default ctor: terminal default color.
friend bool operator!=(Color lhs, Color rhs)
std::string debugstr() const
The colors SGRsequence human readable.
Color operator<(Color::Fg rhs) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
union ztui::ansi::Color::Comp _comp
static Color fromString(const std::string &colorName)
Color(UidType)
ctor nocolor, all Unchanged, uid 0
Color(Fg fg_r, Bg bg_r=Bg::Unchanged)
friend bool operator==(Color lhs, Color rhs)
static Color nocolor()
Leave everything unchanged.
Color(Attr attr_r, Bg bg_r)
Color operator<(Color::Bg rhs) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
static const std::string & SGRReset()
ANSI SGR sesquence to reset all attributes.
Constant
Predefined (foregreound) color contants Intentionally not an enum class, so it can be used as Color::...
Color & attr(Attr attr_r)
std::uint32_t UidType
Color unique id type.
UidType uid() const
Each color has a unique numeric id.
Color & operator<=(Color rhs)
Update Color (assign components which are not Unchanged in rhs )
const std::string & str() const
The colors SGRsequence if do_colors is true.
static std::string & genSGR(Color color_r)
Return a colors SGRsequence if do_colors retruns true.
Color & operator<=(Color::Bg rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Color & operator<=(Color::Fg rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Color & operator<=(Color::Attr rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Color(Attr attr_r, Fg fg_r=Fg::Unchanged, Bg bg_r=Bg::Unchanged)
Color operator<(Color rhs) const
Return updated color.
Color operator<(Color::Attr rhs) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
std::ostream & operator<<(std::ostream &str, Color obj)
Print the colors SGRsequence if do_colors is true.
ANSI Escape sequences and their fallback if no tty.
std::ostream & operator<<(std::ostream &str, const EscapeSequence &obj)
stream output
EscapeSequence(const char *ansi_r, const char *fallback_r="")
typename enable_if< B, T >::type enable_if_t
std::ostream & operator<<(std::ostream &str, const zypp::shared_ptr< void > &obj)
String related utilities and Regular expression matching.
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 mayUseANSIEscapes()
Simple check whether stdout is a (not dumb) tty.
ansi::Color customColorCtor(ColorContext ctxt_r)
bool do_ttyout()
True unless output is a dumb tty or file.
bool hasANSIColor()
Simple check whether stdout can handle colors.
bool do_colors()
If output is done in colors (depends on config)
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).
@ Default
Request the standard behavior (as defined in zypp.conf or 'Job')
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
void operator()(void const *) const
std::enable_if_t< !ansi::ColorTraits< std::decay_t< CCC_ > >::customColorCtor > DisableIfCustomColorCtor
<Tp_> SFINAE: hide template signatures unless enum is enabled in ColorTraits
std::enable_if_t< ansi::ColorTraits< std::decay_t< CCC_ > >::customColorCtor > EnableIfCustomColorCtor
<Tp_> SFINAE: hide template signatures unless enum is enabled in ColorTraits
Comp(Attr attr_r, Fg fg_r, Bg bg_r)