jensmemesclient/cli/src/table.rs

46 lines
1.3 KiB
Rust
Raw Normal View History

use chrono::{Local, TimeZone};
2021-04-01 19:12:23 +02:00
use jm_client_core::api::{Category, Meme, User};
use term_table::{row::Row, table_cell::TableCell, Table, TableBuilder, TableStyle};
/// returns an empty table with the correct format settings for lists
pub fn list_table<'a>() -> Table<'a> {
TableBuilder::new()
.style(TableStyle::simple())
.separate_rows(false)
.has_top_boarder(false)
.has_bottom_boarder(false)
.build()
}
2021-05-27 18:01:30 +02:00
pub trait AsTableRow {
fn as_table_row(&self) -> term_table::row::Row;
}
2020-12-05 12:08:24 +01:00
2021-05-27 18:01:30 +02:00
impl AsTableRow for Category {
fn as_table_row(&self) -> Row<'_> {
Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
}
}
2021-05-27 18:01:30 +02:00
impl AsTableRow for Meme {
fn as_table_row(&self) -> Row<'_> {
2020-12-05 22:20:08 +01:00
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
TableCell::new(&self.user),
TableCell::new(Local.timestamp(self.timestamp, 0).format("%F %R")),
2021-06-17 12:02:39 +02:00
TableCell::new(&self.id),
2020-12-05 22:20:08 +01:00
])
}
}
2021-05-27 18:01:30 +02:00
impl AsTableRow for User {
fn as_table_row(&self) -> Row<'_> {
2020-12-19 19:43:23 +01:00
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),
TableCell::new(&self.dayuploads),
])
}
}