libzypp  17.37.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-core/base/Gettext.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 
24  namespace text {
25  // translator: usually followed by a ' ' and some explanatory text
26  ColorString tagNote() { return HIGHLIGHTString(_("Note:") ); }
27  // translator: usually followed by a ' ' and some explanatory text
28  ColorString tagWarning() { return MSG_WARNINGString(_("Warning:") ); }
29  // translator: usually followed by a ' ' and some explanatory text
30  ColorString tagError() { return MSG_ERRORString(_("Error:") ); }
31 
32  const char * qContinue() { return _("Continue?"); }
33  }
34 
36 namespace out
37 {
38  unsigned defaultTermwidth()
39  { return Application::instance().out().termwidth(); }
40 } // namespace out
42 
44 // class TermLine
46 
47 std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) const
48 {
49  utf8::string l(lhs); // utf8::string::size() returns visible chars (ignores ansi SGR)!
50  utf8::string r(rhs);
51 
52  if ( width_r == out::termwidthUnlimited )
53  return zypp::str::Str() << l << r; // plain string if zero width
54 
55  unsigned llen = l.size();
56  unsigned rlen = r.size();
57  int diff = width_r - llen - rlen;
58 
59  //AutoDispose<int> _delay( 1, ::sleep );
60 
61  if ( diff > 0 )
62  {
63  // expand...
64  if ( ! ( flags_r.testFlag( SF_EXPAND ) && ::isatty(STDOUT_FILENO) ) )
65  return zypp::str::Str() << l << r;
66 
67  if ( percentHint < 0 || percentHint > 100 )
68  return zypp::str::Str() << l << std::string( diff, exp_r ) << r;
69 
70  // else: draw % indicator
71  // -------
72  // <1%>===
73  // .<99%>=
74  // .<100%>
75  if ( percentHint == 0 )
76  return zypp::str::Str() << l << std::string( diff, '-' ) << r;
77 
78 
79  unsigned pc = diff * percentHint / 100; // diff > 0 && percentHint > 0
80  if ( diff < 6 ) // not enough space for fancy stuff
81  return zypp::str::Str() << l << std::string( pc, '.' ) << std::string( diff-pc, '=' ) << r;
82 
83  // else: less boring
84  std::string tag( zypp::str::Str() << '<' << percentHint << "%>" );
85  pc = pc > tag.size() ? (diff - tag.size()) * percentHint / 100 : 0;
86  return zypp::str::Str() << l << std::string( pc, '.' ) << tag << std::string( diff-pc-tag.size(), '=' ) << r;
87  }
88  else if ( diff < 0 )
89  {
90  // truncate...
91  if ( flags_r.testFlag( SF_CRUSH ) )
92  {
93  if ( rlen > width_r )
94  return r.substr( 0, width_r ).str();
95  return zypp::str::Str() << l.substr( 0, width_r - rlen ) << r;
96  }
97  else if ( flags_r.testFlag( SF_SPLIT ) )
98  {
99  zypp::str::Str out;
100  if ( llen > width_r )
101  mbs_write_wrapped( out.stream(), l.str(), 0, width_r );
102  else
103  out << l;
104  return out << "\n" << ( rlen > width_r ? r.substr( 0, width_r ) : std::string( width_r - rlen, ' ' ) + r );
105  }
106  // else:
107  return zypp::str::Str() << l << r;
108  }
109  // else: fits exactly
110  return zypp::str::Str() << l << r;
111 }
112 
114 // class Out
116 
117 constexpr Out::Type Out::TYPE_NONE;
118 constexpr Out::Type Out::TYPE_ALL;
119 
121 {}
122 
124 {
125  return verbosity() < Out::NORMAL;
126 }
127 
129 {
130  return e.asUserHistory();
131 }
132 
133 void Out::searchResult( const Table & table_r )
134 {
135  std::cout << table_r;
136 }
137 
138 void Out::progressEnd( const std::string & id, const std::string & label, ProgressEnd donetag_r )
139 {
140  // translator: Shown as result tag in a progress bar: ............[done]
141  static const std::string done { _("done") };
142  // translator: Shown as result tag in a progress bar: .......[attention]
143  static const std::string attention { MSG_WARNINGString(_("attention")).str() };
144  // translator: Shown as result tag in a progress bar: ...........[error]
145  static const std::string error { MSG_ERRORString(_("error")).str() };
146 
147  const std::string & donetag { donetag_r==ProgressEnd::done ? done : donetag_r==ProgressEnd::error ? error : attention };
148  progressEnd( id, label, donetag, donetag_r==ProgressEnd::error );
149 }
150 
152 // class Out::Error
154 
155 int Out::Error::report( Application & app_r ) const
156 {
157  if ( ! ( _msg.empty() && _hint.empty() ) )
158  app_r.out().error( _msg, _hint );
159  if ( _exitcode != ZTUI_EXIT_OK ) // ZTUI_EXIT_OK indicates exitcode is already set.
160  app_r.setExitCode( _exitcode );
161  return app_r.exitCode();
162 }
163 
164 std::string Out::Error::combine( std::string && msg_r, const zypp::Exception & ex_r )
165 {
166  if ( msg_r.empty() )
167  {
168  msg_r = combine( ex_r );
169  }
170  else
171  {
172  msg_r += "\n";
173  msg_r += combine( ex_r );
174  }
175  return std::move(msg_r);
176 }
177 std::string Out::Error::combine( const zypp::Exception & ex_r )
178 { return Application::instance().out().zyppExceptionReport( ex_r ); }
179 
180 }
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition: Out.cc:128
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
Colored string if do_colors.
Definition: ansi.h:496
#define _(MSG)
Definition: Gettext.h:39
size_type size() const
utf8 size
Definition: Utf8.h:49
static constexpr unsigned termwidthUnlimited
Definition: Out.h:74
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:120
Verbosity verbosity() const
Get current verbosity.
Definition: Out.h:864
const std::ostream & stream() const
Definition: String.h:225
CCString< ColorContext::MSG_WARNING > MSG_WARNINGString
Definition: colors.h:81
ColorString tagError()
translated "Error:" error color
Definition: Out.cc:30
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:361
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition: String.h:212
static constexpr Type TYPE_ALL
Definition: Out.h:445
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:140
Simple utf8 string.
Definition: Utf8.h:31
Default output verbosity level.
Definition: Out.h:430
zypp::DefaultIntegral< int,-1 > percentHint
Definition: Out.h:359
CCString< ColorContext::MSG_ERROR > MSG_ERRORString
Definition: colors.h:80
ProgressEnd
ProgressBars default end tags.
Definition: Out.h:45
const char * qContinue()
translated "Continue?"
Definition: Out.cc:32
std::string get() const
Return plain line made of lhs + rhs.
Definition: Out.h:366
unsigned defaultTermwidth()
Definition: Out.cc:38
zypp::str::Str rhs
Definition: Out.h:362
virtual bool progressFilter()
Determine whether to show progress.
Definition: Out.cc:123
static constexpr Type TYPE_NONE
Definition: Out.h:444
ColorString tagNote()
translated "Note:" highlighted
Definition: Out.cc:26
Base class for Exception.
Definition: Exception.h:152
std::string _hint
Definition: Out.h:1171
int _exitcode
Definition: Out.h:1169
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition: Out.cc:155
virtual void searchResult(const Table &table_r)
Print out a search result.
Definition: Out.cc:133
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition: Out.cc:164
std::string _msg
Definition: Out.h:1170
CCString< ColorContext::HIGHLIGHT > HIGHLIGHTString
Definition: colors.h:87
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
ColorString tagWarning()
translated "Warning:" warning color
Definition: Out.cc:28
const std::string & str() const
Definition: Utf8.h:44