use std::{
fmt,
io::{self, Write},
};
use tabled::{
settings::{
object::Rows,
style::{BorderColor, BorderSpanCorrection},
Alignment, Color, Modify, Panel, Style,
},
Table,
};
pub(crate) const TABLE_TITLE: &str = "homestar(╯°□°)╯";
#[derive(Debug, Clone, PartialEq)]
pub struct Output(String);
impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.trim_end())
}
}
impl Output {
pub(crate) fn new(table: String) -> Self {
Self(table)
}
#[allow(dead_code)]
pub(crate) fn inner(&self) -> &str {
&self.0
}
pub(crate) fn echo(&self) -> Result<(), io::Error> {
let stdout = io::stdout();
let mut handle = io::BufWriter::new(stdout);
writeln!(handle, "{}", self.0)
}
}
pub trait ConsoleTable {
fn table(&self) -> Output;
fn echo_table(&self) -> Result<(), io::Error>;
}
#[allow(dead_code)]
pub(crate) trait ApplyStyle {
fn default(&mut self) -> Output;
fn default_with_title(&mut self, ext_title: &str) -> Output;
}
impl ApplyStyle for Table {
fn default(&mut self) -> Output {
let table = self
.with(Style::modern())
.with(Panel::header(TABLE_TITLE))
.with(Modify::new(Rows::first()).with(Alignment::left()))
.with(BorderColor::filled(Color::FG_WHITE))
.with(BorderSpanCorrection)
.to_string();
Output(table)
}
fn default_with_title(&mut self, ext_title: &str) -> Output {
let table = self
.with(Style::modern())
.with(Panel::header(format!("{TABLE_TITLE} - {ext_title}")))
.with(Modify::new(Rows::first()).with(Alignment::left()))
.with(BorderColor::filled(Color::FG_WHITE))
.with(BorderSpanCorrection)
.to_string();
Output(table)
}
}