From 8e0e1c232d14459ec19ada8cd937624880877710 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Thu, 4 Jan 2024 14:36:03 +0000 Subject: [PATCH] cli: improve Author labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When an alias cannot be found for an `Author` the DID can be repeated eg. ✓ accepted by z6MkkfM…jXVsVz5 z6MkkfM…jXVsVz5 To prevent this we output one of four cases: * `(, (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 Notably, we output a blank `Label` when no alias was found. Signed-off-by: Fintan Halpenny X-Clacks-Overhead: GNU Terry Pratchett --- radicle-cli/src/terminal/format.rs | 33 ++++++++++++++++-------------- radicle-term/src/element.rs | 4 ++++ radicle-term/src/label.rs | 5 +++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/radicle-cli/src/terminal/format.rs b/radicle-cli/src/terminal/format.rs index 61ddf910..bf80bbac 100644 --- a/radicle-cli/src/terminal/format.rs +++ b/radicle-cli/src/terminal/format.rs @@ -136,7 +136,7 @@ pub struct Author<'a> { impl<'a> Author<'a> { pub fn new(nid: &'a NodeId, profile: &Profile) -> Author<'a> { - let alias = profile.aliases().alias(nid); + let alias = profile.alias(nid); Self { nid, @@ -157,28 +157,31 @@ impl<'a> Author<'a> { } } + /// 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 { + let alias = match self.alias.as_ref() { Some(alias) => term::format::primary(alias).into(), - None => term::format::primary(term::format::node(self.nid)) + None if self.you => term::format::primary(term::format::node(self.nid)) .dim() .into(), + None => term::Label::blank(), }; - if let Some(you) = self.you() { - (alias, you) - } else { - ( - alias, - term::format::primary(term::format::node(self.nid)) - .dim() - .into(), - ) - } + 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 (first, second) = self.labels(); - Line::spaced([first, second]) + let (alias, author) = self.labels(); + Line::spaced([alias, author]) } } diff --git a/radicle-term/src/element.rs b/radicle-term/src/element.rs index 6b504fd9..9432b4d9 100644 --- a/radicle-term/src/element.rs +++ b/radicle-term/src/element.rs @@ -186,6 +186,10 @@ impl Line { pub fn spaced(items: impl IntoIterator) -> Self { let mut line = Self::default(); for item in items.into_iter() { + // Don't create spaces around empty labels. + if item.is_blank() { + continue; + } line.push(item); line.push(Label::space()); } diff --git a/radicle-term/src/label.rs b/radicle-term/src/label.rs index cd43c1f2..10e44058 100644 --- a/radicle-term/src/label.rs +++ b/radicle-term/src/label.rs @@ -17,6 +17,11 @@ impl Label { Self(Paint::default()) } + /// Is this label empty. + pub fn is_blank(&self) -> bool { + self.content().is_empty() + } + /// Get unstyled content. pub fn content(&self) -> &str { self.0.content()