]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- some cleanup in formatters classes 556/head
authorArvin Schnell <aschnell@suse.de>
Thu, 3 Sep 2020 08:50:57 +0000 (10:50 +0200)
committerArvin Schnell <aschnell@suse.de>
Thu, 3 Sep 2020 08:50:57 +0000 (10:50 +0200)
24 files changed:
client/Command/GetConfig/ConfigData/Csv.cc
client/Command/GetConfig/ConfigData/Json.cc
client/Command/GetConfig/ConfigData/Table.cc
client/Command/GetConfig/ConfigData/Table.h
client/Command/ListConfigs/SnappersData/Csv.cc
client/Command/ListConfigs/SnappersData/Json.cc
client/Command/ListConfigs/SnappersData/Table.cc
client/Command/ListConfigs/SnappersData/Table.h
client/Command/ListSnapshots/SnappersData/Csv.cc
client/Command/ListSnapshots/SnappersData/Json.cc
client/Command/ListSnapshots/SnappersData/Table.cc
client/Command/ListSnapshots/SnappersData/Table.h
client/GlobalOptions.cc
client/GlobalOptions.h
client/utils/CsvFormatter.cc
client/utils/CsvFormatter.h
client/utils/JsonFormatter.cc
client/utils/JsonFormatter.h
client/utils/Table.cc
client/utils/Table.h
client/utils/TableFormatter.cc
client/utils/TableFormatter.h
testsuite/csv-formatter.cc
testsuite/json-formatter.cc

index a31bd37b3427ca1a725411aea0d94357ca0404e8..70808b316d39f4c3a312cd1e870b8c33d3f655ca 100644 (file)
@@ -64,7 +64,7 @@ namespace snapper
 
            CsvFormatter formatter(columns, rows, _separator);
 
-           return formatter.output();
+           return formatter.str();
        }
 
     }
index 823d11a6f07827493567a9717286c9573969f7d6..ce8dde48ec8b5ea64f0a9e3a9579a3a0fd6ba00a 100644 (file)
@@ -38,7 +38,7 @@ namespace snapper
 
            JsonFormatter formatter(data);
 
-           return formatter.output();
+           return formatter.str();
        }
 
     }
index 00e62d61d16e8c560c99b3fa4076c2baaad65aa0..49671babb696c56c42f0bef7dca423a4efcdf0d3 100644 (file)
@@ -32,7 +32,7 @@ namespace snapper
     {
 
        Command::GetConfig::ConfigData::Table::Table(
-           const Command::GetConfig& command, TableLineStyle style) :
+           const Command::GetConfig& command, TableStyle style) :
            ConfigData(command), _style(style)
        {}
 
@@ -63,7 +63,7 @@ namespace snapper
 
            TableFormatter formatter(columns, rows, _style);
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
index a50fc46e350ebd79ecdf50e31d644b6bcdaf3742..ec82cef2a06ebdf7d50cf3c7a231affe9b717a29 100644 (file)
@@ -37,7 +37,7 @@ namespace snapper
 
        public:
 
-           Table(const GetConfig& command, TableLineStyle style);
+           Table(const GetConfig& command, TableStyle style);
 
            virtual std::string output() const override;
 
@@ -45,7 +45,7 @@ namespace snapper
 
            std::string label_for(const string& column) const;
 
-           TableLineStyle _style;
+           TableStyle _style;
 
        };
 
index 5bd9637476db3e26bbdb0a61dd1911c90905f3e5..e11d22beddfcc6d9c880e9ad9125f04eaea92100 100644 (file)
@@ -59,7 +59,7 @@ namespace snapper
 
            CsvFormatter formatter(columns, rows, _separator);
 
-           return formatter.output();
+           return formatter.str();
        }
 
     }
index 8206c0f6e9d0d2346b84b5027434ddd202144f3a..8da87e13a687c69f264cef15f3f604e91c671b0a 100644 (file)
@@ -48,7 +48,7 @@ namespace snapper
 
            formatter.skip_format_values({ SNAPPERS_KEY });
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
@@ -64,7 +64,7 @@ namespace snapper
 
            JsonFormatter::List json_list(data);
 
-           return json_list.output(1);
+           return json_list.str(1);
        }
 
 
@@ -77,7 +77,7 @@ namespace snapper
 
            JsonFormatter formatter(data);
 
-           return formatter.output(2);
+           return formatter.str(2);
        }
 
     }
index 62f097b26cf313532e52df26ddd41fe2c93cd072..b2f4496ea6074e415c9e6ef1a450c90757d6447c 100644 (file)
@@ -32,7 +32,7 @@ namespace snapper
     {
 
        Command::ListConfigs::SnappersData::Table::Table(
-           const Command::ListConfigs& command, TableLineStyle style) :
+           const Command::ListConfigs& command, TableStyle style) :
            SnappersData(command), _style(style)
        {}
 
