libzypp  17.32.4
Out.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 ----------------------------------------------------------------------/
9 *
10 * This file contains private API, this might break at any time between releases.
11 * Strictly for internal use!
12 */
13 
14 #ifndef OUT_H_
15 #define OUT_H_
16 
17 #include <string>
18 #include <sstream>
19 #include <optional>
20 
21 #include <zypp-core/base/Xml.h>
24 #include <zypp-core/base/String.h>
25 #include <zypp-core/base/Flags.h>
26 #include <utility>
27 #include <zypp-core/base/DefaultIntegral>
28 #include <zypp-core/base/DtorReset>
29 #include <zypp-core/base/Gettext.h>
30 #include <zypp-core/Url.h>
31 #include <zypp-core/TriBool.h>
32 #include <zypp-core/ui/ProgressData>
34 
35 #include <zypp-tui/utils/text.h>
36 #include <zypp-tui/utils/colors.h>
38 #include <zypp-tui/Table.h>
39 #include <zypp-tui/output/PromptOptions>
40 
41 namespace ztui {
42 
43 class Application;
44 
46 enum class ProgressEnd { done, attention, error };
47 
48 namespace text
49 {
50  // translator: usually followed by a ' ' and some explanatory text
51  inline ColorString tagNote() { return HIGHLIGHTString(_("Note:") ); }
52  // translator: usually followed by a ' ' and some explanatory text
53  inline ColorString tagWarning() { return MSG_WARNINGString(_("Warning:") ); }
54  // translator: usually followed by a ' ' and some explanatory text
55  inline ColorString tagError() { return MSG_ERRORString(_("Error:") ); }
56 
57  inline const char * qContinue() { return _("Continue?"); }
58 
59 
61  template <class Tltext, class Trtext>
62  inline std::string join( const Tltext & ltext, const Trtext & rtext, const char * sep = " " )
63  { std::string ret( zypp::str::asString(ltext) ); ret += sep; ret += zypp::str::asString(rtext); return ret; }
64 
66  inline bool endsOnWS( const std::string & str_r )
67  {
68  bool ret = false;
69  if ( !str_r.empty() )
70  { int l = *str_r.rbegin(); if ( ::strchr( " \n\t", l ) ) ret = true; }
71  return ret;
72  }
73 
74  inline const char * optBlankAfter( const std::string & str_r )
75  { return( endsOnWS( str_r ) ? "" : " " ); }
76 }
77 
78 namespace out
79 {
80  static constexpr unsigned termwidthUnlimited = 0u;
81  unsigned defaultTermwidth(); // Zypper::instance().out().termwidth()
82 } // namespace out
83 
84 namespace out
85 {
91  struct ListLayout
92  {
93  template <class TFormater> struct Writer;
94 
95  ListLayout( bool singleline_r, bool wrapline_r, bool gaped_r, unsigned indent_r )
96  : _singleline( singleline_r )
97  , _wrapline( wrapline_r )
98  , _gaped( gaped_r )
99  , _indent( indent_r )
100  {}
101  bool _singleline;
102  bool _wrapline;
103  bool _gaped;
104  unsigned _indent;
105  };
106 
107  namespace detail
108  {
109  template <bool singleline_, bool wrapline_, bool gaped_, unsigned indent_>
110  struct ListLayoutInit : public ListLayout { ListLayoutInit() : ListLayout( singleline_, wrapline_, gaped_, indent_ ) {} };
111  }
112 
119 
124  struct TableLayout
125  {
126  template <class TFormater> struct Writer;
127  };
128 
130 
131  // Either specialize per Type or define a custom Formater:
132 
134  template <class Tp>
135  std::string asXmlListElement( const Tp & val_r );
136  inline std::string asXmlListElement( const std::string & val_r ){ return val_r; }
137  inline std::string asXmlListElement( const char * val_r ) { return val_r; }
138 
140  template <class Tp>
141  std::string asListElement( const Tp & val_r );
142  inline std::string asListElement( const std::string & val_r ) { return val_r; }
143  inline std::string asListElement( const char * val_r ) { return val_r; }
144 
146  template <class Tp = void>
147  TableHeader asTableHeader();
148 
149  template <>
151  { return TableHeader(); }
152 
154  template <class Tp>
155  TableRow asTableRow( const Tp & val_r );
156 
161  struct XmlFormater
162  {
163  template <class Tp>
164  std::string xmlListElement( const Tp & val_r ) const//< XML representation of element
165  { return asXmlListElement( val_r ); }
166  };
167 
172  struct ListFormater : public XmlFormater
173  {
174  using NormalLayout = DefaultListLayout; //< ListLayout for NORMAL lists
175 
176  template <class Tp>
177  std::string listElement( const Tp & val_r ) const //< NORMAL representation of list element
178  { return asListElement( val_r ); }
179  };
180 
185  struct TableFormater : public XmlFormater
186  {
187  using NormalLayout = DefaultTableLayout; //< NORMAL layout as Table
188 
189  TableHeader header() const //< TableHeader for TableRow representation
190  { return asTableHeader<>(); }
191 
192  template <class Tp> //< Representation as TableRow
193  TableRow row( const Tp & val_r ) const
194  { return asTableRow( val_r ); }
195  };
196 
201 
202  template <class TFormater>
204  {
205  using NormalLayout = XmlListLayout; //< Layout as XML list
206 
207  template <class Tp>
208  std::string listElement( const Tp & val_r ) const //< use TFormater::asXmlListElement
209  { return _formater.xmlListElement( val_r ); }
210 
211  XmlFormaterAdaptor( const TFormater & formater_r )
212  : _formater( formater_r )
213  {}
214  private:
215  const TFormater & _formater;
216  };
217 
218 } // namespace out
220 
222 namespace out
223 {
227  // TODO: wrap singlelines; support for attributed text;
229  template <class TFormater>
230  struct ListLayout::Writer
231  {
232  NON_COPYABLE( Writer );
233 
234  Writer( std::ostream & str_r, const ListLayout & layout_r, const TFormater & formater_r )
235  : _str( str_r )
236  , _layout( layout_r )
237  , _formater( formater_r )
239  , _indent( _layout._indent, ' ' )
240  {}
241 
243  { if ( !_layout._singleline && _cpos ) _str << std::endl; }
244 
245  template <class Tp>
246  void operator<<( Tp && val_r ) const
247  {
248  const std::string & element( _formater.listElement( std::forward<Tp>(val_r) ) );
249 
250  if ( _layout._singleline )
251  {
252  if ( _layout._gaped )
253  _str << std::endl;
254  _str << _indent << element << std::endl;
255  }
256  else
257  {
258  if ( _cpos != 0 && ! fitsOnLine( 1/*' '*/ + element.size() ) )
259  endLine();
260 
261  if ( _cpos == 0 )
262  {
263  if ( !_indent.empty() )
265  }
266  else
267  printAndCount( " " );
268 
269  printAndCount( element );
270  }
271  }
272 
273  private:
274  bool fitsOnLine( unsigned size_r ) const
275  { return( !_layout._wrapline || _linewidth == out::termwidthUnlimited || _cpos + size_r <= _linewidth ); }
276 
277  void printAndCount( const std::string & element_r ) const
278  { _cpos += element_r.size(); _str << element_r; }
279 
280  void endLine() const
281  { _str << std::endl; _cpos = 0U; }
282 
283  private:
284  std::ostream & _str;
286  const TFormater & _formater;
287  const unsigned _linewidth;
288  const std::string _indent;
289  mutable unsigned _cpos = 0U;
290  };
291 
296  template <class TFormater>
297  struct TableLayout::Writer
298  {
299  NON_COPYABLE( Writer );
300 
301  Writer( std::ostream & str_r, const TableLayout & layout_r, const TFormater & formater_r )
302  : _str( str_r )
303  , _layout( layout_r )
304  , _formater( formater_r )
305  {}
306 
308  {
309  if ( !_t.empty() )
310  {
311  _t.setHeader( _formater.header() );
312  _str << _t;
313  }
314  }
315 
316  template <class Tp>
317  void operator<<( Tp && val_r ) const
318  { _t.add( _formater.row( std::forward<Tp>(val_r) ) ); }
319 
320  private:
321  std::ostream & _str;
323  const TFormater & _formater;
324  mutable Table _t;
325  };
326 
327 
329  template <class TContainer, class TFormater, class TLayout = typename TFormater::NormalLayout>
330  void writeContainer( std::ostream & str_r, const TContainer & container_r, const TFormater & formater_r, const TLayout & layout_r = TLayout() )
331  {
332  typedef typename TLayout::template Writer<TFormater> Writer;
333  Writer writer( str_r, layout_r, formater_r );
334  for ( auto && el : container_r )
335  writer << el;
336  }
337 
339  template <class TContainer, class TFormater>
340  void xmlWriteContainer( std::ostream & str_r, const TContainer & container_r, const TFormater & formater_r )
341  { writeContainer( str_r, container_r, out::XmlFormaterAdaptor<TFormater>(formater_r) ); }
342 
343 } // namespace out
345 
346 // Too simple on small terminals as esc-sequences may get truncated.
347 // A table like writer for attributed strings is desirable.
348 struct TermLine
349 {
351  {
352  SF_CRUSH = 1<<0, //< truncate lhs, then rhs
353  SF_SPLIT = 1<<1, //< split line across two
354  SF_EXPAND = 1<<2 //< expand short lines iff stdout is a tty
355  };
356  ZYPP_DECLARE_FLAGS( SplitFlags, SplitFlag );
357 
358  TermLine( SplitFlags flags_r, char exp_r ) : flagsHint( flags_r ), expHint( exp_r ) {}
359  TermLine( SplitFlags flags_r ) : flagsHint( flags_r ) {}
360  TermLine( char exp_r ) : expHint( exp_r ) {}
361  TermLine() {}
362 
363  SplitFlags flagsHint; //< flags to use if not passed to \ref get
364  zypp::DefaultIntegral<char,' '> expHint; //< expand char to use if not passed to \ref get
365  zypp::DefaultIntegral<int,-1> percentHint; //< draw progress indicator in expanded space if in [0,100]
366 
367  zypp::str::Str lhs; //< left side
368  zypp::str::Str rhs; //< right side
369 
370 
372  std::string get() const
373  { return std::string(lhs) + std::string(rhs); }
374 
378  std::string get( unsigned width_r, SplitFlags flags_r, char exp_r ) const;
380  std::string get( unsigned width_r, SplitFlags flags_r ) const
381  { return get( width_r, flags_r, expHint ); }
383  std::string get( unsigned width_r, char exp_r ) const
384  { return get( width_r, flagsHint, exp_r ); }
386  std::string get( unsigned width_r ) const
387  { return get( width_r, flagsHint, expHint ); }
388 };
389 ZYPP_DECLARE_OPERATORS_FOR_FLAGS( TermLine::SplitFlags );
390 
430 {
431 public:
433  typedef enum
434  {
435  QUIET = 0,
436  NORMAL = 1,
437  HIGH = 2,
439  DEBUG = 3
440  } Verbosity;
441 
443  enum TypeBit
444  {
445  TYPE_NORMAL = 0x01<<0,
446  TYPE_XML = 0x01<<1
447  };
449 
450  static constexpr Type TYPE_NONE = Type(0x00);
451  static constexpr Type TYPE_ALL = Type(0xff);
452 
453  using PromptId = unsigned;
454 
455 protected:
458  {}
459 
460 public:
461  virtual ~Out();
462 
463 protected:
467  struct ParentOut
468  {
469  ParentOut( Out & out_r ) : _out( out_r ) {}
470  Out & out() { return _out; }
471  private:
473  };
474 
475 public:
485  struct XmlNode : protected ParentOut
486  {
488 
490  XmlNode( Out & out_r, const std::string & name_r, const std::initializer_list<Attr> & attrs_r = {} )
491  : ParentOut( out_r )
492  {
493  if ( out().typeXML() && ! name_r.empty() )
494  { _node.reset( new zypp::xmlout::Node( std::cout, name_r, attrs_r ) ); }
495  }
496 
498  XmlNode( Out & out_r, const std::string & name_r, Attr attr_r )
499  : XmlNode( out_r, name_r, { std::move(attr_r) } )
500  {}
501 
503  XmlNode( XmlNode && rhs ) noexcept : ParentOut( rhs ) { _node.swap( rhs._node ); }
504 
505  private:
506  zypp::scoped_ptr<zypp::xmlout::Node> _node;
507  };
509 
515  void xmlNode( const std::string & name_r, const std::initializer_list<XmlNode::Attr> & attrs_r = {} )
516  { if ( typeXML() ) { zypp::xmlout::node( std::cout, name_r, attrs_r ); } }
518  void xmlNode( const std::string & name_r, XmlNode::Attr attr_r )
519  { xmlNode( name_r, { std::move(attr_r) } ); }
520 
524  struct TitleNode : public XmlNode
525  {
526  TitleNode( XmlNode && node_r, const std::string & title_r = "" )
527  : XmlNode( std::move(node_r) )
528  { if ( out().typeNORMAL() && ! title_r.empty() ) std::cout << title_r << std::endl; }
529  };
530 
531 private:
534  template <class TContainer, class TFormater>
535  void container( const std::string & nodeName_r, const std::string & title_r,
536  const TContainer & container_r, const TFormater & formater_r )
537  {
538  TitleNode guard( XmlNode( *this, nodeName_r, XmlNode::Attr( "size", zypp::str::numstring( container_r.size() ) ) ),
539  zypp::str::Format( title_r ) % container_r.size() );
540  switch ( type() )
541  {
542  case TYPE_NORMAL:
543  writeContainer( std::cout, container_r, formater_r );
544  break;
545  case TYPE_XML:
546  ztui::out::xmlWriteContainer( std::cout, container_r, formater_r );
547  break;
548  }
549  }
550 
551 public:
554  template <class TContainer, class TFormater = out::ListFormater>
555  void list( const std::string & nodeName_r, const std::string & title_r,
556  const TContainer & container_r, const TFormater & formater_r = TFormater() )
557  { container( nodeName_r, title_r, container_r, formater_r ); }
558 
561  template <class TContainer, class TFormater = out::TableFormater>
562  void table( const std::string & nodeName_r, const std::string & title_r,
563  const TContainer & container_r, const TFormater & formater_r = TFormater() )
564  { container( nodeName_r, title_r, container_r, formater_r ); }
565 
566 public:
568  void gap() { if ( type() == TYPE_NORMAL ) std::cout << std::endl; }
569 
570  void printRichText( std::string text, unsigned indent_r = 0U )
571  { ztui::printRichText( std::cout, std::move(text), indent_r, termwidth() ); }
572 
573 
575  struct ParFormat // placeholder until we need it
576  {};
577 
579  template <class Text>
580  void par( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
581  {
582  gap(); // if needed make it optional via ParFormat
583  zypp::str::Str formated;
584  mbs_write_wrapped( formated.stream(), zypp::str::asString(text_r), indent_r, defaultFormatWidth( 100 ) );
585  info( formated );
586  }
588  template <class Text>
589  void par( const Text & text_r, ParFormat format_r = ParFormat() )
590  { par( 0, text_r, format_r ); }
591 
592 
594  template <class TText, class Text>
595  void taggedPar( size_t indent_r, const TText & tag_r, const Text & text_r, ParFormat format_r = ParFormat() )
596  { par( indent_r, text::join( tag_r, text_r ), format_r ); }
598  template <class TText, class Text>
599  void taggedPar( const TText & tag_r, const Text & text_r, ParFormat format_r = ParFormat() )
600  { taggedPar( 0, tag_r, text_r, format_r ); }
601 
602 
604  template <class Text>
605  void notePar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
606  { taggedPar( indent_r, text::tagNote(), text_r, format_r ); }
608  template <class Text>
609  void notePar( const Text & text_r, ParFormat format_r = ParFormat() )
610  { notePar( 0, text_r, format_r ); }
611 
613  template <class Text>
614  void warningPar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
615  { taggedPar( indent_r, text::tagWarning(), text_r, format_r ); }
617  template <class Text>
618  void warningPar( const Text & text_r, ParFormat format_r = ParFormat() )
619  { warningPar( 0, text_r, format_r ); }
620 
622  template <class Text>
623  void errorPar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
624  { taggedPar( indent_r, text::tagError(), text_r, format_r ); }
626  template <class Text>
627  void errorPar( const Text & text_r, ParFormat format_r = ParFormat() )
628  { errorPar( 0, text_r, format_r ); }
629 
630 public:
644  virtual void info(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0;
646  void info( std::string msg, const std::string & msg2, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
647  { info( (msg+=msg2), verbosity, mask ); }
648 
650  virtual void infoLine(const TermLine & msg_r, Verbosity verbosity_r = NORMAL, Type mask_r = TYPE_ALL)
651  { info( msg_r.get(), verbosity_r, mask_r ); }
652 
653  struct Info : protected ParentOut
654  {
655  NON_COPYABLE( Info );
656 
657  Info( Out & out_r )
658  : ParentOut( out_r )
659  , _str( new std::ostringstream )
660  {}
661 
662  Info( Out::Info && rhs ) noexcept
663  : ParentOut( rhs )
664  , _str( std::move(rhs._str) )
665  {}
666 
668  { out().info( _str->str() ); }
669 
670  template<class Tp>
671  std::ostream & operator<<( const Tp & val )
672  { return (*_str) << val; /*return *this;*/ }
673 
674  operator std::ostream &()
675  { return *_str; }
676 
677  std::ostream & stream()
678  { return *_str; }
679 
680  private:
681  std::unique_ptr<std::ostringstream> _str; // work around missing move ctor
682  };
683 
684  Info info() { return Info( *this ); }
685 
686 
688  void infoLR( const std::string & lmsg, const std::string & rmsg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
689  {
691  outstr.lhs << lmsg;
692  outstr.rhs << ' ' << rmsg;
693  infoLine( outstr, verbosity, mask );
694  }
695 
697  void infoLRHint( const std::string & lmsg, const std::string & hint, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
698  {
700  outstr.lhs << lmsg;
701  outstr.rhs << " (" << hint << ')';
702  infoLine( outstr, verbosity, mask );
703  }
704 
705 
719  virtual void warning(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0;
720 
722  struct Error;
723 
732  virtual void error(const std::string & problem_desc, const std::string & hint = "") = 0;
733 
742  virtual void error(const zypp::Exception & e,
743  const std::string & problem_desc,
744  const std::string & hint = "") = 0;
745 
747 
750 
752  class ProgressBar;
753 
764  virtual void progressStart(const std::string & id,
765  const std::string & label,
766  bool is_tick = false) = 0;
767 
777  virtual void progress(const std::string & id,
778  const std::string & label,
779  int value = -1) = 0;
780 
791  virtual void progressEnd(const std::string & id,
792  const std::string & label,
793  const std::string & donetag,
794  bool error = false) = 0;
796  void progressEnd( const std::string & id, const std::string & label, ProgressEnd donetag );
798  void progressEnd( const std::string & id, const std::string & label, bool error = false )
801 
809  virtual void dwnldProgressStart(const zypp::Url & uri) = 0;
810 
818  virtual void dwnldProgress(const zypp::Url & uri,
819  int value = -1,
820  long rate = -1) = 0;
828  virtual void dwnldProgressEnd(const zypp::Url & uri,
829  long rate = -1,
830  zypp::TriBool error = false) = 0;
832 
842  virtual void searchResult( const Table & table_r );
843 
857  virtual void prompt(PromptId id,
858  const std::string & prompt,
859  const PromptOptions & poptions,
860  const std::string & startdesc = "") = 0;
861 
866  virtual void promptHelp(const PromptOptions & poptions) = 0;
867 
868 public:
870  Verbosity verbosity() const { return _verbosity; }
871 
874 
887 #define SCOPED_VERBOSITY( OUT, LEVEL ) const auto & raii __attribute__ ((__unused__))( (OUT).scopedVerbosity( LEVEL ))
888 
891  {
892  std::swap( _verbosity, verbosity_r );
893  return zypp::DtorReset( _verbosity, verbosity_r );
894  }
895 
897  virtual void setUseColors( bool yesno ) {}
898 
899 public:
901  TypeBit type() const { return _type; }
902 
904  bool type( TypeBit type_r ) const { return type() == type_r; }
906  bool typeNORMAL() const { return type( TYPE_NORMAL ); }
908  bool typeXML() const { return type( TYPE_XML ); }
909 
914  unsigned defaultFormatWidth( unsigned desired_r = 0 ) const
915  {
916  unsigned ret = termwidth();
917  if ( ret == out::termwidthUnlimited )
918  ret = desired_r ? desired_r : 150U;
919  else if ( desired_r < ret )
920  ret = desired_r;
921  return ret;
922  }
923 
925  virtual unsigned termwidth() const { return out::termwidthUnlimited; }
926 
927 protected:
928 
932  virtual bool mine(Type type) = 0;
933 
940  virtual bool progressFilter();
941 
945  virtual std::string zyppExceptionReport(const zypp::Exception & e);
946 
947 private:
949  const TypeBit _type;
950 };
951 
953 
989 {
990 public:
992  struct NoStartBar {};
994  static constexpr NoStartBar noStartBar = NoStartBar();
995 
996 public:
1001  ProgressBar( Out & out_r, NoStartBar, std::string progressId_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1002  : _out( out_r )
1003  , _progressId(std::move( progressId_r ))
1004  {
1005  if ( total_r )
1006  _labelPrefix = zypp::str::form( "(%*u/%u) ", numDigits( total_r ), current_r, total_r );
1007  else if ( current_r )
1008  _labelPrefix = zypp::str::form( "(%u) ", current_r );
1009  _progress.name( label_r );
1010  _progress.sendTo( Print( *this ) );
1011  }
1012 
1013  ProgressBar( Out & out_r,NoStartBar, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1014  : ProgressBar( out_r, noStartBar, "", label_r, current_r, total_r )
1015  {}
1016 
1021  ProgressBar( Out & out_r, const std::string & progressId_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1022  : ProgressBar( out_r, noStartBar, progressId_r, label_r, current_r, total_r )
1023  {
1024  // print the initial progress bar
1026  }
1027 
1028  ProgressBar( Out & out_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1029  : ProgressBar( out_r, "", label_r, current_r, total_r )
1030  {}
1031 
1037  {
1038  _progress.noSend(); // suppress ~ProgressData final report
1039  if ( not _donetag )
1042  }
1043 
1045  void print()
1047 
1049  void print( const std::string & label_r )
1050  { _progress.name( label_r ); print(); }
1051 
1054  { _donetag = donetag_r; }
1055 
1057  void error( bool error_r )
1058  { _donetag = error_r ? ProgressEnd::error : ProgressEnd::done; }
1059 
1061  void errorreset()
1062  { _donetag.reset(); }
1063 
1065  void error( const std::string & label_r )
1066  { _progress.name( label_r ); error( true ); }
1067 
1069  void error( const char * label_r )
1070  { _progress.name( label_r ); error( true ); }
1071 
1072 public:
1076  { return &_progress; }
1077 
1079  { return &_progress; }
1080 
1082  { return _progress; }
1083 
1085  { return _progress; }
1087 
1088 private:
1095  struct Print
1096  {
1097  Print( ProgressBar & bar_r ) : _bar( &bar_r ) {}
1098  bool operator()( const zypp::ProgressData & progress_r )
1099  { _bar->_out.progress( _bar->_progressId, _bar->outLabel( progress_r.name() ), progress_r.reportValue() ); return true; }
1100  private:
1102  };
1103 
1104  std::string outLabel( const std::string & msg_r ) const
1105  { return _labelPrefix.empty() ? msg_r : _labelPrefix + msg_r; }
1106 
1107  int numDigits( unsigned num_r ) const
1108  { int ret = 1; while ( num_r /= 10 ) ++ret; return ret; }
1109 
1110 private:
1112  std::optional<ProgressEnd> _donetag;
1114  std::string _progressId;
1115  std::string _labelPrefix;
1116 };
1118 
1152 {
1153  Error( int exitcode_r )
1154  : _exitcode( exitcode_r ) {}
1155 
1156  // basic: code msg hint
1157  Error( int exitcode_r, std::string msg_r, std::string hint_r = std::string() )
1158  : _exitcode( exitcode_r ), _msg( std::move(msg_r) ), _hint( std::move(hint_r) ) {}
1159 
1160  // code exception hint
1161  Error( int exitcode_r, const zypp::Exception & ex_r, std::string hint_r = std::string() )
1162  : _exitcode( exitcode_r ), _msg( combine( ex_r ) ), _hint( std::move(hint_r) ) {}
1163 
1164  // code (msg exception) hint
1165  Error( int exitcode_r, std::string msg_r, const zypp::Exception & ex_r, std::string hint_r = std::string() )
1166  : _exitcode( exitcode_r ), _msg( combine( std::move(msg_r), ex_r ) ), _hint( std::move(hint_r) ) {}
1167 
1173  int report( Application & app_r ) const;
1174 
1175  int _exitcode; //< ZYPPER_EXIT_OK indicates exitcode is already set.
1176  std::string _msg;
1177  std::string _hint;
1178 
1179 private:
1180  static std::string combine( std::string && msg_r, const zypp::Exception & ex_r );
1181  static std::string combine( const zypp::Exception & ex_r );
1182 };
1183 
1184 }
1185 
1186 #endif /*OUT_H_*/
zypp::ProgressData * operator->()
Definition: Out.h:1075
virtual void setUseColors(bool yesno)
Hint for a handler whether config would allow to use colors.
Definition: Out.h:897
Convenience class for progress output.
Definition: Out.h:988
std::ostream & node(std::ostream &out_r, const std::string &name_r, Node::Attr attr_r)
Definition: Xml.h:204
std::string asListElement(const Tp &val_r)
ProgressBar(Out &out_r, const std::string &progressId_r, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Ctor displays initial progress bar.
Definition: Out.h:1021
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition: Out.cc:117
const TypeBit _type
Definition: Out.h:949
void errorPar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:627
void infoLRHint(const std::string &lmsg, const std::string &hint, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)
Info message with R-adjusted "(hint)".
Definition: Out.h:697
zypp::ProgressData & operator*()
Definition: Out.h:1081
XmlNode(XmlNode &&rhs) noexcept
Move ctor.
Definition: Out.h:503
void mbs_write_wrapped(std::ostream &out, boost::string_ref text_r, size_t indent_r, size_t wrap_r, int indentFix_r=0)
Wrap and indent given text and write it to the output stream out.
Definition: text.h:631
boost::logic::tribool TriBool
3-state boolean logic (true, false and indeterminate).
Definition: String.h:30
Colored string if do_colors.
Definition: ansi.h:496
void taggedPar(size_t indent_r, const TText &tag_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph of text preceded by &#39;tag_r&#39; and a &#39; &#39;.
Definition: Out.h:595
#define _(MSG)
Definition: Gettext.h:37
void notePar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Note: &#39;.
Definition: Out.h:605
ProgressBar * _bar
Definition: Out.h:1101
Table & add(TableRow tr)
Definition: Table.cc:329
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: progressdata.h:229
std::string xmlListElement(const Tp &val_r) const
Definition: Out.h:164
Base class for producing common (for now) zypper output.
Definition: Out.h:429
bool type(TypeBit type_r) const
Test for a specific type.
Definition: Out.h:904
Error(int exitcode_r)
Definition: Out.h:1153
RAII writing a nodes start/end tag.
Definition: Xml.h:84
zypp::DtorReset scopedVerbosity(Verbosity verbosity_r)
Return RAII class for exception safe scoped verbosity change.
Definition: Out.h:890
unsigned _indent
amount of indent
Definition: Out.h:104
virtual void warning(const std::string &msg, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)=0
Show a warning.
Writer(std::ostream &str_r, const ListLayout &layout_r, const TFormater &formater_r)
Definition: Out.h:234
XmlNode with optional normal text headline (NL appended)
Definition: Out.h:524
bool _gaped
add extra NL before element (if singleline)
Definition: Out.h:103
virtual void dwnldProgress(const zypp::Url &uri, int value=-1, long rate=-1)=0
Reports download progress.
Out & out()
Definition: Out.h:470
void error(ProgressEnd donetag_r=ProgressEnd::error)
Explicitly indicate the error condition for the final progress bar.
Definition: Out.h:1053
Print(ProgressBar &bar_r)
Definition: Out.h:1097
static constexpr unsigned termwidthUnlimited
Definition: Out.h:80
const zypp::ProgressData & operator*() const
Definition: Out.h:1084
Error(int exitcode_r, std::string msg_r, std::string hint_r=std::string())
Definition: Out.h:1157
virtual void progressEnd(const std::string &id, const std::string &label, const std::string &donetag, bool error=false)=0
End of an operation with reported progress.
void xmlWriteContainer(std::ostream &str_r, const TContainer &container_r, const TFormater &formater_r)
Write XML formatted container to stream.
Definition: Out.h:340
bool fitsOnLine(unsigned size_r) const
Definition: Out.h:274
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Definition: String.h:139
Definition: Arch.h:363
virtual ~Out()
Definition: Out.cc:109
Example: PromptOptions popts; popts.setOptions(_("y/n/p"), 0 / * default reply * /); popts...
Definition: promptoptions.h:38
TableHeader asTableHeader< void >()
Definition: Out.h:150
XmlNode(Out &out_r, const std::string &name_r, const std::initializer_list< Attr > &attrs_r={})
Ctor taking nodename and attribute list.
Definition: Out.h:490
bool _singleline
one list element per line
Definition: Out.h:101
std::string _progressId
Definition: Out.h:1114
Verbosity verbosity() const
Get current verbosity.
Definition: Out.h:870
bool empty() const
Definition: Table.h:411
TableHeader header() const
Definition: Out.h:189
XmlFormaterAdaptor(const TFormater &formater_r)
Definition: Out.h:211
const TFormater & _formater
Definition: Out.h:286
Basic list layout.
Definition: Out.h:91
std::unique_ptr< std::ostringstream > _str
Definition: Out.h:681
Convenient building of std::string with boost::format.
Definition: String.h:252
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:37
TableRow asTableRow(const Tp &val_r)
More detailed description of the operations.
Definition: Out.h:438
Error(int exitcode_r, const zypp::Exception &ex_r, std::string hint_r=std::string())
Definition: Out.h:1161
Error(int exitcode_r, std::string msg_r, const zypp::Exception &ex_r, std::string hint_r=std::string())
Definition: Out.h:1165
Basic table layout.
Definition: Out.h:124
(Key, Value) string pair of XML node attributes
Definition: Xml.h:43
std::ostream & printRichText(std::ostream &str, std::string text, unsigned indent_r=0U, unsigned width_r=0U)
Print [Rich]Text optionally indented.
Definition: richtext.h:26
const std::ostream & stream() const
Definition: String.h:224
CCString< ColorContext::MSG_WARNING > MSG_WARNINGString
Definition: colors.h:81
ColorString tagError()
Definition: Out.h:55
XML representation of types in container [asXmlListElement].
Definition: Out.h:161
void par(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph of text, optionally indented, or without leading gap.
Definition: Out.h:580
zypp::str::Str lhs
Definition: Out.h:367
Convenience class Error reporting.
Definition: Out.h:1151
Assign a vaiable a certain value when going out of scope.
Definition: dtorreset.h:49
zypp::scoped_ptr< zypp::xmlout::Node > _node
Definition: Out.h:506
std::optional< ProgressEnd > _donetag
Definition: Out.h:1112
void setVerbosity(Verbosity verbosity)
Set current verbosity.
Definition: Out.h:873
bool operator()(const zypp::ProgressData &progress_r)
Definition: Out.h:1098
virtual void infoLine(const TermLine &msg_r, Verbosity verbosity_r=NORMAL, Type mask_r=TYPE_ALL)
info taking a TermLine
Definition: Out.h:650
bool endsOnWS(const std::string &str_r)
Whether the str_r ends with a WS.
Definition: Out.h:66
const TFormater & _formater
Definition: Out.h:323
zypp::ProgressData _progress
Definition: Out.h:1113
void error(const char *label_r)
Definition: Out.h:1069
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:211
detail::ListLayoutInit< true, true, false, 0U > DefaultListLayout
one element per line, no indent
Definition: Out.h:114
TermLine(SplitFlags flags_r)
Definition: Out.h:359
unsigned PromptId
Definition: Out.h:453
TableLayout DefaultTableLayout
Simple Table.
Definition: Out.h:129
static constexpr Type TYPE_ALL
Definition: Out.h:451
std::string _labelPrefix
Definition: Out.h:1115
void writeContainer(std::ostream &str_r, const TContainer &container_r, const TFormater &formater_r, const TLayout &layout_r=TLayout())
Write formatted container to stream.
Definition: Out.h:330
Table & setHeader(TableHeader tr)
Definition: Table.cc:335
Default output verbosity level.
Definition: Out.h:436
void xmlNode(const std::string &name_r, const std::initializer_list< XmlNode::Attr > &attrs_r={})
XML only: Write a leaf node without PCDATA.
Definition: Out.h:515
ZYPP_DECLARE_FLAGS(Type, TypeBit)
std::ostream & _str
Definition: Out.h:321
detail::ListLayoutInit< true, false, false, 0U > XmlListLayout
Definition: Out.h:113
ProgressBar(Out &out_r, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Definition: Out.h:1028
virtual unsigned termwidth() const
Width for formatted output [0==unlimited].
Definition: Out.h:925
Maintain [min,max] and counter (value) for progress counting.
Definition: progressdata.h:131
Out(TypeBit type, Verbosity verbosity=NORMAL)
Definition: Out.h:456
xml output
Definition: Out.h:446
virtual void dwnldProgressEnd(const zypp::Url &uri, long rate=-1, zypp::TriBool error=false)=0
Reports end of a download.
TableRow row(const Tp &val_r) const
Definition: Out.h:193
std::ostream & stream()
Definition: Out.h:677
Write out a Table according to the layout.
Definition: Out.h:126
zypp::DefaultIntegral< int,-1 > percentHint
Definition: Out.h:365
static constexpr NoStartBar noStartBar
Indicator argument for ctor not drawing an initial start bar.
Definition: Out.h:994
CCString< ColorContext::MSG_ERROR > MSG_ERRORString
Definition: colors.h:80
const std::string _indent
Definition: Out.h:288
const unsigned _linewidth
desired line width
Definition: Out.h:287
void noSend()
Set no ReceiverFnc.
Definition: progressdata.h:233
ProgressEnd
ProgressBars default end tags.
Definition: Out.h:46
TypeBit
Known output types implemented by derived classes.
Definition: Out.h:443
void print()
Immediately print the progress bar not waiting for a new trigger.
Definition: Out.h:1045
void notePar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:609
std::string get() const
Return plain line made of lhs + rhs.
Definition: Out.h:372
plain text output
Definition: Out.h:445
TermLine(SplitFlags flags_r, char exp_r)
Definition: Out.h:358
unsigned defaultTermwidth()
Definition: Out.cc:27
virtual void info(const std::string &msg, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)=0
Show an info message.
std::string numstring(char n, int w=0)
Definition: String.h:289
void info(std::string msg, const std::string &msg2, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)
Definition: Out.h:646
zypp::str::Str rhs
Definition: Out.h:368
void errorPar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Error: &#39;.
Definition: Out.h:623
TermLine()
Definition: Out.h:361
ListLayout(bool singleline_r, bool wrapline_r, bool gaped_r, unsigned indent_r)
Definition: Out.h:95
const char * qContinue()
Definition: Out.h:57
Indicator type for ctor not drawing an initial start bar.
Definition: Out.h:992
void print(const std::string &label_r)
Definition: Out.h:1049
Verbosity _verbosity
Definition: Out.h:948
Info(Out::Info &&rhs) noexcept
Definition: Out.h:662
virtual void progressStart(const std::string &id, const std::string &label, bool is_tick=false)=0
Start of an operation with reported progress.
Less common Paragraph formats.
Definition: Out.h:575
virtual bool progressFilter()
Determine whether to show progress.
Definition: Out.cc:112
std::ostream & operator<<(const Tp &val)
Definition: Out.h:671
Only important messages (no progress or status, only the result).
Definition: Out.h:435
void table(const std::string &nodeName_r, const std::string &title_r, const TContainer &container_r, const TFormater &formater_r=TFormater())
Write table from container creating a TitleNode with size="nnn" attribute and replacing optional %1% ...
Definition: Out.h:562
std::string asXmlListElement(const std::string &val_r)
Definition: Out.h:136
static constexpr Type TYPE_NONE
Definition: Out.h:450
bool typeXML() const
Definition: Out.h:908
Verbosity
Verbosity levels.
Definition: Out.h:433
ParentOut(Out &out_r)
Definition: Out.h:469
SplitFlags flagsHint
Definition: Out.h:363
std::string asListElement(const std::string &val_r)
Definition: Out.h:142
void par(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:589
void xmlNode(const std::string &name_r, XmlNode::Attr attr_r)
Definition: Out.h:518
unsigned defaultFormatWidth(unsigned desired_r=0) const
Terminal width or 150 if unlimited.
Definition: Out.h:914
ColorString tagNote()
Definition: Out.h:51
Base class for Exception.
Definition: Exception.h:146
std::string _hint
Definition: Out.h:1177
ProgressBar(Out &out_r, NoStartBar, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Definition: Out.h:1013
virtual void dwnldProgressStart(const zypp::Url &uri)=0
Reoprt start of a download.
ProgressBar(Out &out_r, NoStartBar, std::string progressId_r, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Ctor not displaying an initial progress bar.
Definition: Out.h:1001
const TableLayout & _layout
Definition: Out.h:322
void errorreset()
Reset any error condition.
Definition: Out.h:1061
virtual void progress(const std::string &id, const std::string &label, int value=-1)=0
Progress report for an on-going operation.
const ListLayout & _layout
Definition: Out.h:285
ZYPP_DECLARE_FLAGS(SplitFlags, SplitFlag)
std::string listElement(const Tp &val_r) const
Definition: Out.h:177
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(TermLine::SplitFlags)
XML only: RAII writing a XML nodes start/end tag.
Definition: Out.h:485
ProgressData::ReceiverFnc printing to a ProgressBar.
Definition: Out.h:1095
Convenience base class storing the back reference to Out.
Definition: Out.h:467
value_type reportValue() const
Definition: progressdata.h:322
const char * optBlankAfter(const std::string &str_r)
Definition: Out.h:74
void printAndCount(const std::string &element_r) const
Definition: Out.h:277
int _exitcode
Definition: Out.h:1175
std::string listElement(const Tp &val_r) const
Definition: Out.h:208
Write out a List according to the layout.
Definition: Out.h:93
void gap()
NORMAL: An empty line.
Definition: Out.h:568
void error(const std::string &label_r)
Definition: Out.h:1065
TitleNode(XmlNode &&node_r, const std::string &title_r="")
Definition: Out.h:526
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition: Out.cc:144
NodeAttr Attr
Definition: Xml.h:87
TypeBit type() const
Return the type of the instance.
Definition: Out.h:901
virtual void promptHelp(const PromptOptions &poptions)=0
Print help for prompt, if available.
virtual void searchResult(const Table &table_r)
Print out a search result.
Definition: Out.cc:122
const TFormater & _formater
Definition: Out.h:215
void error(bool error_r)
Definition: Out.h:1057
void infoLR(const std::string &lmsg, const std::string &rmsg, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)
Info message, 2 strings L/R-adjusted.
Definition: Out.h:688
std::string join(const Tltext &ltext, const Trtext &rtext, const char *sep=" ")
Simple join of two string types.
Definition: Out.h:62
bool _wrapline
fold lines longer than _linewidth
Definition: Out.h:102
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition: Out.cc:153
zypp::DefaultIntegral< char,' '> expHint
Definition: Out.h:364
TermLine(char exp_r)
Definition: Out.h:360
std::ostream & _str
Definition: Out.h:284
void progressEnd(const std::string &id, const std::string &label, bool error=false)
Definition: Out.h:798
std::string _msg
Definition: Out.h:1176
void operator<<(Tp &&val_r) const
Definition: Out.h:246
std::string asXmlListElement(const Tp &val_r)
void name(const std::string &name_r)
Set counter name.
Definition: progressdata.h:225
void endLine() const
Definition: Out.h:280
CCString< ColorContext::HIGHLIGHT > HIGHLIGHTString
Definition: colors.h:87
void printRichText(std::string text, unsigned indent_r=0U)
Definition: Out.h:570
int numDigits(unsigned num_r) const
Definition: Out.h:1107
void taggedPar(const TText &tag_r, const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:599
void operator<<(Tp &&val_r) const
Definition: Out.h:317
virtual bool mine(Type type)=0
Determine whether the output is intended for the particular type.
Info info()
Definition: Out.h:684
XmlNode(Out &out_r, const std::string &name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition: Out.h:498
~ProgressBar()
Dtor displays final progress bar.
Definition: Out.h:1036
std::string outLabel(const std::string &msg_r) const
Definition: Out.h:1104
Special list formater writing a Table [asTableHeader|asTableRow].
Definition: Out.h:185
bool reportPercent() const
Definition: progressdata.h:309
virtual void error(const std::string &problem_desc, const std::string &hint="")=0
Show an error message and an optional hint.
Info(Out &out_r)
Definition: Out.h:657
Default representation of types in Lists [asListElement].
Definition: Out.h:172
void warningPar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Warning: &#39;.
Definition: Out.h:614
virtual void prompt(PromptId id, const std::string &prompt, const PromptOptions &poptions, const std::string &startdesc="")=0
Prompt the user for a decision.
Url manipulation class.
Definition: Url.h:91
Writer(std::ostream &str_r, const TableLayout &layout_r, const TFormater &formater_r)
Definition: Out.h:301
void list(const std::string &nodeName_r, const std::string &title_r, const TContainer &container_r, const TFormater &formater_r=TFormater())
Write list from container creating a TitleNode with size="nnn" attribute and replacing optional %1% i...
Definition: Out.h:555
Class representing an application (appdata.xml)
Definition: Application.h:27
const zypp::ProgressData * operator->() const
Definition: Out.h:1078
void warningPar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:618
void container(const std::string &nodeName_r, const std::string &title_r, const TContainer &container_r, const TFormater &formater_r)
Write container creating a TitleNode with size="nnn" attribute and replacing optional %1% in title_r ...
Definition: Out.h:535
bool typeNORMAL() const
Definition: Out.h:906
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
ColorString tagWarning()
Definition: Out.h:53