libzypp 17.34.0
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
22namespace ztui {
23
25namespace out
26{
28 { return Application::instance().out().termwidth(); }
29} // namespace out
31
33// class TermLine
35
36std::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)!
40
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
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 {
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
106constexpr Out::Type Out::TYPE_NONE;
107constexpr Out::Type Out::TYPE_ALL;
108
110{}
111
113{
114 return verbosity() < Out::NORMAL;
115}
116
118{
119 return e.asUserHistory();
120}
121
123{
124 std::cout << table_r;
125}
126
127void 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
138}
139
141// class Out::Error
143
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
153std::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}
167{ return Application::instance().out().zyppExceptionReport( ex_r ); }
168
169}
static Application & instance()
Verbosity verbosity() const
Get current verbosity.
Definition Out.h:870
static constexpr Type TYPE_ALL
Definition Out.h:451
@ NORMAL
Default output verbosity level.
Definition Out.h:436
static constexpr Type TYPE_NONE
Definition Out.h:450
virtual bool progressFilter()
Determine whether to show progress.
Definition Out.cc:112
virtual void searchResult(const Table &table_r)
Print out a search result.
Definition Out.cc:122
virtual std::string zyppExceptionReport(const zypp::Exception &e)
Return a Exception as a string suitable for output.
Definition Out.cc:117
virtual ~Out()
Definition Out.cc:109
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.
Simple utf8 string.
Definition Utf8.h:32
Class representing an application (appdata.xml)
Definition Application.h:28
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
Base class for Exception.
Definition Exception.h:147
static constexpr unsigned termwidthUnlimited
Definition Out.h:80
unsigned defaultTermwidth()
Definition Out.cc:27
CCString< ColorContext::MSG_ERROR > MSG_ERRORString
Definition colors.h:80
CCString< ColorContext::MSG_WARNING > MSG_WARNINGString
Definition colors.h:81
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:46
std::string _hint
Definition Out.h:1177
static std::string combine(std::string &&msg_r, const zypp::Exception &ex_r)
Definition Out.cc:153
std::string _msg
Definition Out.h:1176
int report(Application &app_r) const
Default way of processing a caught Error exception.
Definition Out.cc:144
zypp::DefaultIntegral< int,-1 > percentHint
Definition Out.h:365
std::string get() const
Return plain line made of lhs + rhs.
Definition Out.h:372
zypp::str::Str rhs
Definition Out.h:368
zypp::str::Str lhs
Definition Out.h:367
Convenient building of std::string via std::ostringstream Basically a std::ostringstream autoconverti...
Definition String.h:212
const std::ostream & stream() const
Definition String.h:224
#define _(MSG)
Definition Gettext.h:39