2021-12-01 19:20:23 +00:00
|
|
|
use nu_protocol::Config;
|
2022-10-03 16:40:16 +00:00
|
|
|
use nu_table::{Alignments, Table, TableTheme, TextStyle};
|
2020-09-01 05:09:55 +00:00
|
|
|
use std::collections::HashMap;
|
2022-10-03 16:40:16 +00:00
|
|
|
use tabled::papergrid::records::{cell_info::CellInfo, tcell::TCell};
|
2020-06-20 03:41:53 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args: Vec<_> = std::env::args().collect();
|
2021-02-19 08:40:53 +00:00
|
|
|
let mut width = 0;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
// Width in terminal characters
|
|
|
|
width = args[1].parse::<usize>().expect("Need a width in columns");
|
|
|
|
}
|
|
|
|
|
|
|
|
if width < 4 {
|
|
|
|
println!("Width must be greater than or equal to 4, setting width to 80");
|
|
|
|
width = 80;
|
|
|
|
}
|
2020-06-20 03:41:53 +00:00
|
|
|
|
2021-02-16 15:47:47 +00:00
|
|
|
// The mocked up table data
|
|
|
|
let (table_headers, row_data) = make_table_data();
|
|
|
|
// The table headers
|
|
|
|
let headers = vec_of_str_to_vec_of_styledstr(&table_headers, true);
|
|
|
|
// The table rows
|
|
|
|
let rows = vec_of_str_to_vec_of_styledstr(&row_data, false);
|
|
|
|
// The table itself
|
2022-10-03 16:40:16 +00:00
|
|
|
let count_cols = std::cmp::max(rows.len(), headers.len());
|
|
|
|
let mut rows = vec![rows; 3];
|
|
|
|
rows.insert(0, headers);
|
|
|
|
let table = Table::new(rows, (3, count_cols), width, true, false);
|
2020-09-01 05:09:55 +00:00
|
|
|
// FIXME: Config isn't available from here so just put these here to compile
|
2021-02-22 18:33:34 +00:00
|
|
|
let color_hm: HashMap<String, nu_ansi_term::Style> = HashMap::new();
|
2021-12-01 19:20:23 +00:00
|
|
|
// get the default config
|
|
|
|
let config = Config::default();
|
2021-09-10 02:27:12 +00:00
|
|
|
// Capture the table as a string
|
2022-07-14 20:24:32 +00:00
|
|
|
let output_table = table
|
2022-10-03 16:40:16 +00:00
|
|
|
.draw_table(
|
|
|
|
&config,
|
|
|
|
&color_hm,
|
|
|
|
Alignments::default(),
|
|
|
|
&TableTheme::rounded(),
|
|
|
|
width,
|
2022-11-16 14:03:56 +00:00
|
|
|
false,
|
2022-10-03 16:40:16 +00:00
|
|
|
)
|
2022-07-06 19:57:40 +00:00
|
|
|
.unwrap_or_else(|| format!("Couldn't fit table into {} columns!", width));
|
2021-09-10 02:27:12 +00:00
|
|
|
// Draw the table
|
|
|
|
println!("{}", output_table)
|
2021-02-16 15:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) {
|
2021-03-26 08:26:57 +00:00
|
|
|
let table_headers = vec![
|
|
|
|
"category",
|
|
|
|
"description",
|
|
|
|
"emoji",
|
|
|
|
"ios_version",
|
|
|
|
"unicode_version",
|
|
|
|
"aliases",
|
|
|
|
"tags",
|
|
|
|
"category2",
|
|
|
|
"description2",
|
|
|
|
"emoji2",
|
|
|
|
"ios_version2",
|
|
|
|
"unicode_version2",
|
|
|
|
"aliases2",
|
|
|
|
"tags2",
|
|
|
|
];
|
2021-02-16 15:47:47 +00:00
|
|
|
|
2021-03-26 08:26:57 +00:00
|
|
|
let row_data = vec![
|
|
|
|
"Smileys & Emotion",
|
|
|
|
"grinning face",
|
|
|
|
"😀",
|
|
|
|
"6",
|
|
|
|
"6.1",
|
|
|
|
"grinning",
|
|
|
|
"smile",
|
|
|
|
"Smileys & Emotion",
|
|
|
|
"grinning face",
|
|
|
|
"😀",
|
|
|
|
"6",
|
|
|
|
"6.1",
|
|
|
|
"grinning",
|
|
|
|
"smile",
|
|
|
|
];
|
2021-02-16 15:47:47 +00:00
|
|
|
|
|
|
|
(table_headers, row_data)
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:40:16 +00:00
|
|
|
fn vec_of_str_to_vec_of_styledstr(
|
|
|
|
data: &[&str],
|
|
|
|
is_header: bool,
|
|
|
|
) -> Vec<TCell<CellInfo<'static>, TextStyle>> {
|
2021-02-16 15:47:47 +00:00
|
|
|
let mut v = vec![];
|
|
|
|
|
2021-09-09 22:44:22 +00:00
|
|
|
for x in data {
|
2021-02-16 15:47:47 +00:00
|
|
|
if is_header {
|
2022-10-03 16:40:16 +00:00
|
|
|
v.push(Table::create_cell(
|
2021-02-16 15:47:47 +00:00
|
|
|
String::from(*x),
|
|
|
|
TextStyle::default_header(),
|
|
|
|
))
|
|
|
|
} else {
|
2022-10-03 16:40:16 +00:00
|
|
|
v.push(Table::create_cell(
|
|
|
|
String::from(*x),
|
|
|
|
TextStyle::basic_left(),
|
|
|
|
))
|
2021-02-16 15:47:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
v
|
2020-06-20 03:41:53 +00:00
|
|
|
}
|