refactor table system (#132)

* init table trait

* add shrink_col_index and max_width method to Table trait

* make unicodes work when shrinking

* improve readability of the table

* replace old table system with new one

* update changelog
This commit is contained in:
Clément DOUIN
2021-04-27 14:54:53 +02:00
committed by GitHub
parent cb296a5d98
commit cddb7bde37
8 changed files with 348 additions and 278 deletions
+20 -26
View File
@@ -4,7 +4,7 @@ use std::fmt;
use crate::{
output::fmt::{get_output_fmt, OutputFmt, Response},
table::{self, DisplayRow, DisplayTable},
table::{Cell, Row, Table},
};
// Mbox
@@ -26,15 +26,25 @@ impl Mbox {
}
}
impl DisplayRow for Mbox {
fn to_row(&self) -> Vec<table::Cell> {
use crate::table::*;
impl Table for Mbox {
fn head() -> Row {
Row::new()
.cell(Cell::new("DELIM").bold().underline().white())
.cell(Cell::new("NAME").bold().underline().white())
.cell(
Cell::new("ATTRIBUTES")
.shrinkable()
.bold()
.underline()
.white(),
)
}
vec![
Cell::new(&[BLUE], &self.delim),
Cell::new(&[GREEN], &self.name),
FlexCell::new(&[YELLOW], &self.attributes.join(", ")),
]
fn row(&self) -> Row {
Row::new()
.cell(Cell::new(&self.delim).red())
.cell(Cell::new(&self.name).green())
.cell(Cell::new(&self.attributes.join(", ")).shrinkable().yellow())
}
}
@@ -43,28 +53,12 @@ impl DisplayRow for Mbox {
#[derive(Debug, Serialize)]
pub struct Mboxes(pub Vec<Mbox>);
impl<'a> DisplayTable<'a, Mbox> for Mboxes {
fn header_row() -> Vec<table::Cell> {
use crate::table::*;
vec![
Cell::new(&[BOLD, UNDERLINE, WHITE], "DELIM"),
Cell::new(&[BOLD, UNDERLINE, WHITE], "NAME"),
FlexCell::new(&[BOLD, UNDERLINE, WHITE], "ATTRIBUTES"),
]
}
fn rows(&self) -> &Vec<Mbox> {
&self.0
}
}
impl fmt::Display for Mboxes {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
unsafe {
match get_output_fmt() {
&OutputFmt::Plain => {
writeln!(f, "\n{}", self.to_table())
writeln!(f, "\n{}", Table::render(&self.0))
}
&OutputFmt::Json => {
let res = serde_json::to_string(&Response::new(self)).unwrap();