libzypp: zypp-tui/output/Out.cc Source File

libzypp 17.38.14

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