cli: improve Author labels

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:

* `(<alias>, (you))` -- the `Author` is the local peer and has an alias
* `(<did>, (you))` -- the `Author` is the local peer and has no alias
* `(<alias>, <did>)` -- the `Author` is another peer and has an alias
* `(<blank>, <did>)` -- 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 <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2024-01-04 14:36:03 +00:00 committed by cloudhead
parent 93a6f90d6e
commit 8e0e1c232d
No known key found for this signature in database
3 changed files with 27 additions and 15 deletions

View File

@ -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:
///
/// * `(<alias>, (you))` -- the `Author` is the local peer and has an alias
/// * `(<did>, (you))` -- the `Author` is the local peer and has no alias
/// * `(<alias>, <did>)` -- the `Author` is another peer and has an alias
/// * `(<blank>, <did>)` -- 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])
}
}

View File

@ -186,6 +186,10 @@ impl Line {
pub fn spaced(items: impl IntoIterator<Item = Label>) -> 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());
}

View File

@ -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()