@@ -58,7 +58,7 @@ namespace snapper
 
            TableFormatter formatter(columns, rows, _style);
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
index 13d2aeeae3d5f8eaa39ec2f7449ec2eb535efcc6..cf51fa10338d1025321b356977e4cacc433b020f 100644 (file)
@@ -37,7 +37,7 @@ namespace snapper
 
        public:
 
-           Table(const ListConfigs& command, TableLineStyle style);
+           Table(const ListConfigs& command, TableStyle style);
 
            virtual std::string output() const override;
 
@@ -45,7 +45,7 @@ namespace snapper
 
            std::string label_for(const std::string& column) const;
 
-           TableLineStyle _style;
+           TableStyle _style;
 
        };
 
index 9be1ba6248b7f54565f3e85c5260cebffedfc50b..d9634e16b781493891cf1edad68cbfa92a278e8d 100644 (file)
@@ -55,7 +55,7 @@ namespace snapper
 
            CsvFormatter formatter(columns, rows, _separator);
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
index b62449005459a07a806ff391f7e35b95bb0dbeec..7804c861d9b272e1c02edec819f0b816aab73197 100644 (file)
@@ -49,7 +49,7 @@ namespace snapper
 
                formatter.set_inline(true);
 
-               return formatter.output(3);
+               return formatter.str(3);
            }
 
        }
@@ -72,7 +72,7 @@ namespace snapper
 
            formatter.skip_format_values(keys);
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
@@ -118,12 +118,12 @@ namespace snapper
 
                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);
        }
 
 
index d88dbdc8bed66f4ca07cde8993d3cf9573ac2286..7238af611072e637cb0c563423759be3ca3e3d05 100644 (file)
@@ -36,7 +36,7 @@ namespace snapper
     {
 
        Command::ListSnapshots::SnappersData::Table::Table(
-           const Command::ListSnapshots& command, TableLineStyle style) :
+           const Command::ListSnapshots& command, TableStyle style) :
            SnappersData(command), _style(style)
        {}
 
@@ -178,7 +178,7 @@ namespace snapper
 
            TableFormatter formatter(columns, rows, _style);
 
-           return formatter.output();
+           return formatter.str();
        }
 
 
index b32d3ce7c4d5598e27bc7c893822bcc05b4cafdb..397a6ca7011ae830f64c85299a7c7f5a6a5d2de6 100644 (file)
@@ -37,7 +37,7 @@ namespace snapper
 
        public:
 
-           Table(const ListSnapshots& command, TableLineStyle style);
+           Table(const ListSnapshots& command, TableStyle style);
 
            virtual std::string output() const override;
 
@@ -58,7 +58,7 @@ namespace snapper
 
            TableAlign column_alignment(const string& column) const;
 
-           TableLineStyle _style;
+           TableStyle _style;
 
        };
 
index 703b5be3a00f079cde722c32ce4b81c8ba6e84cd..0e5aae66fb29e54639e9c629fd04a58c6af95b56 100644 (file)
@@ -107,12 +107,12 @@ namespace snapper
     }
 
 
-    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;
 
@@ -123,7 +123,7 @@ namespace snapper
            if (value >= Table::numStyles)
                throw exception();
 
-           return (TableLineStyle)(value);
+           return (TableStyle)(value);
        }
        catch (const exception&)
        {
@@ -133,7 +133,7 @@ namespace snapper
            SN_THROW(OptionsException(error));
        }
 
-       return cli::TableFormatter::default_style();
+       return cli::TableFormatter::default_style;
     }
 
 
@@ -171,7 +171,7 @@ namespace snapper
     {
        ParsedOpts::const_iterator it = opts.find("separator");
        if (it == opts.end())
-           return cli::CsvFormatter::default_separator();
+           return cli::CsvFormatter::default_separator;
 
        return it->second;
     }
index b4abff225c5c3b338de11bdfb89ab5c1c2d11ac3..9c953dfdb487ef28bbf0251b2bfb12e1ca60ad06 100644 (file)
@@ -52,7 +52,7 @@ namespace snapper
        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; }
@@ -65,7 +65,7 @@ namespace snapper
 
        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;
@@ -79,7 +79,7 @@ namespace snapper
        bool _no_dbus;
        bool _version;
        bool _help;
-       TableLineStyle _table_style;
+       TableStyle _table_style;
        OutputFormat _output_format;
        string _separator;
        string _config;
index b566b89f513037e4a31c0947f1436c3de19f76b3..860eda51b9b8ba83f00b3593af2243f30c7ec589 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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);
 
@@ -108,11 +76,12 @@ namespace snapper
        }
 
 
-       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;
@@ -121,5 +90,20 @@ namespace snapper
            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
+
+}
index 24c8c327a64bcdc546a323aa6625fe2640b66b62..363132d289e6b330ffc1d62f524ebd5f4da717d3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
     {
 
@@ -35,36 +40,36 @@ namespace snapper
 
        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
index ec2f7c667d2e9aa1bd8f840e002ee74cef1db518..b22bc9c2c56a9516c75192ef247845b4562f38e0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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);
 
