use std::path::Path;
use std::time::Duration;
use nu_ansi_term::{Color, Style};
use crate::args;
use crate::files;
use crate::lines::paint;
#[derive(Debug, Default)]
pub(crate) struct Shown {
pub(crate) dir: String,
pub(crate) new_store: bool,
pub(crate) tokens: Vec<String>,
pub(crate) node: Option<NodeShown>,
}
#[derive(Debug, Default)]
pub(crate) struct NodeShown {
pub(crate) listen: Option<String>,
pub(crate) from: Vec<String>,
pub(crate) files: Option<Vec<String>>,
pub(crate) catalog: bool,
pub(crate) advertise: Option<String>,
pub(crate) failover: Option<String>,
pub(crate) synchronous_with: Vec<String>,
pub(crate) synchronous: bool,
pub(crate) tls: bool,
pub(crate) config: Option<String>,
}
impl Shown {
pub(crate) fn of(serve: &args::Serve) -> Shown {
let new_store = std::fs::read_dir(&serve.dir).map_or(true, |mut entries| entries.next().is_none());
let tokens = serve.remote.as_ref().map(|remote| capabilities(&remote.tokens)).unwrap_or_default();
let node = serve.node.as_ref().map(|node| {
let settings = node.settings();
NodeShown {
listen: node.listen.clone(),
from: node.from.clone(),
files: settings.files.clone(),
catalog: settings.catalog,
advertise: settings.advertise.clone(),
failover: settings.failover.clone(),
synchronous_with: settings.synchronous_with.clone(),
synchronous: settings.synchronous,
tls: settings.tls.is_some(),
config: node
.config
.as_ref()
.and_then(|(file, _)| file.file_name())
.map(|name| name.to_string_lossy().into_owned()),
}
});
Shown { dir: serve.dir.display().to_string(), new_store, tokens, node }
}
}
fn capabilities(tokens: &Path) -> Vec<String> {
let read = std::fs::read_to_string(tokens).unwrap_or_default();
let lines = read.lines().map(str::trim).filter(|line| !line.is_empty() && !line.starts_with('#'));
lines.filter_map(|line| line.split_whitespace().next()).map(str::to_owned).collect()
}
pub(crate) fn text(colours: bool, shown: &Shown, store: &Path, endpoints: &[String], ready: Duration) -> String {
let paint = |style: Style, text: &str| paint(colours, style, text);
let (bold, dim, on, address) =
(Style::new().bold(), Style::new().dimmed(), Color::Green.bold(), Color::Cyan.normal());
let mut banner = heading(colours, shown);
let mut row = |lit: bool, label: &str, what: String, how: String| {
let (dot, label) = match (label.is_empty(), lit) {
(true, _) => (" ".to_owned(), " ".repeat(8)),
(false, true) => (paint(on, "●"), paint(bold, &format!("{label:<8}"))),
(false, false) => (paint(dim, "○"), paint(dim, &format!("{label:<8}"))),
};
banner.push_str(&format!(" {dot} {label} {what} {how}\n"));
};
let column = |text: &str, style: Style| {
format!("{}{}", paint(style, text), " ".repeat(28usize.saturating_sub(text.chars().count())))
};
let local = endpoints.first().map_or_else(String::new, |endpoint| local_name(endpoint));
let found = format!("found through {}/server/SERVE", shown.dir.trim_end_matches(['/', '\\']));
row(
true,
"local",
column("programs on this machine", Style::new()),
format!("{} {} {}", paint(address, &local), paint(dim, "·"), paint(dim, &found)),
);
match endpoints.get(1) {
Some(remote) => {
let tokens = match shown.tokens.len() {
0 => paint(Color::Yellow.normal(), "no tokens"),
count => format!(
"{} {}",
count,
paint(dim, &format!("token{}: {}", if count == 1 { "" } else { "s" }, shown.tokens.join(", ")))
),
};
row(true, "remote", column(remote, address), tokens);
}
None => {
row(false, "remote", column("off", dim), paint(dim, "--listen tls://<host>:<port> serves other machines"))
}
}
match &shown.node {
Some(node) => node_rows(&mut row, &column, node, colours),
None => {
row(false, "node", column("off", dim), paint(dim, "--node-listen <host>:<port> lets other nodes copy it"))
}
}
banner.push_str(&footing(colours, shown, store, ready));
banner
}
fn heading(colours: bool, shown: &Shown) -> String {
let paint = |style: Style, text: &str| paint(colours, style, text);
let (bold, dim) = (Style::new().bold(), Style::new().dimmed());
let new = if shown.new_store { format!(" {}", paint(Color::Yellow.normal(), "new store")) } else { String::new() };
format!(
"\n {} {} {} {}{new}\n\n",
paint(bold, "tinystore"),
paint(dim, env!("CARGO_PKG_VERSION")),
paint(dim, "·"),
paint(bold, &shown.dir)
)
}
fn footing(colours: bool, shown: &Shown, store: &Path, ready: Duration) -> String {
let paint = |style: Style, text: &str| paint(colours, style, text);
let dim = Style::new().dimmed();
format!(
"\n {}\n {} {} {} {}\n\n",
files_line(colours, store),
paint(dim, &format!("ready in {} ms · Ctrl+C stops ·", ready.as_millis())),
paint(Style::new(), &format!("tinystore logs {} -f", shown.dir)),
paint(dim, "·"),
paint(Style::new(), &format!("tinystore status {}", shown.dir)),
)
}
fn node_rows(
row: &mut impl FnMut(bool, &str, String, String),
column: &impl Fn(&str, Style) -> String,
node: &NodeShown,
colours: bool,
) {
let paint = |style: Style, text: &str| paint(colours, style, text);
let (dim, address) = (Style::new().dimmed(), Color::Cyan.normal());
let listen = node.listen.as_ref().map_or_else(|| "serves no node".to_owned(), |listen| format!("listen {listen}"));
let mut said = Vec::new();
if let Some(advertise) = &node.advertise {
said.push(format!("advertised as {}", paint(address, advertise)));
}
said.push(if node.tls { "TLS".to_owned() } else { paint(Color::Yellow.normal(), "plain TCP") });
if node.catalog {
said.push("keeps the catalog".to_owned());
}
row(true, "node", column(&listen, address), said.join(&paint(dim, " · ")));
if !node.from.is_empty() {
let files = node.files.as_ref().map_or_else(|| "every file".to_owned(), |files| files.join(", "));
let from = format!("copies {}", node.from.join(", "));
let config = node.config.as_ref().map_or_else(String::new, |config| format!(" ({config})"));
row_more(row, column(&from, Style::new()), paint(dim, &format!("{files}{config}")));
} else if let Some(config) = &node.config {
row_more(row, column("", Style::new()), paint(dim, &format!("settings from {config}")));
}
let mut decides = Vec::new();
if let Some(failover) = &node.failover {
decides.push(format!("failover {}", paint(address, failover)));
}
if !node.synchronous_with.is_empty() {
decides.push(format!("synchronous with {}", node.synchronous_with.join(", ")));
} else if node.synchronous {
decides.push("synchronous with any copy".to_owned());
}
if !decides.is_empty() {
row_more(row, column("", Style::new()), decides.join(&paint(dim, " · ")));
}
}
fn row_more(row: &mut impl FnMut(bool, &str, String, String), what: String, how: String) {
row(true, "", what, how);
}
fn files_line(colours: bool, store: &Path) -> String {
let dim = Style::new().dimmed();
let found = files::of(store);
if found.is_empty() {
return paint(colours, dim, "empty: each engine makes its file the first time a program opens it");
}
let shown: Vec<String> =
found.iter().map(|file| format!("{} {}", file.name, paint(colours, dim, files::bytes(file.bytes)))).collect();
shown.join(&paint(colours, dim, " · "))
}
fn local_name(endpoint: &str) -> String {
if let Some(pipe) = endpoint.strip_prefix("pipe:") {
return format!("pipe {pipe}");
}
if let Some(socket) = endpoint.strip_prefix("unix://") {
return format!("socket {socket}");
}
endpoint.to_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_banner_says_what_is_on_what_is_off_and_how_to_turn_it_on() {
let dir = tempfile::tempdir().unwrap();
let shown = Shown { dir: "./data".to_owned(), new_store: true, ..Shown::default() };
let banner =
text(false, &shown, dir.path(), &["pipe:tinystore-5434ed53".to_owned()], Duration::from_millis(14));
assert!(banner.contains("tinystore") && banner.contains("./data new store"), "{banner}");
assert!(banner.contains("● local") && banner.contains("pipe tinystore-5434ed53"), "{banner}");
assert!(banner.contains("○ remote") && banner.contains("--listen tls://"), "{banner}");
assert!(banner.contains("○ node") && banner.contains("--node-listen"), "{banner}");
assert!(banner.contains("empty") && banner.contains("tinystore logs ./data -f"), "{banner}");
assert!(!banner.contains('\u{1b}'));
}
}