mirror of
https://gitlab.com/arnekeller/ascii-table.git
synced 2024-12-04 13:39:06 +00:00
save game
This commit is contained in:
parent
71f651f36a
commit
1f5dab68d2
@ -12,3 +12,6 @@ documentation = "https://docs.rs/ascii_table"
|
||||
readme = "readme.md"
|
||||
categories = ["command-line-interface"]
|
||||
keywords = ["ascii", "table"]
|
||||
|
||||
[dev-dependencies]
|
||||
colorful = "0.2"
|
||||
|
@ -225,13 +225,17 @@ impl AsciiTable {
|
||||
let result: Vec<_> = (0..num_cols).map(|a| {
|
||||
let default_conf = &DEFAULT_COLUMN;
|
||||
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();
|
||||
column_width.max(header_width).min(conf.max_width)
|
||||
}).collect();
|
||||
self.truncate_widths(result)
|
||||
}
|
||||
|
||||
fn count_characters(cell: &str) -> usize {
|
||||
cell.chars().count()
|
||||
}
|
||||
|
||||
fn truncate_widths(&self, mut widths: Vec<usize>) -> Vec<usize> {
|
||||
let max_width = self.max_width;
|
||||
let table_padding = Self::smallest_width(widths.len());
|
||||
|
29
src/test.rs
29
src/test.rs
@ -15,9 +15,13 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with ascii-table. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use colorful::Color;
|
||||
use colorful::Colorful;
|
||||
|
||||
use crate::{AsciiTable, Column};
|
||||
use crate::Align::*;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt::Display;
|
||||
|
||||
fn cube_config() -> AsciiTable {
|
||||
@ -28,6 +32,19 @@ fn cube_config() -> AsciiTable {
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backwards_compatible() {
|
||||
AsciiTable {
|
||||
max_width: 0,
|
||||
columns: BTreeMap::new()
|
||||
};
|
||||
Column {
|
||||
header: String::new(),
|
||||
align: Left,
|
||||
max_width: 0
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_rows() {
|
||||
let config = AsciiTable::default();
|
||||
@ -504,3 +521,15 @@ fn mixed_types() {
|
||||
|
||||
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));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user