homestar_runtime/cli/
show.rs1use std::{
4 fmt,
5 io::{self, Write},
6};
7use tabled::{
8 settings::{
9 object::Rows,
10 style::{BorderColor, BorderSpanCorrection},
11 Alignment, Color, Modify, Panel, Style,
12 },
13 Table,
14};
15
16pub(crate) const TABLE_TITLE: &str = "homestar(╯°□°)╯";
18
19#[derive(Debug, Clone, PartialEq)]
21pub struct Output(String);
22
23impl fmt::Display for Output {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 write!(f, "{}", self.0.trim_end())
26 }
27}
28
29impl Output {
30 pub(crate) fn new(table: String) -> Self {
32 Self(table)
33 }
34
35 #[allow(dead_code)]
37 pub(crate) fn inner(&self) -> &str {
38 &self.0
39 }
40
41 pub(crate) fn echo(&self) -> Result<(), io::Error> {
43 let stdout = io::stdout();
44 let mut handle = io::BufWriter::new(stdout);
45 writeln!(handle, "{}", self.0)
46 }
47}
48
49pub trait ConsoleTable {
51 fn table(&self) -> Output;
53 fn echo_table(&self) -> Result<(), io::Error>;
55}
56
57#[allow(dead_code)]
59pub(crate) trait ApplyStyle {
60 fn default(&mut self) -> Output;
61 fn default_with_title(&mut self, ext_title: &str) -> Output;
62}
63
64impl ApplyStyle for Table {
65 fn default(&mut self) -> Output {
66 let table = self
67 .with(Style::modern())
68 .with(Panel::header(TABLE_TITLE))
69 .with(Modify::new(Rows::first()).with(Alignment::left()))
70 .with(BorderColor::filled(Color::FG_WHITE))
71 .with(BorderSpanCorrection)
72 .to_string();
73
74 Output(table)
75 }
76
77 fn default_with_title(&mut self, ext_title: &str) -> Output {
78 let table = self
79 .with(Style::modern())
80 .with(Panel::header(format!("{TABLE_TITLE} - {ext_title}")))
81 .with(Modify::new(Rows::first()).with(Alignment::left()))
82 .with(BorderColor::filled(Color::FG_WHITE))
83 .with(BorderSpanCorrection)
84 .to_string();
85
86 Output(table)
87 }
88}