Skip to main content

godwit/iohandler/
printer.rs

1//! Printer
2//!
3//! Controls input and read operations. Utility abstraction over general stdio input/read operations.
4use crate::errors::StateError;
5use crate::statehandler::State;
6use log::info;
7use prettytable::{cell, format, row, Table};
8use std::error::Error;
9
10/// Print pretty state-graphs.
11pub fn print_state_graph(state_list: Vec<State>, verbose: bool) -> Result<(), Box<dyn Error>> {
12	if state_list.is_empty() {
13		info!("No projects added yet. Use the `add` operation.");
14		return Err(StateError::EmptyStateList.into());
15	}
16
17	let mut table = Table::new();
18
19	let format = format::FormatBuilder::new()
20		.column_separator('│')
21		.borders('│')
22		.separator(
23			format::LinePosition::Top,
24			format::LineSeparator::new('─', '┬', '┌', '┐'),
25		)
26		.separator(
27			format::LinePosition::Intern,
28			format::LineSeparator::new('─', '┼', '├', '┤'),
29		)
30		.separator(
31			format::LinePosition::Bottom,
32			format::LineSeparator::new('─', '┴', '└', '┘'),
33		)
34		.padding(1, 1)
35		.build();
36
37	table.set_format(format);
38
39	if verbose {
40		table.set_titles(row![bic => "Project", "Location", "Status"]);
41
42		for state in state_list {
43			table.add_row(row![c =>
44				state.glyph,
45				format!("{}", state.directory.unwrap_or_default().display()),
46				format!("{:?}", state.status.unwrap_or_default()),
47			]);
48		}
49	} else {
50		table.set_titles(row![bic => "Project", "Location"]);
51
52		for state in state_list {
53			table.add_row(row![c =>
54				state.glyph,
55				format!("{}", state.directory.unwrap_or_default().display()),
56			]);
57		}
58	}
59
60	table.printstd();
61	Ok(())
62}