libzypp  17.32.5
Out.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 ----------------------------------------------------------------------*/
9 
10 #include <unistd.h>
11 
12 #include <iostream>
13 #include <sstream>
14 
15 //#include <zypp/AutoDispose.h>
16 
17 #include "Out.h"
18 #include <zypp-tui/Table.h>
19 #include <zypp-tui/Application>
20 #include "Utf8.h"
21 
22 namespace ztui {
23 
25 namespace out
26 {
27  unsigned defaultTermwidth()
28  { return Application::instance().out().termwidth(); }
29 } // namespace out
31 
33 // class TermLine
35 
36 std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) const
37 {
38  utf8::string l(lhs); // utf8::string::size() returns visible chars (ignores ansi SGR)!
39  utf8::string r(rhs);
40 
41  if ( width_r == out::termwidthUnlimited )
42  return zypp::str::Str() << l << r; // plain string if zero width
43 
44  unsigned llen = l.size();
45  unsigned rlen = r.size();
46  int diff = width_r - llen - rlen;
47 
48  //AutoDispose<int> _delay( 1, ::sleep );
49 
50  if ( diff > 0 )
51  {
52  // expand...
53  if ( ! ( flags_r.testFlag( SF_EXPAND ) && ::isatty(STDOUT_FILENO) ) )
54  return zypp::str::Str() << l << r;
55 
56  if ( percentHint < 0 || percentHint > 100 )
57  return zypp::str::Str() << l << std::string( diff, exp_r ) << r;
58 
59  // else: draw % indicator
60  // -------
61  // <1%>===
62  // .<99%>=
63  // .<100%>
64  if ( percentHint == 0 )
65  return zypp::str::Str() << l << std::string( diff, '-' ) << r;
66 
67 
68  unsigned pc = diff * percentHint / 100; // diff > 0 && percentHint > 0
69  if ( diff < 6 ) // not enough space for fancy stuff
70  return zypp::str::Str() << l << std::string( pc, '.' ) << std::string( diff-pc, '=' ) << r;
71 
72  // else: less boring
73  std::string tag( zypp::str::Str() << '<' << percentHint << "%>" );
74  pc = pc > tag.size() ? (diff - tag.size()) * percentHint / 100 : 0;
75  return zypp::str::Str() << l << std::string( pc, '.' ) << tag << std::string( diff-pc-tag.size(), '=' ) << r;
76  }
77  else if ( diff < 0 )
78  {
79  // truncate...
80  if ( flags_r.testFlag( SF_CRUSH ) )
81  {
82  if ( rlen > width_r )
83  return r.substr( 0, width_r ).str();
84  return zypp::str::Str() << l.substr( 0, width_r - rlen ) << r;
85  }
86  else if ( flags_r.testFlag( SF_SPLIT ) )
87  {
88  zypp::str::Str out;
89  if ( llen > width_r )
90  mbs_write_wrapped( out.stream(), l.str(), 0, width_r );
91  else
92  out << l;
93  return out << "\n" << ( rlen > width_r ? r.substr( 0, width_r ) : std::string( width_r - rlen, ' ' ) + r );
94  }
95  // else:
96  return zypp::str::Str() << l << r;
97  }
98  // else: fits exactly
99  return zypp::str::Str() << l << r;
100 }
101 
103 // class Out
105 
106 constexpr Out::Type Out::TYPE_NONE;
107 constexpr Out::Type Out::TYPE_ALL;
108 
110 {}
111 
113 {
114  return verbosity() < Out::NORMAL;
115 }
116 
118 {
119  return e.asUserHistory();
120 }
121 
122 void Out::searchResult( const Table & table_r )
123 {
124  std::cout << table_r;
125 }
126 
127 void Out::progressEnd( const std::string & id, const std::string & label, ProgressEnd donetag_r )
128 {
129  // translator: Shown as result tag in a progress bar: ............[done]
130  static const std::string done { _("done") };
131  // translator: Shown as result tag in a progress bar: .......[attention]
132  static const std::string attention { MSG_WARNINGString(_("attention")).str() };
133  // translator: Shown as result tag in a progress bar: ...........[error]
134  static const std::string error { MSG_ERRORString(_("error")).str() };
135 
136  const std::string & donetag { donetag_r==ProgressEnd::done ? done : donetag_r==ProgressEnd::error ? error : attention };
137  progressEnd( id, label, donetag, donetag_r==ProgressEnd::error );
138 }
139 
141 // class Out::Error
143 
144 int Out::Error::report( Application & app_r ) const
145 {
146  if ( ! ( _msg.empty() && _hint.empty() ) )
147  app_r.out().error( _msg, _hint );
148  if ( _exitcode != ZTUI_EXIT_OK ) // ZTUI_EXIT_OK indicates exitcode is already set.
149  app_r.setExitCode( _exitcode );
150  return app_r.exitCode();
151 }
152 
153 std::string Out::Error::combine( std::string && msg_r, const zypp::Exception & ex_r )
154 {
155  if ( msg_r.empty() )
156  {
157  msg_r = combine( ex_r );
158  }
159  else
160  {
161  msg_r += "\n";
162  msg_r += combine( ex_r );
163  }
164  return std::move(msg_r);
165 }
166 std::string Out::Error::combine( const zypp::Exception & ex_r )
167 { return Application::instance().out().zyppExceptionReport( ex_r ); }
168 
169 }
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition: Out.cc:117
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
#define _(MSG)
Definition: Gettext.h:37
size_type size() const
utf8 size
Definition: Utf8.h:49
static constexpr unsigned termwidthUnlimited
Definition: Out.h:80
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.
virtual ~Out()
Definition: Out.cc:109
Verbosity verbosity() const
Get current verbosity.
Definition: Out.h:870
const std::ostream & stream() const
Definition: String.h:224
CCString< ColorContext::MSG_WARNING > MSG_WARNINGString
Definition: colors.h:81
string substr(size_type pos_r=0, size_type len_r=npos) const
utf8 substring
Definition: Utf8.h:122
zypp::str::Str lhs
Definition: Out.h:367
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:211
static constexpr Type TYPE_ALL
Definition: Out.h:451
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:110
Simple utf8 string.
Definition: Utf8.h:31
Default output verbosity level.
Definition: Out.h:436
zypp::DefaultIntegral< int,-1 > percentHint
Definition: Out.h:365
CCString< ColorContext::MSG_ERROR > MSG_ERRORString
Definition: colors.h:80
ProgressEnd
ProgressBars default end tags.
Definition: Out.h:46
std::string get() const
Return plain line made of lhs + rhs.
Definition: Out.h:372
unsigned defaultTermwidth()
Definition: Out.cc:27
zypp::str::Str rhs
Definition: Out.h:368
virtual bool progressFilter()
Determine whether to show progress.
Definition: Out.cc:112
static constexpr Type TYPE_NONE
Definition: Out.h:450
Base class for Exception.
Definition: Exception.h:146
std::string _hint
Definition: Out.h:1177
int _exitcode
Definition: Out.h:1175
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition: Out.cc:144
virtual void searchResult(const Table &table_r)
Print out a search result.
Definition: Out.cc:122
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition: Out.cc:153
std::string _msg
Definition: Out.h:1176
virtual void error(const std::string &problem_desc, const std::string &hint="")=0
Show an error message and an optional hint.
Class representing an application (appdata.xml)
Definition: Application.h:27
static constexpr int ZTUI_EXIT_OK
Definition: application.h:24
const std::string & str() const
Definition: Utf8.h:44