libzypp 17.37.17
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
16
17#include "Out.h"
18#include <zypp-tui/Table.h>
19#include <zypp-tui/Application>
20#include "Utf8.h"
21
22namespace 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
36namespace out
37{
39 { return Application::instance().out().termwidth(); }
40} // namespace out
41
42
44// class TermLine
46
47std::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)!
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
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 {
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
117constexpr Out::Type Out::TYPE_NONE;
118constexpr Out::Type Out::TYPE_ALL;
119
121{}
122
124{
125 return verbosity() < Out::NORMAL;
126}
127
129{
130 return e.asUserHistory();
131}
132
133void Out::searchResult( const Table & table_r )
134{
135 std::cout << table_r;
136}
137
138void 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
155int 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
164std::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}
177std::string Out::Error::combine( const zypp::Exception & ex_r )
178{ return Application::instance().out().zyppExceptionReport( ex_r ); }
179
180}
static Application & instance()
Verbosity verbosity() const
Get current verbosity.
Definition Out.h:864
static constexpr Type TYPE_ALL
Definition Out.h:445
@ NORMAL
Default output verbosity level.
Definition Out.h:430
static constexpr Type TYPE_NONE
Definition Out.h:444
virtual bool progressFilter()
Determine whether to show progress.
Definition Out.cc:123
virtual void error(const std::string &problem_desc, const std::string &hint="")=0
Show an error message and an optional hint.
virtual void searchResult(const Table &table_r)
Print out a search result.
Definition Out.cc:133
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition Out.cc:128
virtual ~Out()
Definition Out.cc:120
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.
Colored string if do_colors.
Definition ansi.h:497
std::string str() const
Return the colored string if do_colors.
Definition ansi.h:598
Simple utf8 string.
Definition Utf8.h:32
string substr(size_type pos_r=0, size_type len_r=npos) const
utf8 substring
Definition Utf8.h:122
const std::string & str() const
Definition Utf8.h:44
size_type size() const
utf8 size
Definition Utf8.h:49
Class representing an application (appdata.xml)
Definition Application.h:28
Base class for Exception.
Definition Exception.h:153
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition Exception.cc:140
static constexpr unsigned termwidthUnlimited
Definition Out.h:74
unsigned defaultTermwidth()
Definition Out.cc:38
ColorString tagWarning()
translated "Warning:" warning color
Definition Out.cc:28
ColorString tagNote()
translated "Note:" highlighted
Definition Out.cc:26
const char * qContinue()
translated "Continue?"
Definition Out.cc:32
ColorString tagError()
translated "Error:" error color
Definition Out.cc:30
CCString< ColorContext::MSG_ERROR > MSG_ERRORString
Definition colors.h:80
CCString< ColorContext::MSG_WARNING > MSG_WARNINGString
Definition colors.h:81
CCString< ColorContext::HIGHLIGHT > HIGHLIGHTString
Definition colors.h:87
static constexpr int ZTUI_EXIT_OK
Definition application.h:24
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
ProgressEnd
ProgressBars default end tags.
Definition Out.h:45
std::string _hint
Definition Out.h:1171
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition Out.cc:164
std::string _msg
Definition Out.h:1170
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition Out.cc:155
zypp::DefaultIntegral< int,-1 > percentHint
Definition Out.h:359
std::string get() const
Return plain line made of lhs + rhs.
Definition Out.h:366
zypp::str::Str rhs
Definition Out.h:362
zypp::str::Str lhs
Definition Out.h:361
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:213
#define _(MSG)
Definition Gettext.h:39