CsvFormatter formatter(columns, rows, _separator);
- return formatter.output();
+ return formatter.str();
}
}
JsonFormatter formatter(data);
- return formatter.output();
+ return formatter.str();
}
}
{
Command::GetConfig::ConfigData::Table::Table(
- const Command::GetConfig& command, TableLineStyle style) :
+ const Command::GetConfig& command, TableStyle style) :
ConfigData(command), _style(style)
{}
TableFormatter formatter(columns, rows, _style);
- return formatter.output();
+ return formatter.str();
}
public:
- Table(const GetConfig& command, TableLineStyle style);
+ Table(const GetConfig& command, TableStyle style);
virtual std::string output() const override;
std::string label_for(const string& column) const;
- TableLineStyle _style;
+ TableStyle _style;
};
CsvFormatter formatter(columns, rows, _separator);
- return formatter.output();
+ return formatter.str();
}
}
formatter.skip_format_values({ SNAPPERS_KEY });
- return formatter.output();
+ return formatter.str();
}
JsonFormatter::List json_list(data);
- return json_list.output(1);
+ return json_list.str(1);
}
JsonFormatter formatter(data);
- return formatter.output(2);
+ return formatter.str(2);
}
}
{
Command::ListConfigs::SnappersData::Table::Table(
- const Command::ListConfigs& command, TableLineStyle style) :
+ const Command::ListConfigs& command, TableStyle style) :
SnappersData(command), _style(style)
{}
TableFormatter formatter(columns, rows, _style);
- return formatter.output();
+ return formatter.str();
}
public:
- Table(const ListConfigs& command, TableLineStyle style);
+ Table(const ListConfigs& command, TableStyle style);
virtual std::string output() const override;
std::string label_for(const std::string& column) const;
- TableLineStyle _style;
+ TableStyle _style;
};
CsvFormatter formatter(columns, rows, _separator);
- return formatter.output();
+ return formatter.str();
}
formatter.set_inline(true);
- return formatter.output(3);
+ return formatter.str(3);
}
}
formatter.skip_format_values(keys);
- return formatter.output();
+ return formatter.str();
}
formatter.skip_format_values(skip_format);
- data.push_back(formatter.output(2));
+ data.push_back(formatter.str(2));
}
JsonFormatter::List json_list(data);
- return json_list.output(1);
+ return json_list.str(1);
}
{
Command::ListSnapshots::SnappersData::Table::Table(
- const Command::ListSnapshots& command, TableLineStyle style) :
+ const Command::ListSnapshots& command, TableStyle style) :
SnappersData(command), _style(style)
{}
TableFormatter formatter(columns, rows, _style);
- return formatter.output();
+ return formatter.str();
}
public:
- Table(const ListSnapshots& command, TableLineStyle style);
+ Table(const ListSnapshots& command, TableStyle style);
virtual std::string output() const override;
TableAlign column_alignment(const string& column) const;
- TableLineStyle _style;
+ TableStyle _style;
};
}
- TableLineStyle
+ TableStyle
GlobalOptions::table_style_value(const ParsedOpts& opts) const
{
ParsedOpts::const_iterator it = opts.find("table-style");
if (it == opts.end())
- return cli::TableFormatter::default_style();
+ return cli::TableFormatter::default_style;
string str = it->second;
if (value >= Table::numStyles)
throw exception();
- return (TableLineStyle)(value);
+ return (TableStyle)(value);
}
catch (const exception&)
{
SN_THROW(OptionsException(error));
}
- return cli::TableFormatter::default_style();
+ return cli::TableFormatter::default_style;
}
{
ParsedOpts::const_iterator it = opts.find("separator");
if (it == opts.end())
- return cli::CsvFormatter::default_separator();
+ return cli::CsvFormatter::default_separator;
return it->second;
}
bool no_dbus() const { return _no_dbus; }
bool version() const { return _version; }
bool help() const { return _help; }
- TableLineStyle table_style() const { return _table_style; }
+ TableStyle table_style() const { return _table_style; }
OutputFormat output_format() const { return _output_format; }
string separator() const { return _separator; }
string config() const { return _config; }
void check_options(const ParsedOpts& parsed_opts) const;
- TableLineStyle table_style_value(const ParsedOpts& parsed_opts) const;
+ TableStyle table_style_value(const ParsedOpts& parsed_opts) const;
OutputFormat output_format_value(const ParsedOpts& parsed_opts) const;
string separator_value(const ParsedOpts& parsed_opts) const;
string config_value(const ParsedOpts& parsed_opts) const;
bool _no_dbus;
bool _version;
bool _help;
- TableLineStyle _table_style;
+ TableStyle _table_style;
OutputFormat _output_format;
string _separator;
string _config;
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include "client/utils/CsvFormatter.h"
-using namespace std;
namespace snapper
{
- namespace cli
- {
-
- namespace
- {
-
- const string DEFAULT_SEPARATOR = ",";
-
-
- string double_quotes(const string value)
- {
- return boost::algorithm::replace_all_copy(value, "\"", "\"\"" );
- }
-
-
- string enclose_with_quotes(const string value)
- {
- return "\"" + value + "\"";
- }
-
- }
+ using namespace std;
- const string CsvFormatter::default_separator()
- {
- return DEFAULT_SEPARATOR;
- }
-
-
- CsvFormatter::CsvFormatter(
- vector<string> columns,
- vector<vector<string>> rows) :
- _columns(columns), _rows(rows), _separator(default_separator())
- {}
+ namespace cli
+ {
- CsvFormatter::CsvFormatter(
- vector<string> columns,
- vector<vector<string>> rows,
- const string separator) :
- _columns(columns), _rows(rows), _separator(separator)
- {}
+ const std::string CsvFormatter::default_separator = ",";
- string CsvFormatter::output() const
+ string
+ CsvFormatter::str() const
{
string cvs_output;
- cvs_output = csv_line(_columns);
+ cvs_output = csv_line(header);
- for (auto row : _rows)
+ for (const vector<string>& row : rows)
cvs_output += csv_line(row);
return cvs_output;
}
- string CsvFormatter::csv_line(vector<string> values) const
+ string
+ CsvFormatter::csv_line(const vector<string>& values) const
{
vector<string> csv_values;
- for (auto value : values)
+ for (const string& value : values)
csv_values.push_back(csv_value(value));
- return boost::algorithm::join(csv_values, _separator) + "\n";
+ return boost::algorithm::join(csv_values, separator) + "\n";
}
- string CsvFormatter::csv_value(const string value) const
+ string
+ CsvFormatter::csv_value(const string& value) const
{
string fixed_value = boost::algorithm::trim_copy(value);
}
- bool CsvFormatter::has_special_chars(const string value) const
+ bool
+ CsvFormatter::has_special_chars(const string& value) const
{
- vector<string> special_chars = { _separator, "\n", "\"" };
+ vector<string> special_chars = { separator, "\n", "\"" };
- for (auto special_char : special_chars)
+ for (const string& special_char : special_chars)
{
if (value.find(special_char) != string::npos)
return true;
return false;
}
+
+ string
+ CsvFormatter::double_quotes(const string& value)
+ {
+ return boost::algorithm::replace_all_copy(value, "\"", "\"\"" );
+ }
+
+
+ string
+ CsvFormatter::enclose_with_quotes(const string& value)
+ {
+ return "\"" + value + "\"";
+ }
+
}
-}
\ No newline at end of file
+
+}
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include <string>
#include <vector>
+
namespace snapper
{
+
+ using namespace std;
+
+
namespace cli
{
public:
- static const std::string default_separator();
-
- CsvFormatter(
- std::vector<std::string> columns,
- std::vector<std::vector<std::string>> rows);
+ static const string default_separator;
- CsvFormatter(
- std::vector<std::string> columns,
- std::vector<std::vector<std::string>> rows,
- const std::string separator);
+ CsvFormatter(const vector<string>& header, const vector<vector<string>>& rows,
+ const string& separator)
+ : header(header), rows(rows), separator(separator)
+ {
+ }
- std::string output() const;
+ string str() const;
private:
- std::string csv_line(std::vector<std::string> values) const;
+ string csv_line(const vector<string>& values) const;
- std::string csv_value(const std::string value) const;
+ string csv_value(const string& value) const;
- bool has_special_chars(const std::string value) const;
+ bool has_special_chars(const string& value) const;
- std::vector<std::string> _columns;
+ static string double_quotes(const string& value);
- std::vector<std::vector<std::string>> _rows;
+ static string enclose_with_quotes(const string& value);
- const std::string _separator;
+ const vector<string> header;
+ const vector<vector<string>> rows;
+ const string separator;
};
}
+
}
#endif
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
-#include <boost/algorithm/string/predicate.hpp>
#include "client/utils/JsonFormatter.h"
-using namespace std;
namespace snapper
{
- namespace cli
- {
- namespace
- {
-
- string indent(u_int indent_level)
- {
- return string(indent_level * 2, ' ');
- }
+ using namespace std;
- string escape(const string& value)
- {
- string fixed_value = value;
+ namespace cli
+ {
- boost::algorithm::replace_all(fixed_value, "\\", "\\\\");
- boost::algorithm::replace_all(fixed_value, "\"", "\\\"");
- boost::algorithm::replace_all(fixed_value, "\b", "\\b");
- boost::algorithm::replace_all(fixed_value, "\f", "\\f");
- boost::algorithm::replace_all(fixed_value, "\n", "\\n");
- boost::algorithm::replace_all(fixed_value, "\r", "\\r");
- boost::algorithm::replace_all(fixed_value, "\t", "\\t");
+ string
+ JsonFormatter::indent(u_int indent_level)
+ {
+ return string(indent_level * 2, ' ');
+ }
- return fixed_value;
- }
+ string
+ JsonFormatter::escape(const string& value)
+ {
+ string fixed_value = value;
- string quote(const string& value)
- {
- return "\"" + value + "\"";
- }
+ boost::algorithm::replace_all(fixed_value, "\\", "\\\\");
+ boost::algorithm::replace_all(fixed_value, "\"", "\\\"");
+ boost::algorithm::replace_all(fixed_value, "\b", "\\b");
+ boost::algorithm::replace_all(fixed_value, "\f", "\\f");
+ boost::algorithm::replace_all(fixed_value, "\n", "\\n");
+ boost::algorithm::replace_all(fixed_value, "\r", "\\r");
+ boost::algorithm::replace_all(fixed_value, "\t", "\\t");
+ return fixed_value;
+ }
- string to_json(const string& value)
- {
- return quote(escape(boost::algorithm::trim_copy(value)));
- }
+ string
+ JsonFormatter::quote(const string& value)
+ {
+ return "\"" + value + "\"";
}
- JsonFormatter::JsonFormatter(const Data& data) :
- _data(data), _inline(false)
- {}
+ string
+ JsonFormatter::to_json(const string& value)
+ {
+ return quote(escape(boost::algorithm::trim_copy(value)));
+ }
- string JsonFormatter::output(u_int indent_level) const
+ string
+ JsonFormatter::str(u_int indent_level) const
{
string first_indent = _inline ? "" : indent(indent_level);
}
- string JsonFormatter::json_attributes(u_int indent_level) const
+ string
+ JsonFormatter::json_attributes(u_int indent_level) const
{
vector<string> attributes;
- for (auto& data_pair : _data)
+ for (auto& data_pair : data)
{
string value = data_pair.second;
}
- bool JsonFormatter::skip_format_value(const string& key) const
+ bool
+ JsonFormatter::skip_format_value(const string& key) const
{
- auto it = find(_skip_format_values.begin(), _skip_format_values.end(), key);
-
- if (it != _skip_format_values.end())
- return true;
-
- return false;
+ return find(_skip_format_values.begin(), _skip_format_values.end(), key) !=
+ _skip_format_values.end();
}
- JsonFormatter::JsonFormatter::List::List(const vector<string>& data) :
- _data(data)
- {}
-
-
- string JsonFormatter::List::output(u_int indent_level) const
+ string
+ JsonFormatter::List::str(u_int indent_level) const
{
return "[\n" +
- boost::algorithm::join(_data, ",\n") + "\n" +
+ boost::algorithm::join(data, ",\n") + "\n" +
indent(indent_level) + "]";
}
}
+
}
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include <vector>
#include <utility>
+
namespace snapper
{
+
+ using namespace std;
+
+
namespace cli
{
/* Very simplistic JSON formatter, but enough for current requirements.
*
* If needed, this could be replaced by some modern library, for example:
- * https://github.com/nlohmann/json
+ * libjson-c
*/
class JsonFormatter
{
class List;
- using Data = std::vector<std::pair<std::string, std::string>>;
+ using Data = vector<pair<string, string>>;
- JsonFormatter(const Data& data);
+ JsonFormatter(const Data& data)
+ : data(data), _inline(false)
+ {
+ }
- void skip_format_values(const std::vector<std::string>& skip_format_values)
+ void skip_format_values(const vector<string>& skip_format_values)
{
_skip_format_values = skip_format_values;
}
_inline = is_inline;
}
- std::string output(u_int indent_level = 0) const;
+ string str(u_int indent_level = 0) const;
private:
- std::string json_attributes(u_int indent_level) const;
+ string json_attributes(u_int indent_level) const;
- bool skip_format_value(const std::string& key) const;
+ bool skip_format_value(const string& key) const;
- const Data& _data;
+ const Data& data;
- std::vector<std::string> _skip_format_values;
+ vector<string> _skip_format_values;
bool _inline;
+
+ static string indent(u_int indent_level);
+ static string escape(const string& value);
+ static string quote(const string& value);
+ static string to_json(const string& value);
+
};
public:
- List(const std::vector<std::string>& data);
+ List(const vector<string>& data)
+ : data(data)
+ {
+ }
- std::string output(u_int indent_level = 0) const;
+ string str(u_int indent_level = 0) const;
private:
- const std::vector<std::string>& _data;
+ const vector<string>& data;
};
}
+
}
#endif
using namespace std;
-TableLineStyle Table::defaultStyle = Ascii;
static
const char * lines[][3] = {
, _max_col (0)
, _max_width(1, 0)
, _width(0)
- , _style (defaultStyle)
+ , _style (Ascii)
, _screen_width(get_screen_width())
, _margin(0)
, _force_break_after(-1)
_do_wrap = true;
}
-void Table::lineStyle (TableLineStyle st) {
- if (st < _End)
- _style = st;
+
+void
+Table::set_style(TableStyle st)
+{
+ if (st < _End)
+ _style = st;
}
+
void Table::margin(unsigned margin) {
if (margin < (unsigned) (_screen_width/2))
_margin = margin;
TableRow::Less comp (by_column);
_rows.sort (comp);
}
-
-// Local Variables:
-// c-basic-offset: 2
-// End:
using std::list;
using std::vector;
-//! table drawing style
-enum TableLineStyle {
+//! table style
+enum TableStyle {
Ascii = 0, ///< | - +
Light,
Heavy,
public:
typedef list<TableRow> container;
- static TableLineStyle defaultStyle;
-
static const unsigned int numStyles = _End;
void add (const TableRow& tr);
bool empty () const { return _rows.empty(); }
void sort (unsigned by_column); // columns start with 0...
- void lineStyle (TableLineStyle st);
+ void set_style(TableStyle st);
void wrap(int force_break_after = -1);
void allowAbbrev(unsigned column);
void margin(unsigned margin);
mutable vector<unsigned> _max_width;
//! table width (columns)
int _width;
- //! table line drawing style
- TableLineStyle _style;
+ //! table style
+ TableStyle _style;
//! amount of space we have to print this table
int _screen_width;
//! whether to abbreviate the respective column if needed
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include "client/utils/TableFormatter.h"
-using namespace std;
namespace snapper
{
- namespace cli
- {
-
- namespace
- {
- const TableLineStyle DEFAULT_STYLE = Table::defaultStyle;
- }
-
- TableLineStyle TableFormatter::default_style()
- {
- return DEFAULT_STYLE;
- }
+ using namespace std;
- TableFormatter::TableFormatter(
- vector<pair<string, TableAlign>> columns,
- vector<vector<string>> rows) :
- _columns(columns), _rows(rows), _style(default_style())
- {}
-
+ namespace cli
+ {
- TableFormatter::TableFormatter(
- vector<pair<string, TableAlign>> columns,
- vector<vector<string>> rows,
- TableLineStyle style) :
- _columns(columns), _rows(rows), _style(style)
- {}
+ const TableStyle TableFormatter::default_style = Ascii;
- string TableFormatter::output() const
+ string
+ TableFormatter::str() const
{
- Table::defaultStyle = _style;
-
Table table;
+ table.set_style(style);
- TableHeader header;
+ TableHeader table_header;
- for (auto column : _columns)
- header.add(column.first, column.second);
+ for (const pair<string, TableAlign>& column : header)
+ table_header.add(column.first, column.second);
- table.setHeader(header);
+ table.setHeader(table_header);
- for (auto row : _rows)
+ for (const vector<string>& row : rows)
{
TableRow table_row;
- for (auto value : row)
+ for (const string& value : row)
table_row.add(value);
table.add(table_row);
}
ostringstream stream;
-
stream << table;
-
return stream.str();
}
}
-}
\ No newline at end of file
+
+}
/*
- * Copyright (c) [2019] SUSE LLC
+ * Copyright (c) [2019-2020] SUSE LLC
*
* All Rights Reserved.
*
#include "client/utils/Table.h"
+
namespace snapper
{
+
+ using namespace std;
+
+
namespace cli
{
public:
- static TableLineStyle default_style();
+ static const TableStyle default_style;
- TableFormatter(
- std::vector<std::pair<std::string, TableAlign>> columns,
- std::vector<std::vector<std::string>> rows);
+ TableFormatter(const vector<pair<string, TableAlign>>& header,
+ const vector<vector<string>>& rows, TableStyle style)
+ : header(header), rows(rows), style(style)
+ {
+ }
- TableFormatter(
- std::vector<std::pair<std::string, TableAlign>> columns,
- std::vector<std::vector<std::string>> rows,
- TableLineStyle style);
-
- std::string output() const;
+ string str() const;
private:
- std::vector<std::pair<std::string, TableAlign>> _columns;
-
- std::vector<std::vector<std::string>> _rows;
-
- TableLineStyle _style;
+ const vector<pair<string, TableAlign>> header;
+ const vector<vector<string>> rows;
+ const TableStyle style;
};
}
+
}
#endif
"\"value;1\";\"value\n2\";\"value\"\"3\"\n"
"value1;\"\"\"value2\"\"\";value3\n";
- BOOST_CHECK_EQUAL(formatter.output(), result);
+ BOOST_CHECK_EQUAL(formatter.str(), result);
}
snapper::cli::JsonFormatter formatter(data);
- BOOST_CHECK_EQUAL(formatter.output(), expected_result);
+ BOOST_CHECK_EQUAL(formatter.str(), expected_result);
}
formatter.skip_format_values({ "key1" });
- BOOST_CHECK_EQUAL(formatter.output(), expected_result);
+ BOOST_CHECK_EQUAL(formatter.str(), expected_result);
}
snapper::cli::JsonFormatter formatter(data);
- BOOST_CHECK_EQUAL(formatter.output(1), expected_result);
+ BOOST_CHECK_EQUAL(formatter.str(1), expected_result);
}
formatter.set_inline(true);
- BOOST_CHECK_EQUAL(formatter.output(1), expected_result);
+ BOOST_CHECK_EQUAL(formatter.str(1), expected_result);
}
"value3\n"
"]";
- BOOST_CHECK_EQUAL(formatter.output(), result);
+ BOOST_CHECK_EQUAL(formatter.str(), result);
}