use std::fmt; use localtime::LocalTime; pub use radicle_term::format::*; pub use radicle_term::{style, Paint}; use radicle::cob::{ObjectId, Timestamp}; use radicle::identity::Visibility; use radicle::node::{Alias, AliasStore, NodeId}; use radicle::prelude::Did; use radicle::profile::Profile; use radicle_term::element::Line; use crate::terminal as term; /// Format a node id to be more compact. pub fn node(node: &NodeId) -> Paint { let node = node.to_human(); let start = node.chars().take(7).collect::(); let end = node.chars().skip(node.len() - 7).collect::(); Paint::new(format!("{start}…{end}")) } /// Format a git Oid. pub fn oid(oid: impl Into) -> Paint { Paint::new(format!("{:.7}", oid.into())) } /// Wrap parenthesis around styled input, eg. `"input"` -> `"(input)"`. pub fn parens(input: Paint) -> Paint { Paint::new(format!("({})", input.item)).with_style(input.style) } /// Wrap spaces around styled input, eg. `"input"` -> `" input "`. pub fn spaced(input: Paint) -> Paint { Paint::new(format!(" {} ", input.item)).with_style(input.style) } /// Format a command suggestion, eg. `rad init`. pub fn command(cmd: D) -> Paint { primary(format!("`{cmd}`")) } /// Format a COB id. pub fn cob(id: &ObjectId) -> String { format!("{:.7}", id.to_string()) } /// Format a DID. pub fn did(did: &Did) -> Paint { let nid = did.as_key().to_human(); Paint::new(format!("{}…{}", &nid[..7], &nid[nid.len() - 7..])) } /// Format a Visibility. pub fn visibility(v: &Visibility) -> Paint<&str> { match v { Visibility::Public => term::format::positive("public"), Visibility::Private { .. } => term::format::secondary("private"), } } /// Format a timestamp. pub fn timestamp(time: impl Into) -> Paint { let time: LocalTime = time.into(); let now: LocalTime = Timestamp::now().into(); let duration = now - time; let fmt = timeago::Formatter::new(); Paint::new(fmt.convert(duration.into())) } /// Identity formatter that takes a profile and displays it as /// ` ()` depending on the configuration. pub struct Identity<'a> { profile: &'a Profile, /// If true, node id is printed in its compact form. short: bool, /// If true, node id and username are printed using the terminal's /// styled formatters. styled: bool, } impl<'a> Identity<'a> { pub fn new(profile: &'a Profile) -> Self { Self { profile, short: false, styled: false, } } pub fn short(mut self) -> Self { self.short = true; self } pub fn styled(mut self) -> Self { self.styled = true; self } } impl<'a> fmt::Display for Identity<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let nid = self.profile.id(); let alias = self.profile.aliases().alias(nid); let node_id = match self.short { true => self::node(nid).to_string(), false => nid.to_human(), }; if self.styled { write!(f, "{}", term::format::highlight(node_id))?; if let Some(a) = alias { write!(f, " {}", term::format::parens(term::format::dim(a)))?; } } else { write!(f, "{node_id}")?; if let Some(a) = alias { write!(f, " ({a})")?; } } Ok(()) } } /// This enum renders (nid, alias) in terminal depending on user variant. pub struct Author<'a> { nid: &'a NodeId, alias: Option, you: bool, } impl<'a> Author<'a> { pub fn new(nid: &'a NodeId, profile: &Profile) -> Author<'a> { let alias = profile.alias(nid); Self { nid, alias, you: nid == profile.id(), } } pub fn alias(&self) -> Option { self.alias.as_ref().map(|a| a.to_string().into()) } pub fn you(&self) -> Option { if self.you { Some(term::format::primary("(you)").dim().italic().into()) } else { None } } /// Get the labels of the `Author`. The labels can take the following forms: /// /// * `(, (you))` -- the `Author` is the local peer and has an alias /// * `(, (you))` -- the `Author` is the local peer and has no alias /// * `(, )` -- the `Author` is another peer and has an alias /// * `(, )` -- the `Author` is another peer and has no alias pub fn labels(self) -> (term::Label, term::Label) { let alias = match self.alias.as_ref() { Some(alias) => term::format::primary(alias).into(), None if self.you => term::format::primary(term::format::node(self.nid)) .dim() .into(), None => term::Label::blank(), }; let author = self.you().unwrap_or_else(|| { term::format::primary(term::format::node(self.nid)) .dim() .into() }); (alias, author) } pub fn line(self) -> Line { let (alias, author) = self.labels(); Line::spaced([alias, author]) } } /// HTML-related formatting. pub mod html { /// Comment a string with HTML comments. pub fn commented(s: &str) -> String { format!("") } /// Remove html style comments from a string. /// /// The HTML comments must start at the beginning of a line and stop at the end. pub fn strip_comments(s: &str) -> String { let ends_with_newline = s.ends_with('\n'); let mut is_comment = false; let mut w = String::new(); for line in s.lines() { if is_comment { if line.ends_with("-->") { is_comment = false; } continue; } else if line.starts_with(""; let exp = "\ commit 2\n\ "; let res = strip_comments(test); assert_eq!(exp, res); let test = "\ commit 2\n\ -->"; let exp = "\ commit 2\n\ -->"; let res = strip_comments(test); assert_eq!(exp, res); let test = "\ \n\ -->"; let exp = "\ commit 2\n\ \n\ -->"; let res = strip_comments(test); assert_eq!(exp, res); } }