Updated table library to support cell borders without also requiring table borders.

This commit is contained in:
Teal Dulcet 2021-09-14 03:03:02 -07:00
parent f0e9e1f0c3
commit 51fe9094e2
3 changed files with 40 additions and 39 deletions

View File

@ -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 # 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. 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 ## Tables
@ -233,14 +233,14 @@ int main()
Option: `headerrow`\ Option: `headerrow`\
Default value: `false` Default value: `false`
Header rows are bolded and centered. Header rows are bolded, centered and have a border.
#### Header column #### Header column
Option: `headercolumn`\ Option: `headercolumn`\
Default value: `false` Default value: `false`
Header columns are bolded and centered. Header columns are bolded, centered and have a border.
#### Table border #### Table border
@ -252,8 +252,6 @@ Default value: `true`
Option: `cellborder`\ Option: `cellborder`\
Default value: `false` Default value: `false`
Requires `tableborder` to be `true`.
#### Cell padding #### Cell padding
Option: `padding`\ Option: `padding`\

View File

@ -334,7 +334,7 @@ int graph(const size_t height, const size_t width, const long double xmin, const
} }
else if (axaxis) else if (axaxis)
{ {
if (axisunitslabel) if (axislabel and axisunitslabel)
{ {
int adivisor = divisor; int adivisor = divisor;
if (i < yaxis) 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) else if (ayaxis)
{ {
if (axisunitslabel) if (axislabel and axisunitslabel)
{ {
int adivisor = divisor; int adivisor = divisor;
if (j < xaxis) if (j < xaxis)
@ -388,7 +388,7 @@ int graph(const size_t height, const size_t width, const long double xmin, const
output = true; output = true;
} }
} }
else if (yaxislabel and xaxislabel and axisunitslabel) else if (yaxislabel and xaxislabel and axislabel and axisunitslabel)
{ {
cout << "0"; cout << "0";
output = true; 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 int dot = 0;
unsigned short color = 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]; dot += (array[j + k][i + l] ? 1 : 0) * values[k][l];
if (color) if (color)

View File

@ -193,10 +193,10 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt
struct winsize w; struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if (tableborder and (cellborder or headerrow or headercolumn)) if (tableborder or cellborder or headerrow or headercolumn)
width += (((2 * padding) + 1) * columns) + 1; width += (((2 * padding) + 1) * columns) + (tableborder ? 1 : -1);
else else
width += ((2 * padding) * columns) + 2; width += (2 * padding) * columns;
if (width > w.ws_col) 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) for (unsigned int j = 0; j < columns; ++j)
{ {
if (tableborder) 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];
if (j == 0 or cellborder or (i == 0 and headerrow) or (j == 1 and headercolumn)) else if (tableborder or (i > 0 and j > 0 and headerrow) or (j > 1 and headercolumn))
cout << styles[style][1]; cout << " ";
else
cout << " "; const int difference = columnwidth[j] - strcol(array[i][j]);
}
if ((i == 0 and headerrow) or (j == 0 and headercolumn)) if ((i == 0 and headerrow) or (j == 0 and headercolumn))
{ {
int difference = columnwidth[j] - strcol(array[i][j]); const int apadding = (difference / 2);
int apadding = (difference / 2);
for (unsigned int k = 0; k < padding + apadding; ++k) for (unsigned int k = 0; k < padding + apadding; ++k)
cout << " "; 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) for (unsigned int k = 0; k < padding; ++k)
cout << " "; 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) for (unsigned int k = 0; k < padding; ++k)
cout << " "; cout << " ";
@ -273,42 +271,47 @@ int table(const size_t rows, const size_t columns, char ***array, const tableopt
cout << styles[style][8]; cout << styles[style][8];
else if (cellborder or (i == 0 and headerrow) or headercolumn) else if (cellborder or (i == 0 and headerrow) or headercolumn)
cout << styles[style][5]; 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) 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) for (unsigned int k = 0; k < (2 * padding) + columnwidth[j]; ++k)
cout << styles[style][0]; 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) for (unsigned int k = 0; k < (2 * padding) + columnwidth[j]; ++k)
cout << " "; cout << " ";
if (j == (columns - 1)) if (j == (columns - 1))
{ {
if (i == (rows - 1)) if (tableborder)
cout << styles[style][10]; {
else if (cellborder or (i == 0 and headerrow)) if (i == (rows - 1))
cout << styles[style][7]; cout << styles[style][10];
else if (headercolumn) else if (cellborder or (i == 0 and headerrow))
cout << styles[style][1]; 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 else
{ {
if (i == (rows - 1)) if (i == (rows - 1) and tableborder)
{ {
if (cellborder or (j == 0 and headercolumn)) if (cellborder or (j == 0 and headercolumn))
cout << styles[style][9]; cout << styles[style][9];
else else
cout << styles[style][0]; 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]; cout << styles[style][6];
else if (i == 0 and headerrow) else if (i == 0 and headerrow)
cout << styles[style][9]; cout << styles[style][9];
else if (headercolumn) else if (i < (rows - 1) and headercolumn)
{ {
if (j == 0) if (j == 0)
cout << styles[style][7]; 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 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]; char **headerrow = new char *[columns];
for (unsigned int j = 0; j < columns; ++j) for (unsigned int j = 0; j < columns; ++j)
{ {
const size_t length = (sizeof aheaderrow / sizeof aheaderrow[0]);
if (j < (length - 1) or numfunctions == 1) if (j < (length - 1) or numfunctions == 1)
{ {
headerrow[j] = new char[strlen(aheaderrow[j]) + 1]; headerrow[j] = new char[strlen(aheaderrow[j]) + 1];