libzypp  17.37.5
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/Url.h>
30 #include <zypp-core/TriBool.h>
31 #include <zypp-core/ui/ProgressData>
33 
34 #include <zypp-tui/utils/text.h>
35 #include <zypp-tui/utils/colors.h>
37 #include <zypp-tui/Table.h>
38 #include <zypp-tui/output/PromptOptions>
39 
40 namespace ztui {
41 
42 class Application;
43 
45 enum class ProgressEnd { done, attention, error };
46 
47 namespace text
48 {
49  ColorString tagNote();
50  ColorString tagWarning();
51  ColorString tagError();
52  const char * qContinue();
53 
55  template <class Tltext, class Trtext>
56  inline std::string join( const Tltext & ltext, const Trtext & rtext, const char * sep = " " )
57  { std::string ret( zypp::str::asString(ltext) ); ret += sep; ret += zypp::str::asString(rtext); return ret; }
58 
60  inline bool endsOnWS( const std::string & str_r )
61  {
62  bool ret = false;
63  if ( !str_r.empty() )
64  { int l = *str_r.rbegin(); if ( ::strchr( " \n\t", l ) ) ret = true; }
65  return ret;
66  }
67 
68  inline const char * optBlankAfter( const std::string & str_r )
69  { return( endsOnWS( str_r ) ? "" : " " ); }
70 }
71 
72 namespace out
73 {
74  static constexpr unsigned termwidthUnlimited = 0u;
75  unsigned defaultTermwidth(); // Zypper::instance().out().termwidth()
76 } // namespace out
77 
78 namespace out
79 {
85  struct ListLayout
86  {
87  template <class TFormater> struct Writer;
88 
89  ListLayout( bool singleline_r, bool wrapline_r, bool gaped_r, unsigned indent_r )
90  : _singleline( singleline_r )
91  , _wrapline( wrapline_r )
92  , _gaped( gaped_r )
93  , _indent( indent_r )
94  {}
95  bool _singleline;
96  bool _wrapline;
97  bool _gaped;
98  unsigned _indent;
99  };
100 
101  namespace detail
102  {
103  template <bool singleline_, bool wrapline_, bool gaped_, unsigned indent_>
104  struct ListLayoutInit : public ListLayout { ListLayoutInit() : ListLayout( singleline_, wrapline_, gaped_, indent_ ) {} };
105  }
106 
113 
118  struct TableLayout
119  {
120  template <class TFormater> struct Writer;
121  };
122 
124 
125  // Either specialize per Type or define a custom Formater:
126 
128  template <class Tp>
129  std::string asXmlListElement( const Tp & val_r );
130  inline std::string asXmlListElement( const std::string & val_r ){ return val_r; }
131  inline std::string asXmlListElement( const char * val_r ) { return val_r; }
132 
134  template <class Tp>
135  std::string asListElement( const Tp & val_r );
136  inline std::string asListElement( const std::string & val_r ) { return val_r; }
137  inline std::string asListElement( const char * val_r ) { return val_r; }
138 
140  template <class Tp = void>
141  TableHeader asTableHeader();
142 
143  template <>
145  { return TableHeader(); }
146 
148  template <class Tp>
149  TableRow asTableRow( const Tp & val_r );
150 
155  struct XmlFormater
156  {
157  template <class Tp>
158  std::string xmlListElement( const Tp & val_r ) const//< XML representation of element
159  { return asXmlListElement( val_r ); }
160  };
161 
166  struct ListFormater : public XmlFormater
167  {
168  using NormalLayout = DefaultListLayout; //< ListLayout for NORMAL lists
169 
170  template <class Tp>
171  std::string listElement( const Tp & val_r ) const //< NORMAL representation of list element
172  { return asListElement( val_r ); }
173  };
174 
179  struct TableFormater : public XmlFormater
180  {
181  using NormalLayout = DefaultTableLayout; //< NORMAL layout as Table
182 
183  TableHeader header() const //< TableHeader for TableRow representation
184  { return asTableHeader<>(); }
185 
186  template <class Tp> //< Representation as TableRow
187  TableRow row( const Tp & val_r ) const
188  { return asTableRow( val_r ); }
189  };
190 
195 
196  template <class TFormater>
198  {
199  using NormalLayout = XmlListLayout; //< Layout as XML list
200 
201  template <class Tp>
202  std::string listElement( const Tp & val_r ) const //< use TFormater::asXmlListElement
203  { return _formater.xmlListElement( val_r ); }
204 
205  XmlFormaterAdaptor( const TFormater & formater_r )
206  : _formater( formater_r )
207  {}
208  private:
209  const TFormater & _formater;
210  };
211 
212 } // namespace out
214 
216 namespace out
217 {
221  // TODO: wrap singlelines; support for attributed text;
223  template <class TFormater>
224  struct ListLayout::Writer
225  {
226  NON_COPYABLE( Writer );
227 
228  Writer( std::ostream & str_r, const ListLayout & layout_r, const TFormater & formater_r )
229  : _str( str_r )
230  , _layout( layout_r )
231  , _formater( formater_r )
233  , _indent( _layout._indent, ' ' )
234  {}
235 
237  { if ( !_layout._singleline && _cpos ) _str << std::endl; }
238 
239  template <class Tp>
240  void operator<<( Tp && val_r ) const
241  {
242  const std::string & element( _formater.listElement( std::forward<Tp>(val_r) ) );
243 
244  if ( _layout._singleline )
245  {
246  if ( _layout._gaped )
247  _str << std::endl;
248  _str << _indent << element << std::endl;
249  }
250  else
251  {
252  if ( _cpos != 0 && ! fitsOnLine( 1/*' '*/ + element.size() ) )
253  endLine();
254 
255  if ( _cpos == 0 )
256  {
257  if ( !_indent.empty() )
259  }
260  else
261  printAndCount( " " );
262 
263  printAndCount( element );
264  }
265  }
266 
267  private:
268  bool fitsOnLine( unsigned size_r ) const
269  { return( !_layout._wrapline || _linewidth == out::termwidthUnlimited || _cpos + size_r <= _linewidth ); }
270 
271  void printAndCount( const std::string & element_r ) const
272  { _cpos += element_r.size(); _str << element_r; }
273 
274  void endLine() const
275  { _str << std::endl; _cpos = 0U; }
276 
277  private:
278  std::ostream & _str;
280  const TFormater & _formater;
281  const unsigned _linewidth;
282  const std::string _indent;
283  mutable unsigned _cpos = 0U;
284  };
285 
290  template <class TFormater>
291  struct TableLayout::Writer
292  {
293  NON_COPYABLE( Writer );
294 
295  Writer( std::ostream & str_r, const TableLayout & layout_r, const TFormater & formater_r )
296  : _str( str_r )
297  , _layout( layout_r )
298  , _formater( formater_r )
299  {}
300 
302  {
303  if ( !_t.empty() )
304  {
305  _t.setHeader( _formater.header() );
306  _str << _t;
307  }
308  }
309 
310  template <class Tp>
311  void operator<<( Tp && val_r ) const
312  { _t.add( _formater.row( std::forward<Tp>(val_r) ) ); }
313 
314  private:
315  std::ostream & _str;
317  const TFormater & _formater;
318  mutable Table _t;
319  };
320 
321 
323  template <class TContainer, class TFormater, class TLayout = typename TFormater::NormalLayout>
324  void writeContainer( std::ostream & str_r, const TContainer & container_r, const TFormater & formater_r, const TLayout & layout_r = TLayout() )
325  {
326  typedef typename TLayout::template Writer<TFormater> Writer;
327  Writer writer( str_r, layout_r, formater_r );
328  for ( auto && el : container_r )
329  writer << el;
330  }
331 
333  template <class TContainer, class TFormater>
334  void xmlWriteContainer( std::ostream & str_r, const TContainer & container_r, const TFormater & formater_r )
335  { writeContainer( str_r, container_r, out::XmlFormaterAdaptor<TFormater>(formater_r) ); }
336 
337 } // namespace out
339 
340 // Too simple on small terminals as esc-sequences may get truncated.
341 // A table like writer for attributed strings is desirable.
342 struct TermLine
343 {
345  {
346  SF_CRUSH = 1<<0, //< truncate lhs, then rhs
347  SF_SPLIT = 1<<1, //< split line across two
348  SF_EXPAND = 1<<2 //< expand short lines iff stdout is a tty
349  };
350  ZYPP_DECLARE_FLAGS( SplitFlags, SplitFlag );
351 
352  TermLine( SplitFlags flags_r, char exp_r ) : flagsHint( flags_r ), expHint( exp_r ) {}
353  TermLine( SplitFlags flags_r ) : flagsHint( flags_r ) {}
354  TermLine( char exp_r ) : expHint( exp_r ) {}
355  TermLine() {}
356 
357  SplitFlags flagsHint; //< flags to use if not passed to \ref get
358  zypp::DefaultIntegral<char,' '> expHint; //< expand char to use if not passed to \ref get
359  zypp::DefaultIntegral<int,-1> percentHint; //< draw progress indicator in expanded space if in [0,100]
360 
361  zypp::str::Str lhs; //< left side
362  zypp::str::Str rhs; //< right side
363 
364 
366  std::string get() const
367  { return std::string(lhs) + std::string(rhs); }
368 
372  std::string get( unsigned width_r, SplitFlags flags_r, char exp_r ) const;
374  std::string get( unsigned width_r, SplitFlags flags_r ) const
375  { return get( width_r, flags_r, expHint ); }
377  std::string get( unsigned width_r, char exp_r ) const
378  { return get( width_r, flagsHint, exp_r ); }
380  std::string get( unsigned width_r ) const
381  { return get( width_r, flagsHint, expHint ); }
382 };
383 ZYPP_DECLARE_OPERATORS_FOR_FLAGS( TermLine::SplitFlags );
384 
424 {
425 public:
427  typedef enum
428  {
429  QUIET = 0,
430  NORMAL = 1,
431  HIGH = 2,
433  DEBUG = 3
434  } Verbosity;
435 
437  enum TypeBit
438  {
439  TYPE_NORMAL = 0x01<<0,
440  TYPE_XML = 0x01<<1
441  };
443 
444  static constexpr Type TYPE_NONE = Type(0x00);
445  static constexpr Type TYPE_ALL = Type(0xff);
446 
447  using PromptId = unsigned;
448 
449 protected:
452  {}
453 
454 public:
455  virtual ~Out();
456 
457 protected:
461  struct ParentOut
462  {
463  ParentOut( Out & out_r ) : _out( out_r ) {}
464  Out & out() { return _out; }
465  private:
467  };
468 
469 public:
479  struct XmlNode : protected ParentOut
480  {
482 
484  XmlNode( Out & out_r, const std::string & name_r, const std::initializer_list<Attr> & attrs_r = {} )
485  : ParentOut( out_r )
486  {
487  if ( out().typeXML() && ! name_r.empty() )
488  { _node.reset( new zypp::xmlout::Node( std::cout, name_r, attrs_r ) ); }
489  }
490 
492  XmlNode( Out & out_r, const std::string & name_r, Attr attr_r )
493  : XmlNode( out_r, name_r, { std::move(attr_r) } )
494  {}
495 
497  XmlNode( XmlNode && rhs ) noexcept : ParentOut( rhs ) { _node.swap( rhs._node ); }
498 
499  private:
500  zypp::scoped_ptr<zypp::xmlout::Node> _node;
501  };
503 
509  void xmlNode( const std::string & name_r, const std::initializer_list<XmlNode::Attr> & attrs_r = {} )
510  { if ( typeXML() ) { zypp::xmlout::node( std::cout, name_r, attrs_r ); } }
512  void xmlNode( const std::string & name_r, XmlNode::Attr attr_r )
513  { xmlNode( name_r, { std::move(attr_r) } ); }
514 
518  struct TitleNode : public XmlNode
519  {
520  TitleNode( XmlNode && node_r, const std::string & title_r = "" )
521  : XmlNode( std::move(node_r) )
522  { if ( out().typeNORMAL() && ! title_r.empty() ) std::cout << title_r << std::endl; }
523  };
524 
525 private:
528  template <class TContainer, class TFormater>
529  void container( const std::string & nodeName_r, const std::string & title_r,
530  const TContainer & container_r, const TFormater & formater_r )
531  {
532  TitleNode guard( XmlNode( *this, nodeName_r, XmlNode::Attr( "size", zypp::str::numstring( container_r.size() ) ) ),
533  zypp::str::Format( title_r ) % container_r.size() );
534  switch ( type() )
535  {
536  case TYPE_NORMAL:
537  writeContainer( std::cout, container_r, formater_r );
538  break;
539  case TYPE_XML:
540  ztui::out::xmlWriteContainer( std::cout, container_r, formater_r );
541  break;
542  }
543  }
544 
545 public:
548  template <class TContainer, class TFormater = out::ListFormater>
549  void list( const std::string & nodeName_r, const std::string & title_r,
550  const TContainer & container_r, const TFormater & formater_r = TFormater() )
551  { container( nodeName_r, title_r, container_r, formater_r ); }
552 
555  template <class TContainer, class TFormater = out::TableFormater>
556  void table( const std::string & nodeName_r, const std::string & title_r,
557  const TContainer & container_r, const TFormater & formater_r = TFormater() )
558  { container( nodeName_r, title_r, container_r, formater_r ); }
559 
560 public:
562  void gap() { if ( type() == TYPE_NORMAL ) std::cout << std::endl; }
563 
564  void printRichText( std::string text, unsigned indent_r = 0U )
565  { ztui::printRichText( std::cout, std::move(text), indent_r, termwidth() ); }
566 
567 
569  struct ParFormat // placeholder until we need it
570  {};
571 
573  template <class Text>
574  void par( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
575  {
576  gap(); // if needed make it optional via ParFormat
577  zypp::str::Str formated;
578  mbs_write_wrapped( formated.stream(), zypp::str::asString(text_r), indent_r, defaultFormatWidth( 100 ) );
579  info( formated );
580  }
582  template <class Text>
583  void par( const Text & text_r, ParFormat format_r = ParFormat() )
584  { par( 0, text_r, format_r ); }
585 
586 
588  template <class TText, class Text>
589  void taggedPar( size_t indent_r, const TText & tag_r, const Text & text_r, ParFormat format_r = ParFormat() )
590  { par( indent_r, text::join( tag_r, text_r ), format_r ); }
592  template <class TText, class Text>
593  void taggedPar( const TText & tag_r, const Text & text_r, ParFormat format_r = ParFormat() )
594  { taggedPar( 0, tag_r, text_r, format_r ); }
595 
596 
598  template <class Text>
599  void notePar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
600  { taggedPar( indent_r, text::tagNote(), text_r, format_r ); }
602  template <class Text>
603  void notePar( const Text & text_r, ParFormat format_r = ParFormat() )
604  { notePar( 0, text_r, format_r ); }
605 
607  template <class Text>
608  void warningPar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
609  { taggedPar( indent_r, text::tagWarning(), text_r, format_r ); }
611  template <class Text>
612  void warningPar( const Text & text_r, ParFormat format_r = ParFormat() )
613  { warningPar( 0, text_r, format_r ); }
614 
616  template <class Text>
617  void errorPar( size_t indent_r, const Text & text_r, ParFormat format_r = ParFormat() )
618  { taggedPar( indent_r, text::tagError(), text_r, format_r ); }
620  template <class Text>
621  void errorPar( const Text & text_r, ParFormat format_r = ParFormat() )
622  { errorPar( 0, text_r, format_r ); }
623 
624 public:
638  virtual void info(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0;
640  void info( std::string msg, const std::string & msg2, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
641  { info( (msg+=msg2), verbosity, mask ); }
642 
644  virtual void infoLine(const TermLine & msg_r, Verbosity verbosity_r = NORMAL, Type mask_r = TYPE_ALL)
645  { info( msg_r.get(), verbosity_r, mask_r ); }
646 
647  struct Info : protected ParentOut
648  {
649  NON_COPYABLE( Info );
650 
651  Info( Out & out_r )
652  : ParentOut( out_r )
653  , _str( new std::ostringstream )
654  {}
655 
656  Info( Out::Info && rhs ) noexcept
657  : ParentOut( rhs )
658  , _str( std::move(rhs._str) )
659  {}
660 
662  { out().info( _str->str() ); }
663 
664  template<class Tp>
665  std::ostream & operator<<( const Tp & val )
666  { return (*_str) << val; /*return *this;*/ }
667 
668  operator std::ostream &()
669  { return *_str; }
670 
671  std::ostream & stream()
672  { return *_str; }
673 
674  private:
675  std::unique_ptr<std::ostringstream> _str; // work around missing move ctor
676  };
677 
678  Info info() { return Info( *this ); }
679 
680 
682  void infoLR( const std::string & lmsg, const std::string & rmsg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
683  {
685  outstr.lhs << lmsg;
686  outstr.rhs << ' ' << rmsg;
687  infoLine( outstr, verbosity, mask );
688  }
689 
691  void infoLRHint( const std::string & lmsg, const std::string & hint, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL )
692  {
694  outstr.lhs << lmsg;
695  outstr.rhs << " (" << hint << ')';
696  infoLine( outstr, verbosity, mask );
697  }
698 
699 
713  virtual void warning(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0;
714 
716  struct Error;
717 
726  virtual void error(const std::string & problem_desc, const std::string & hint = "") = 0;
727 
736  virtual void error(const zypp::Exception & e,
737  const std::string & problem_desc,
738  const std::string & hint = "") = 0;
739 
741 
744 
746  class ProgressBar;
747 
758  virtual void progressStart(const std::string & id,
759  const std::string & label,
760  bool is_tick = false) = 0;
761 
771  virtual void progress(const std::string & id,
772  const std::string & label,
773  int value = -1) = 0;
774 
785  virtual void progressEnd(const std::string & id,
786  const std::string & label,
787  const std::string & donetag,
788  bool error = false) = 0;
790  void progressEnd( const std::string & id, const std::string & label, ProgressEnd donetag );
792  void progressEnd( const std::string & id, const std::string & label, bool error = false )
795 
803  virtual void dwnldProgressStart(const zypp::Url & uri) = 0;
804 
812  virtual void dwnldProgress(const zypp::Url & uri,
813  int value = -1,
814  long rate = -1) = 0;
822  virtual void dwnldProgressEnd(const zypp::Url & uri,
823  long rate = -1,
824  zypp::TriBool error = false) = 0;
826 
836  virtual void searchResult( const Table & table_r );
837 
851  virtual void prompt(PromptId id,
852  const std::string & prompt,
853  const PromptOptions & poptions,
854  const std::string & startdesc = "") = 0;
855 
860  virtual void promptHelp(const PromptOptions & poptions) = 0;
861 
862 public:
864  Verbosity verbosity() const { return _verbosity; }
865 
868 
881 #define SCOPED_VERBOSITY( OUT, LEVEL ) const auto & raii __attribute__ ((__unused__))( (OUT).scopedVerbosity( LEVEL ))
882 
885  {
886  std::swap( _verbosity, verbosity_r );
887  return zypp::DtorReset( _verbosity, verbosity_r );
888  }
889 
891  virtual void setUseColors( bool yesno ) {}
892 
893 public:
895  TypeBit type() const { return _type; }
896 
898  bool type( TypeBit type_r ) const { return type() == type_r; }
900  bool typeNORMAL() const { return type( TYPE_NORMAL ); }
902  bool typeXML() const { return type( TYPE_XML ); }
903 
908  unsigned defaultFormatWidth( unsigned desired_r = 0 ) const
909  {
910  unsigned ret = termwidth();
911  if ( ret == out::termwidthUnlimited )
912  ret = desired_r ? desired_r : 150U;
913  else if ( desired_r < ret )
914  ret = desired_r;
915  return ret;
916  }
917 
919  virtual unsigned termwidth() const { return out::termwidthUnlimited; }
920 
921 protected:
922 
926  virtual bool mine(Type type) = 0;
927 
934  virtual bool progressFilter();
935 
939  virtual std::string zyppExceptionReport(const zypp::Exception & e);
940 
941 private:
943  const TypeBit _type;
944 };
945 
947 
983 {
984 public:
986  struct NoStartBar {};
988  static constexpr NoStartBar noStartBar = NoStartBar();
989 
990 public:
995  ProgressBar( Out & out_r, NoStartBar, std::string progressId_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
996  : _out( out_r )
997  , _progressId(std::move( progressId_r ))
998  {
999  if ( total_r )
1000  _labelPrefix = zypp::str::form( "(%*u/%u) ", numDigits( total_r ), current_r, total_r );
1001  else if ( current_r )
1002  _labelPrefix = zypp::str::form( "(%u) ", current_r );
1003  _progress.name( label_r );
1004  _progress.sendTo( Print( *this ) );
1005  }
1006 
1007  ProgressBar( Out & out_r,NoStartBar, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1008  : ProgressBar( out_r, noStartBar, "", label_r, current_r, total_r )
1009  {}
1010 
1015  ProgressBar( Out & out_r, const std::string & progressId_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1016  : ProgressBar( out_r, noStartBar, progressId_r, label_r, current_r, total_r )
1017  {
1018  // print the initial progress bar
1020  }
1021 
1022  ProgressBar( Out & out_r, const std::string & label_r, unsigned current_r = 0, unsigned total_r = 0 )
1023  : ProgressBar( out_r, "", label_r, current_r, total_r )
1024  {}
1025 
1031  {
1032  _progress.noSend(); // suppress ~ProgressData final report
1033  if ( not _donetag )
1036  }
1037 
1039  void print()
1041 
1043  void print( const std::string & label_r )
1044  { _progress.name( label_r ); print(); }
1045 
1048  { _donetag = donetag_r; }
1049 
1051  void error( bool error_r )
1052  { _donetag = error_r ? ProgressEnd::error : ProgressEnd::done; }
1053 
1055  void errorreset()
1056  { _donetag.reset(); }
1057 
1059  void error( const std::string & label_r )
1060  { _progress.name( label_r ); error( true ); }
1061 
1063  void error( const char * label_r )
1064  { _progress.name( label_r ); error( true ); }
1065 
1066 public:
1070  { return &_progress; }
1071 
1073  { return &_progress; }
1074 
1076  { return _progress; }
1077 
1079  { return _progress; }
1081 
1082 private:
1089  struct Print
1090  {
1091  Print( ProgressBar & bar_r ) : _bar( &bar_r ) {}
1092  bool operator()( const zypp::ProgressData & progress_r )
1093  { _bar->_out.progress( _bar->_progressId, _bar->outLabel( progress_r.name() ), progress_r.reportValue() ); return true; }
1094  private:
1096  };
1097 
1098  std::string outLabel( const std::string & msg_r ) const
1099  { return _labelPrefix.empty() ? msg_r : _labelPrefix + msg_r; }
1100 
1101  int numDigits( unsigned num_r ) const
1102  { int ret = 1; while ( num_r /= 10 ) ++ret; return ret; }
1103 
1104 private:
1106  std::optional<ProgressEnd> _donetag;
1108  std::string _progressId;
1109  std::string _labelPrefix;
1110 };
1112 
1146 {
1147  Error( int exitcode_r )
1148  : _exitcode( exitcode_r ) {}
1149 
1150  // basic: code msg hint
1151  Error( int exitcode_r, std::string msg_r, std::string hint_r = std::string() )
1152  : _exitcode( exitcode_r ), _msg( std::move(msg_r) ), _hint( std::move(hint_r) ) {}
1153 
1154  // code exception hint
1155  Error( int exitcode_r, const zypp::Exception & ex_r, std::string hint_r = std::string() )
1156  : _exitcode( exitcode_r ), _msg( combine( ex_r ) ), _hint( std::move(hint_r) ) {}
1157 
1158  // code (msg exception) hint
1159  Error( int exitcode_r, std::string msg_r, const zypp::Exception & ex_r, std::string hint_r = std::string() )
1160  : _exitcode( exitcode_r ), _msg( combine( std::move(msg_r), ex_r ) ), _hint( std::move(hint_r) ) {}
1161 
1167  int report( Application & app_r ) const;
1168 
1169  int _exitcode; //< ZYPPER_EXIT_OK indicates exitcode is already set.
1170  std::string _msg;
1171  std::string _hint;
1172 
1173 private:
1174  static std::string combine( std::string && msg_r, const zypp::Exception & ex_r );
1175  static std::string combine( const zypp::Exception & ex_r );
1176 };
1177 
1178 }
1179 
1180 #endif /*OUT_H_*/
zypp::ProgressData * operator->()
Definition: Out.h:1069
virtual void setUseColors(bool yesno)
Hint for a handler whether config would allow to use colors.
Definition: Out.h:891
Convenience class for progress output.
Definition: Out.h:982
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:1015
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition: Out.cc:128
const TypeBit _type
Definition: Out.h:943
void errorPar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:621
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:691
zypp::ProgressData & operator*()
Definition: Out.h:1075
XmlNode(XmlNode &&rhs) noexcept
Move ctor.
Definition: Out.h:497
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:31
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:589
void notePar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Note: &#39;.
Definition: Out.h:599
ProgressBar * _bar
Definition: Out.h:1095
Table & add(TableRow tr)
Definition: Table.cc:332
void sendTo(const ReceiverFnc &fnc_r)
Set ReceiverFnc.
Definition: progressdata.h:229
std::string xmlListElement(const Tp &val_r) const
Definition: Out.h:158
Base class for producing common (for now) zypper output.
Definition: Out.h:423
bool type(TypeBit type_r) const
Test for a specific type.
Definition: Out.h:898
Error(int exitcode_r)
Definition: Out.h:1147
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:884
unsigned _indent
amount of indent
Definition: Out.h:98
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:228
XmlNode with optional normal text headline (NL appended)
Definition: Out.h:518
bool _gaped
add extra NL before element (if singleline)
Definition: Out.h:97
virtual void dwnldProgress(const zypp::Url &uri, int value=-1, long rate=-1)=0
Reports download progress.
Out & out()
Definition: Out.h:464
void error(ProgressEnd donetag_r=ProgressEnd::error)
Explicitly indicate the error condition for the final progress bar.
Definition: Out.h:1047
Print(ProgressBar &bar_r)
Definition: Out.h:1091
static constexpr unsigned termwidthUnlimited
Definition: Out.h:74
const zypp::ProgressData & operator*() const
Definition: Out.h:1078
Error(int exitcode_r, std::string msg_r, std::string hint_r=std::string())
Definition: Out.h:1151
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:334
bool fitsOnLine(unsigned size_r) const
Definition: Out.h:268
const std::string & asString(const std::string &t)
Global asString() that works with std::string too.
Definition: String.h:140
Definition: Arch.h:363
virtual ~Out()
Definition: Out.cc:120
Example: PromptOptions popts; popts.setOptions(_("y/n/p"), 0 / * default reply * /); popts...
Definition: promptoptions.h:38
TableHeader asTableHeader< void >()
Definition: Out.h:144
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:484
bool _singleline
one list element per line
Definition: Out.h:95
std::string _progressId
Definition: Out.h:1108
Verbosity verbosity() const
Get current verbosity.
Definition: Out.h:864
bool empty() const
Definition: Table.h:410
TableHeader header() const
Definition: Out.h:183
XmlFormaterAdaptor(const TFormater &formater_r)
Definition: Out.h:205
const TFormater & _formater
Definition: Out.h:280
Basic list layout.
Definition: Out.h:85
std::unique_ptr< std::ostringstream > _str
Definition: Out.h:675
Convenient building of std::string with boost::format.
Definition: String.h:253
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition: String.cc:39
TableRow asTableRow(const Tp &val_r)
More detailed description of the operations.
Definition: Out.h:432
Error(int exitcode_r, const zypp::Exception &ex_r, std::string hint_r=std::string())
Definition: Out.h:1155
Error(int exitcode_r, std::string msg_r, const zypp::Exception &ex_r, std::string hint_r=std::string())
Definition: Out.h:1159
Basic table layout.
Definition: Out.h:118
(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:225
ColorString tagError()
translated "Error:" error color
Definition: Out.cc:30
XML representation of types in container [asXmlListElement].
Definition: Out.h:155
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:574
zypp::str::Str lhs
Definition: Out.h:361
Convenience class Error reporting.
Definition: Out.h:1145
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:500
std::optional< ProgressEnd > _donetag
Definition: Out.h:1106
void setVerbosity(Verbosity verbosity)
Set current verbosity.
Definition: Out.h:867
bool operator()(const zypp::ProgressData &progress_r)
Definition: Out.h:1092
virtual void infoLine(const TermLine &msg_r, Verbosity verbosity_r=NORMAL, Type mask_r=TYPE_ALL)
info taking a TermLine
Definition: Out.h:644
bool endsOnWS(const std::string &str_r)
Whether the str_r ends with a WS.
Definition: Out.h:60
const TFormater & _formater
Definition: Out.h:317
zypp::ProgressData _progress
Definition: Out.h:1107
void error(const char *label_r)
Definition: Out.h:1063
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:212
detail::ListLayoutInit< true, true, false, 0U > DefaultListLayout
one element per line, no indent
Definition: Out.h:108
TermLine(SplitFlags flags_r)
Definition: Out.h:353
unsigned PromptId
Definition: Out.h:447
TableLayout DefaultTableLayout
Simple Table.
Definition: Out.h:123
static constexpr Type TYPE_ALL
Definition: Out.h:445
std::string _labelPrefix
Definition: Out.h:1109
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:324
Table & setHeader(TableHeader tr)
Definition: Table.cc:338
Default output verbosity level.
Definition: Out.h:430
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:509
ZYPP_DECLARE_FLAGS(Type, TypeBit)
std::ostream & _str
Definition: Out.h:315
detail::ListLayoutInit< true, false, false, 0U > XmlListLayout
Definition: Out.h:107
ProgressBar(Out &out_r, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Definition: Out.h:1022
virtual unsigned termwidth() const
Width for formatted output [0==unlimited].
Definition: Out.h:919
Maintain [min,max] and counter (value) for progress counting.
Definition: progressdata.h:131
Out(TypeBit type, Verbosity verbosity=NORMAL)
Definition: Out.h:450
xml output
Definition: Out.h:440
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:187
std::ostream & stream()
Definition: Out.h:671
Write out a Table according to the layout.
Definition: Out.h:120
zypp::DefaultIntegral< int,-1 > percentHint
Definition: Out.h:359
static constexpr NoStartBar noStartBar
Indicator argument for ctor not drawing an initial start bar.
Definition: Out.h:988
const std::string _indent
Definition: Out.h:282
const unsigned _linewidth
desired line width
Definition: Out.h:281
void noSend()
Set no ReceiverFnc.
Definition: progressdata.h:233
ProgressEnd
ProgressBars default end tags.
Definition: Out.h:45
TypeBit
Known output types implemented by derived classes.
Definition: Out.h:437
void print()
Immediately print the progress bar not waiting for a new trigger.
Definition: Out.h:1039
void notePar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:603
const char * qContinue()
translated "Continue?"
Definition: Out.cc:32
std::string get() const
Return plain line made of lhs + rhs.
Definition: Out.h:366
plain text output
Definition: Out.h:439
TermLine(SplitFlags flags_r, char exp_r)
Definition: Out.h:352
unsigned defaultTermwidth()
Definition: Out.cc:38
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:290
void info(std::string msg, const std::string &msg2, Verbosity verbosity=NORMAL, Type mask=TYPE_ALL)
Definition: Out.h:640
zypp::str::Str rhs
Definition: Out.h:362
void errorPar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Error: &#39;.
Definition: Out.h:617
TermLine()
Definition: Out.h:355
ListLayout(bool singleline_r, bool wrapline_r, bool gaped_r, unsigned indent_r)
Definition: Out.h:89
Indicator type for ctor not drawing an initial start bar.
Definition: Out.h:986
void print(const std::string &label_r)
Definition: Out.h:1043
Verbosity _verbosity
Definition: Out.h:942
Info(Out::Info &&rhs) noexcept
Definition: Out.h:656
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:569
virtual bool progressFilter()
Determine whether to show progress.
Definition: Out.cc:123
std::ostream & operator<<(const Tp &val)
Definition: Out.h:665
Only important messages (no progress or status, only the result).
Definition: Out.h:429
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:556
std::string asXmlListElement(const std::string &val_r)
Definition: Out.h:130
static constexpr Type TYPE_NONE
Definition: Out.h:444
bool typeXML() const
Definition: Out.h:902
Verbosity
Verbosity levels.
Definition: Out.h:427
ParentOut(Out &out_r)
Definition: Out.h:463
SplitFlags flagsHint
Definition: Out.h:357
std::string asListElement(const std::string &val_r)
Definition: Out.h:136
void par(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:583
void xmlNode(const std::string &name_r, XmlNode::Attr attr_r)
Definition: Out.h:512
unsigned defaultFormatWidth(unsigned desired_r=0) const
Terminal width or 150 if unlimited.
Definition: Out.h:908
ColorString tagNote()
translated "Note:" highlighted
Definition: Out.cc:26
Base class for Exception.
Definition: Exception.h:152
std::string _hint
Definition: Out.h:1171
ProgressBar(Out &out_r, NoStartBar, const std::string &label_r, unsigned current_r=0, unsigned total_r=0)
Definition: Out.h:1007
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:995
const TableLayout & _layout
Definition: Out.h:316
void errorreset()
Reset any error condition.
Definition: Out.h:1055
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:279
ZYPP_DECLARE_FLAGS(SplitFlags, SplitFlag)
std::string listElement(const Tp &val_r) const
Definition: Out.h:171
ZYPP_DECLARE_OPERATORS_FOR_FLAGS(TermLine::SplitFlags)
XML only: RAII writing a XML nodes start/end tag.
Definition: Out.h:479
ProgressData::ReceiverFnc printing to a ProgressBar.
Definition: Out.h:1089
Convenience base class storing the back reference to Out.
Definition: Out.h:461
value_type reportValue() const
Definition: progressdata.h:322
const char * optBlankAfter(const std::string &str_r)
Definition: Out.h:68
void printAndCount(const std::string &element_r) const
Definition: Out.h:271
int _exitcode
Definition: Out.h:1169
std::string listElement(const Tp &val_r) const
Definition: Out.h:202
Write out a List according to the layout.
Definition: Out.h:87
void gap()
NORMAL: An empty line.
Definition: Out.h:562
void error(const std::string &label_r)
Definition: Out.h:1059
TitleNode(XmlNode &&node_r, const std::string &title_r="")
Definition: Out.h:520
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition: Out.cc:155
NodeAttr Attr
Definition: Xml.h:87
TypeBit type() const
Return the type of the instance.
Definition: Out.h:895
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:133
const TFormater & _formater
Definition: Out.h:209
void error(bool error_r)
Definition: Out.h:1051
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:682
std::string join(const Tltext &ltext, const Trtext &rtext, const char *sep=" ")
Simple join of two string types.
Definition: Out.h:56
bool _wrapline
fold lines longer than _linewidth
Definition: Out.h:96
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition: Out.cc:164
zypp::DefaultIntegral< char,' '> expHint
Definition: Out.h:358
TermLine(char exp_r)
Definition: Out.h:354
std::ostream & _str
Definition: Out.h:278
void progressEnd(const std::string &id, const std::string &label, bool error=false)
Definition: Out.h:792
std::string _msg
Definition: Out.h:1170
void operator<<(Tp &&val_r) const
Definition: Out.h:240
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:274
void printRichText(std::string text, unsigned indent_r=0U)
Definition: Out.h:564
int numDigits(unsigned num_r) const
Definition: Out.h:1101
void taggedPar(const TText &tag_r, const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:593
void operator<<(Tp &&val_r) const
Definition: Out.h:311
virtual bool mine(Type type)=0
Determine whether the output is intended for the particular type.
Info info()
Definition: Out.h:678
XmlNode(Out &out_r, const std::string &name_r, Attr attr_r)
Convenience ctor for one attribute pair.
Definition: Out.h:492
~ProgressBar()
Dtor displays final progress bar.
Definition: Out.h:1030
std::string outLabel(const std::string &msg_r) const
Definition: Out.h:1098
Special list formater writing a Table [asTableHeader|asTableRow].
Definition: Out.h:179
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:651
Default representation of types in Lists [asListElement].
Definition: Out.h:166
void warningPar(size_t indent_r, const Text &text_r, ParFormat format_r=ParFormat())
Paragraph tagged with &#39;Warning: &#39;.
Definition: Out.h:608
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:92
Writer(std::ostream &str_r, const TableLayout &layout_r, const TFormater &formater_r)
Definition: Out.h:295
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:549
Class representing an application (appdata.xml)
Definition: Application.h:27
const zypp::ProgressData * operator->() const
Definition: Out.h:1072
void warningPar(const Text &text_r, ParFormat format_r=ParFormat())
Definition: Out.h:612
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:529
bool typeNORMAL() const
Definition: Out.h:900
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
ColorString tagWarning()
translated "Warning:" warning color
Definition: Out.cc:28