Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: improve Author labels
Fintan Halpenny committed 2 years ago
commit 8e0e1c232d14459ec19ada8cd937624880877710
parent 93a6f90d6e7a973228895beecd1d0a2fab0ff2a6
3 files changed +27 -15
modified 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:
+
    ///
+
    ///   * `(<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])
    }
}

modified radicle-term/src/element.rs
@@ -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());
        }
modified 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()