]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/Table.h
added install instructions
[gen-shell.git] / src / libshared / src / Table.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #ifndef INCLUDED_TABLE
28 #define INCLUDED_TABLE
29
30 #include <string>
31 #include <vector>
32 #include <Color.h>
33
34 class Table
35 {
36 public:
37   // View specifications.
38   void add (const std::string& col, bool alignLeft = true, bool wrap = true);
39   void width (int width)               { _width = width;             }
40   void leftMargin (int margin)         { _left_margin = margin;      }
41   void colorHeader (const Color& c)    { _header = c;                }
42   void colorOdd (const Color& c)       { _odd = c;                   }
43   void colorEven (const Color& c)      { _even = c;                  }
44   void intraPadding (int padding)      { _intra_padding = padding;   }
45   void intraColorOdd (const Color& c)  { _intra_odd = c;             }
46   void intraColorEven (const Color& c) { _intra_even = c;            }
47   void extraPadding (int padding)      { _extra_padding = padding;   }
48   void extraColorOdd (const Color& c)  { _extra_odd = c;             }
49   void extraColorEven (const Color& c) { _extra_even = c;            }
50   void truncateLines (int n)           { _truncate_lines = n;        }
51   void truncateRows (int n)            { _truncate_rows = n;         }
52   void forceColor ()                   { _forceColor = true;         }
53   void obfuscate ()                    { _obfuscate = true;          }
54   void underlineHeaders ()             { _underline_headers = true;  }
55   int lines ()                         { return _lines;              }
56   int rows ()                          { return (int) _data.size (); }
57
58   // Data provision.
59   int addRow ();
60   int addRowOdd ();
61   int addRowEven ();
62   void set (int, int, const std::string&, const Color color = Color::nocolor);
63   void set (int, int, int, const Color color = Color::nocolor);
64   void set (int, int, const Color);
65
66   // View rendering.
67   std::string render ();
68
69 private:
70   void measureCell (const std::string&, unsigned int&, unsigned int&) const;
71   void renderCell (std::vector <std::string>&, const std::string&, int, bool, bool, const Color&) const;
72
73 private:
74   std::vector <std::vector <std::string>> _data;
75   std::vector <std::vector <Color>>       _color;
76   std::vector <std::string>               _columns;
77   std::vector <bool>                      _align;
78   std::vector <bool>                      _wrap;
79   std::vector <bool>                      _oddness;
80   int                                     _width             {0};
81   int                                     _left_margin       {0};
82   Color                                   _header            {0};
83   Color                                   _odd               {0};
84   Color                                   _even              {0};
85   int                                     _intra_padding     {1};
86   Color                                   _intra_odd         {0};
87   Color                                   _intra_even        {0};
88   int                                     _extra_padding     {0};
89   Color                                   _extra_odd         {0};
90   Color                                   _extra_even        {0};
91   int                                     _truncate_lines    {0};
92   int                                     _truncate_rows     {0};
93   bool                                    _forceColor        {false};
94   bool                                    _obfuscate         {false};
95   bool                                    _underline_headers {false};
96   int                                     _lines             {0};
97   int                                     _rows              {0};
98 };
99
100 #endif