save game

This commit is contained in:
Gerrit Viljoen 2020-05-16 21:53:43 +02:00
parent 71f651f36a
commit 1f5dab68d2
3 changed files with 37 additions and 1 deletions

View File

@ -12,3 +12,6 @@ documentation = "https://docs.rs/ascii_table"
readme = "readme.md" readme = "readme.md"
categories = ["command-line-interface"] categories = ["command-line-interface"]
keywords = ["ascii", "table"] keywords = ["ascii", "table"]
[dev-dependencies]
colorful = "0.2"

View File

@ -225,13 +225,17 @@ impl AsciiTable {
let result: Vec<_> = (0..num_cols).map(|a| { let result: Vec<_> = (0..num_cols).map(|a| {
let default_conf = &DEFAULT_COLUMN; let default_conf = &DEFAULT_COLUMN;
let conf = self.columns.get(&a).unwrap_or(default_conf); let conf = self.columns.get(&a).unwrap_or(default_conf);
let column_width = data.iter().map(|row| row[a].chars().count()).max().unwrap(); let column_width = data.iter().map(|row| Self::count_characters(&row[a])).max().unwrap();
let header_width = conf.header.chars().count(); let header_width = conf.header.chars().count();
column_width.max(header_width).min(conf.max_width) column_width.max(header_width).min(conf.max_width)
}).collect(); }).collect();
self.truncate_widths(result) self.truncate_widths(result)
} }
fn count_characters(cell: &str) -> usize {
cell.chars().count()
}
fn truncate_widths(&self, mut widths: Vec<usize>) -> Vec<usize> { fn truncate_widths(&self, mut widths: Vec<usize>) -> Vec<usize> {
let max_width = self.max_width; let max_width = self.max_width;
let table_padding = Self::smallest_width(widths.len()); let table_padding = Self::smallest_width(widths.len());

View File

@ -15,9 +15,13 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with ascii-table. If not, see <http://www.gnu.org/licenses/>. // along with ascii-table. If not, see <http://www.gnu.org/licenses/>.
use colorful::Color;
use colorful::Colorful;
use crate::{AsciiTable, Column}; use crate::{AsciiTable, Column};
use crate::Align::*; use crate::Align::*;
use std::collections::BTreeMap;
use std::fmt::Display; use std::fmt::Display;
fn cube_config() -> AsciiTable { fn cube_config() -> AsciiTable {
@ -28,6 +32,19 @@ fn cube_config() -> AsciiTable {
result result
} }
#[test]
fn backwards_compatible() {
AsciiTable {
max_width: 0,
columns: BTreeMap::new()
};
Column {
header: String::new(),
align: Left,
max_width: 0
};
}
#[test] #[test]
fn empty_rows() { fn empty_rows() {
let config = AsciiTable::default(); let config = AsciiTable::default();
@ -504,3 +521,15 @@ fn mixed_types() {
assert_eq!(expected, config.format(input)); assert_eq!(expected, config.format(input));
} }
#[test]
fn color_codes() {
let config = AsciiTable::default();
let text = "Hello".color(Color::Blue).bg_color(Color::Yellow).bold();
let input = vec![vec![text]];
let expected = "┌───────┐\n\
\u{1b}[38;5;4m\u{1b}[48;5;3;1mHello\u{1b}[0m \n\
\n";
assert_eq!(expected, config.format(input));
}