@@ -87,11 +84,12 @@ namespace snapper
        }
 
 
-       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;
 
@@ -105,28 +103,22 @@ namespace snapper
        }
 
 
-       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) + "]";
        }
 
     }
+
 }
index 79f7672de3adc2f6bbfcf57b00ee302ae80af398..48ed9d1d81c543ade1e03d74f25aeaa5258c973b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
        {
@@ -43,11 +48,14 @@ namespace snapper
 
            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;
            }
@@ -57,19 +65,25 @@ namespace snapper
                _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);
+
        };
 
 
@@ -78,17 +92,21 @@ namespace snapper
 
        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
index 371f8e2480a41a1702002e9912eb232461019080..7990ae22c770d0a956f051d8b521a1f9a8219778 100644 (file)
@@ -11,7 +11,6 @@
 
 using namespace std;
 
-TableLineStyle Table::defaultStyle = Ascii;
 
 static
 const char * lines[][3] = {
@@ -130,7 +129,7 @@ Table::Table()
   , _max_col (0)
   , _max_width(1, 0)
   , _width(0)
-  , _style (defaultStyle)
+  , _style (Ascii)
   , _screen_width(get_screen_width())
   , _margin(0)
   , _force_break_after(-1)
@@ -248,11 +247,15 @@ void Table::wrap(int force_break_after)
   _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;
@@ -269,7 +272,3 @@ void Table::sort (unsigned by_column) {
   TableRow::Less comp (by_column);
   _rows.sort (comp);
 }
-
-// Local Variables:
-// c-basic-offset: 2
-// End:
index dd266c89e3f2b65cd237727cda02b7a4fa2a2a4c..b5e1f12becba946f43ba61530dcf311a47fe21ad 100644 (file)
@@ -20,8 +20,8 @@ using std::ostream;
 using std::list;
 using std::vector;
 
-//! table drawing style
-enum TableLineStyle {
+//! table style
+enum TableStyle {
   Ascii         = 0,           ///< | - +
   Light,
   Heavy,
@@ -101,8 +101,6 @@ class Table {
 public:
   typedef list<TableRow> container;
 
-  static TableLineStyle defaultStyle;
-
   static const unsigned int numStyles = _End;
 
   void add (const TableRow& tr);
@@ -111,7 +109,7 @@ public:
   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);
@@ -132,8 +130,8 @@ private:
   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
index 330da3b253e2759a04d3d6e4e7b5c36b2a2530be..40742b8e0af13e1c199120614725a4194ed30c99 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
+
+}
index ed1a3db00b0d17ff00f8277f4bc4326841b618a6..cbbcdaea9fc48ffdc39016de4b811b03a56799a7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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
     {
 
@@ -38,30 +43,26 @@ namespace snapper
 
        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
index 8851bed159a5dd6fedd04ecf5e5a55d116211a51..8415051612b559090794d7965b7229fe05b476e6 100644 (file)
@@ -29,5 +29,5 @@ BOOST_AUTO_TEST_CASE(test1)
        "\"value;1\";\"value\n2\";\"value\"\"3\"\n"
        "value1;\"\"\"value2\"\"\";value3\n";
 
-    BOOST_CHECK_EQUAL(formatter.output(), result);
+    BOOST_CHECK_EQUAL(formatter.str(), result);
 }
index 541a85ad9a925fae68c8bf26f94349edf42d6535..55fb726602650121a08accec3e39ca753eccbd29 100644 (file)
@@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE(test1_escape_values)
 
     snapper::cli::JsonFormatter formatter(data);
 
-    BOOST_CHECK_EQUAL(formatter.output(), expected_result);
+    BOOST_CHECK_EQUAL(formatter.str(), expected_result);
 }
 
 
@@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE(test2_skip_format)
 
     formatter.skip_format_values({ "key1" });
 
-    BOOST_CHECK_EQUAL(formatter.output(), expected_result);
+    BOOST_CHECK_EQUAL(formatter.str(), expected_result);
 }
 
 
@@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(test3_indent)
 
     snapper::cli::JsonFormatter formatter(data);
 
-    BOOST_CHECK_EQUAL(formatter.output(1), expected_result);
+    BOOST_CHECK_EQUAL(formatter.str(1), expected_result);
 }
 
 
@@ -105,7 +105,7 @@ BOOST_AUTO_TEST_CASE(test4_inline)
 
     formatter.set_inline(true);
 
-    BOOST_CHECK_EQUAL(formatter.output(1), expected_result);
+    BOOST_CHECK_EQUAL(formatter.str(1), expected_result);
 }
 
 
@@ -122,5 +122,5 @@ BOOST_AUTO_TEST_CASE(test5_list)
        "value3\n"
        "]";
 
-    BOOST_CHECK_EQUAL(formatter.output(), result);
+    BOOST_CHECK_EQUAL(formatter.str(), result);
 }