1
2
3use std::cmp;
4pub use crate::aggregate::{ Table, TableRow, TableDef, Index, Field };
5pub use crate::log_record::{ LogValueType, LogValue, LogRecord, Accessor };
6
7
8pub enum VisualizeType {
9 Csv,
10 Markdown
11}
12
13pub fn display_as_csv(table: &mut Table) {
14 let def = table.definition.clone();
16 let mut header = String::from("");
17 for (i, &f) in def.field_accessor().iter().enumerate() {
18 header += &f.name;
19 if i != def.field_num() {
20 header += ",";
21 }
22 }
23 println!("{}", &header);
24
25 for (key, row) in table.sorted_rows() {
26 let mut row_str = String::from(key);
27 row_str += ",";
28
29 for (i, v) in row.get_row(&def.fields).iter().enumerate() {
30
31 let s = v.as_string();
32 row_str += &s;
33 if i != def.field_num() {
34 row_str += ",";
35 }
36
37 }
38 println!("{}", &row_str);
39 }
40}
41
42
43pub fn display_as_markdown(table :&mut Table) {
44 let def = table.definition.clone();
47 let mut col_width = vec![10; 1+def.fields.len()];
48 col_width[0] = cmp::max(col_width[0], def.index.name().chars().count());
49 for (i, &f) in def.field_accessor().iter().enumerate() {
50 col_width[i+1] = cmp::max(col_width[i+1], f.name.chars().count());
51 }
52
53 let mut header = String::from("|");
55 header += &format_string(def.index.name(), col_width[0]);
56 header += "|";
57 for (i, &f) in def.field_accessor().iter().enumerate() {
58 let width = col_width[i+1];
59 header += &format_string(&f.name, width);
60 header += "|"
61 }
62 println!("{}", &header);
63
64 let mut separator = String::new();
66 for w in col_width.iter() {
67 separator += &format!("|:{}", "-".repeat(*w-1));
68 }
69 separator += "|";
70 println!("{}", separator);
71
72 for (key, row) in table.sorted_rows() {
74 let width = col_width[0];
75 let mut row_str = String::from("|");
76 row_str += &format_string(key, width);
77 row_str += "|";
78
79 for (i, v) in row.get_row(&def.fields).iter().enumerate() {
80 let width = col_width[i+1];
81 row_str += &format_log_value(v, width);
82 if i != def.field_num() {
83 row_str += "|";
84 }
85
86 }
87 println!("{}", &row_str);
88 }
89}
90
91
92fn format_log_value(v: &LogValue, width: usize) -> String{
93 let s = v.as_string();
94 format!("{:>width$}", &s, width = width)
95}
96
97fn format_string(s: &str, width: usize) -> String{
98 format!("{:width$}", s, width = width)
99}