From 51fe9094e2ccd3298602098c9ebb009f8de408a8 Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Tue, 14 Sep 2021 03:03:02 -0700 Subject: [PATCH] Updated table library to support cell borders without also requiring table borders. --- README.md | 10 ++++----- graphs.hpp | 10 ++++----- tables.hpp | 59 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 64dd8d0..0f71877 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build Status](https://travis-ci.org/tdulcet/Tables-and-Graphs.svg?branch=master)](https://travis-ci.org/tdulcet/Tables-and-Graphs) +[![Build Status](https://travis-ci.com/tdulcet/Tables-and-Graphs.svg?branch=master)](https://travis-ci.com/tdulcet/Tables-and-Graphs) # Tables and Graphs @@ -8,7 +8,7 @@ Copyright © 2018 Teal Dulcet These header only libraries use [box-drawing](https://en.wikipedia.org/wiki/Box-drawing_character#Unicode), [Braille](https://en.wikipedia.org/wiki/Braille_Patterns), [fraction](https://en.wikipedia.org/wiki/Number_Forms) and other Unicode characters and [terminal colors and formatting](https://misc.flogisoft.com/bash/tip_colors_and_formatting) to output tables and graphs/plots to the console. All the tables and graphs are created with a single (one) function call and they do not require any special data structures. -Please visit [tealdulcet.com](https://www.tealdulcet.com/) to support these libraries and my other software development. +❤️ Please visit [tealdulcet.com](https://www.tealdulcet.com/) to support these libraries and my other software development. ## Tables @@ -233,14 +233,14 @@ int main() Option: `headerrow`\ Default value: `false` -Header rows are bolded and centered. +Header rows are bolded, centered and have a border. #### Header column Option: `headercolumn`\ Default value: `false` -Header columns are bolded and centered. +Header columns are bolded, centered and have a border. #### Table border @@ -252,8 +252,6 @@ Default value: `true` Option: `cellborder`\ Default value: `false` -Requires `tableborder` to be `true`. - #### Cell padding Option: `padding`\ diff --git a/graphs.hpp b/graphs.hpp index 751b8de..4277c73 100644 --- a/graphs.hpp +++ b/graphs.hpp @@ -334,7 +334,7 @@ int graph(const size_t height, const size_t width, const long double xmin, const } else if (axaxis) { - if (axisunitslabel) + if (axislabel and axisunitslabel) { int adivisor = divisor; if (i < yaxis) @@ -362,7 +362,7 @@ int graph(const size_t height, const size_t width, const long double xmin, const } else if (ayaxis) { - if (axisunitslabel) + if (axislabel and axisunitslabel) { int adivisor = divisor; if (j < xaxis) @@ -388,7 +388,7 @@ int graph(const size_t height, const size_t width, const long double xmin, const output = true; } } - else if (yaxislabel and xaxislabel and axisunitslabel) + else if (yaxislabel and xaxislabel and axislabel and axisunitslabel) { cout << "0"; output = true; @@ -461,9 +461,9 @@ int graph(const size_t height, const size_t width, const long double xmin, const unsigned int dot = 0; unsigned short color = 0; - for (int k = 0; k < 2 and (j + k) < width; ++k) + for (unsigned int k = 0; k < 2 and k < (width - j); ++k) { - for (int l = 0; l < 4 and (i + l) < height; ++l) + for (unsigned int l = 0; l < 4 and l < (height - i); ++l) { dot += (array[j + k][i + l] ? 1 : 0) * values[k][l]; if (color) diff --git a/tables.hpp b/tables.hpp index e5f6cd1..4a363ec 100644 --- a/tables.hpp +++ b/tables.hpp @@ -193,10 +193,10 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt struct winsize w; ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - if (tableborder and (cellborder or headerrow or headercolumn)) - width += (((2 * padding) + 1) * columns) + 1; + if (tableborder or cellborder or headerrow or headercolumn) + width += (((2 * padding) + 1) * columns) + (tableborder ? 1 : -1); else - width += ((2 * padding) * columns) + 2; + width += (2 * padding) * columns; if (width > w.ws_col) { @@ -229,18 +229,16 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt { for (unsigned int j = 0; j < columns; ++j) { - if (tableborder) - { - if (j == 0 or cellborder or (i == 0 and headerrow) or (j == 1 and headercolumn)) - cout << styles[style][1]; - else - cout << " "; - } + if ((j == 0 and tableborder) or (j > 0 and cellborder) or (i == 0 and j > 0 and headerrow) or (j == 1 and headercolumn)) + cout << styles[style][1]; + else if (tableborder or (i > 0 and j > 0 and headerrow) or (j > 1 and headercolumn)) + cout << " "; + + const int difference = columnwidth[j] - strcol(array[i][j]); if ((i == 0 and headerrow) or (j == 0 and headercolumn)) { - int difference = columnwidth[j] - strcol(array[i][j]); - int apadding = (difference / 2); + const int apadding = (difference / 2); for (unsigned int k = 0; k < padding + apadding; ++k) cout << " "; @@ -255,7 +253,7 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt for (unsigned int k = 0; k < padding; ++k) cout << " "; - cout << aoptions.alignment << setw(columnwidth[j]) << array[i][j]; + cout << aoptions.alignment << setw(difference + strlen(array[i][j])) << array[i][j]; for (unsigned int k = 0; k < padding; ++k) cout << " "; @@ -273,42 +271,47 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt cout << styles[style][8]; else if (cellborder or (i == 0 and headerrow) or headercolumn) cout << styles[style][5]; + } + if ((i == (rows - 1) and tableborder) or (i < (rows - 1) and cellborder) or (i == 0 and headerrow) or (i < (rows - 1) and headercolumn)) + { for (unsigned int j = 0; j < columns; ++j) { - if (cellborder or i == (rows - 1) or (i == 0 and headerrow) or (j == 0 and headercolumn)) + if ((i == (rows - 1) and tableborder) or (i < (rows - 1) and cellborder) or (i == 0 and headerrow) or (i < (rows - 1) and j == 0 and headercolumn)) for (unsigned int k = 0; k < (2 * padding) + columnwidth[j]; ++k) cout << styles[style][0]; - else if (headercolumn) + else if (i < (rows - 1) and headercolumn) for (unsigned int k = 0; k < (2 * padding) + columnwidth[j]; ++k) cout << " "; if (j == (columns - 1)) { - if (i == (rows - 1)) - cout << styles[style][10]; - else if (cellborder or (i == 0 and headerrow)) - cout << styles[style][7]; - else if (headercolumn) - cout << styles[style][1]; + if (tableborder) + { + if (i == (rows - 1)) + cout << styles[style][10]; + else if (cellborder or (i == 0 and headerrow)) + cout << styles[style][7]; + else if (headercolumn) + cout << styles[style][1]; + } - if (cellborder or (i == 0 and headerrow) or headercolumn) - cout << "\n"; + cout << "\n"; } else { - if (i == (rows - 1)) + if (i == (rows - 1) and tableborder) { if (cellborder or (j == 0 and headercolumn)) cout << styles[style][9]; else cout << styles[style][0]; } - else if (cellborder or ((i == 0 and headerrow) and (j == 0 and headercolumn))) + else if ((i < (rows - 1) and cellborder) or ((i == 0 and headerrow) and (j == 0 and headercolumn))) cout << styles[style][6]; else if (i == 0 and headerrow) cout << styles[style][9]; - else if (headercolumn) + else if (i < (rows - 1) and headercolumn) { if (j == 0) cout << styles[style][7]; @@ -433,12 +436,12 @@ int table(const long double xmin, const long double xmax, const long double xscl const char *const aheaderrow[] = {"x", "y"}; // const char* const aheaderrow[] = {"", "x", "y"}; + const size_t length = (sizeof aheaderrow / sizeof aheaderrow[0]); + char **headerrow = new char *[columns]; for (unsigned int j = 0; j < columns; ++j) { - const size_t length = (sizeof aheaderrow / sizeof aheaderrow[0]); - if (j < (length - 1) or numfunctions == 1) { headerrow[j] = new char[strlen(aheaderrow[j]) + 1];