Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli: Improve formatting of Node IDs and addresses
✗ CI failure Lorenz Leutgeb committed 10 months ago
commit 85ccdfab37e350a35a6779021c3b33796062ffb7
parent 010d5134e8fae35e9e4f179d2929e41a20a93d30
1 failed 1 pending (2 total) View logs
9 files changed +60 -26
modified crates/radicle-cli/src/commands/checkout.rs
@@ -178,7 +178,7 @@ pub fn setup_remote(
        term::success!(
            "Remote-tracking branch {} created for {}",
            term::format::tertiary(branch),
-
            term::format::tertiary(term::format::node(remote_id))
+
            term::format::tertiary(term::format::nid_human(remote_id))
        );
    }
    Ok(remote_name)
modified crates/radicle-cli/src/commands/clone.rs
@@ -355,7 +355,7 @@ fn handle_fetch_error(id: RepoId, failure: &node::sync::fetch::TargetMissed) ->
    for (node, reason) in failure.fetch_results().failed() {
        term::warning(format!(
            "{}: {}",
-
            term::format::node(node),
+
            term::format::nid_human(node),
            term::format::yellow(reason),
        ))
    }
modified crates/radicle-cli/src/commands/node/control.rs
@@ -189,7 +189,7 @@ pub fn connect(
) -> anyhow::Result<()> {
    let spinner = term::spinner(format!(
        "Connecting to {}@{addr}...",
-
        term::format::node(&nid)
+
        term::format::nid_human_compact(&nid)
    ));
    match node.connect(
        nid,
@@ -217,7 +217,7 @@ pub fn connect_many(
    for addr in addrs {
        spinner.message(format!(
            "Connecting to {}@{addr}...",
-
            term::format::node(&nid)
+
            term::format::nid_human_compact(&nid)
        ));
        match node.connect(
            nid,
@@ -353,7 +353,7 @@ pub fn sessions(node: &Node) -> Result<Option<term::Table<5, term::Label>>, node
    table.divider();

    for sess in sessions {
-
        let nid = term::format::tertiary(term::format::node(&sess.nid)).into();
+
        let nid = term::format::tertiary(term::format::nid_human(&sess.nid)).into();
        let (addr, state, time) = match sess.state {
            node::State::Initial => (
                term::Label::blank(),
@@ -361,17 +361,17 @@ pub fn sessions(node: &Node) -> Result<Option<term::Table<5, term::Label>>, node
                term::Label::blank(),
            ),
            node::State::Attempted => (
-
                sess.addr.to_string().into(),
+
                term::format::addr_compact(&sess.addr).into(),
                term::Label::from(state_attempted()),
                term::Label::blank(),
            ),
            node::State::Connected { since, .. } => (
-
                sess.addr.to_string().into(),
+
                term::format::addr_compact(&sess.addr).into(),
                term::Label::from(state_connected()),
                term::format::dim(now - since).into(),
            ),
            node::State::Disconnected { since, .. } => (
-
                sess.addr.to_string().into(),
+
                term::format::addr_compact(&sess.addr).into(),
                term::Label::from(state_disconnected()),
                term::format::dim(now - since).into(),
            ),
modified crates/radicle-cli/src/commands/node/routing.rs
@@ -36,7 +36,7 @@ fn print_table(entries: impl IntoIterator<Item = (RepoId, NodeId)>) {
    for (rid, nid) in entries {
        t.push([
            term::format::highlight(rid.to_string()),
-
            term::format::node(&nid),
+
            term::format::nid_human(&nid),
        ]);
    }
    t.print();
modified crates/radicle-cli/src/commands/sync.rs
@@ -674,8 +674,8 @@ impl FetcherSpinner {
            term::format::secondary(self.preferred_seeds),
            term::format::secondary(progress.succeeded()),
            term::format::secondary(self.replicas.lower_bound()),
-
            term::format::tertiary(term::format::node(node)),
-
            term::format::tertiary(addr),
+
            term::format::tertiary(term::format::nid_human_compact(node)),
+
            term::format::tertiary(term::format::addr_compact(addr)),
        ))
    }

@@ -691,8 +691,8 @@ impl FetcherSpinner {
            term::format::secondary(self.preferred_seeds),
            term::format::secondary(progress.succeeded()),
            term::format::secondary(self.replicas.lower_bound()),
-
            term::format::tertiary(term::format::node(node)),
-
            term::format::tertiary(addr),
+
            term::format::tertiary(term::format::nid_human_compact(node)),
+
            term::format::tertiary(term::format::addr_compact(addr)),
        ))
    }

@@ -731,7 +731,7 @@ impl FetcherSpinner {
        let missing_preferred_seeds = missed
            .missed_nodes()
            .iter()
-
            .map(|nid| term::format::node(nid).to_string())
+
            .map(|nid| term::format::nid_human(nid).to_string())
            .collect::<Vec<_>>();
        let required = missed.required_nodes();
        if !missing_preferred_seeds.is_empty() {
@@ -760,7 +760,7 @@ fn display_fetch_result(result: &sync::FetcherResult, verbose: bool) {
                for (node, reason) in results.failed() {
                    term::warning(format!(
                        "{}: {}",
-
                        term::format::node(node),
+
                        term::format::nid_human(node),
                        term::format::yellow(reason),
                    ))
                }
@@ -784,7 +784,7 @@ fn display_fetch_result(result: &sync::FetcherResult, verbose: bool) {
            for (node, reason) in results.failed() {
                term::error(format!(
                    "{}: {}",
-
                    term::format::node(node),
+
                    term::format::nid_human(node),
                    term::format::negative(reason),
                ))
            }
@@ -803,7 +803,7 @@ fn display_success<'a>(
    for (node, updates, _) in results {
        term::println(
            "🌱 Fetched from",
-
            term::format::secondary(term::format::node(node)),
+
            term::format::secondary(term::format::nid_human(node)),
        );
        if verbose {
            let mut updates = updates
@@ -858,7 +858,7 @@ fn print_announcer_result(result: &sync::AnnouncerResult, verbose: bool) {
            if verbose {
                print_synced(result.synced());
                for node in timed_out {
-
                    term::warning(format!("{} timed out", term::format::node(node)));
+
                    term::warning(format!("{} timed out", term::format::nid_human(node)));
                }
            }
        }
@@ -873,7 +873,7 @@ fn print_announcer_result(result: &sync::AnnouncerResult, verbose: bool) {

fn print_synced(synced: &BTreeMap<NodeId, sync::announce::SyncStatus>) {
    for (node, status) in synced.iter() {
-
        let mut message = format!("🌱 Synced with {}", term::format::node(node));
+
        let mut message = format!("🌱 Synced with {}", term::format::nid_human(node));

        match status {
            sync::announce::SyncStatus::AlreadySynced => {
modified crates/radicle-cli/src/node.rs
@@ -242,7 +242,7 @@ where
    match node.announce(rid, settings.timeout, announcer, |node, progress| {
        spinner.message(format!(
            "Synced with {}, {} of {} preferred seeds, and {} of at least {} replica(s).",
-
            term::format::node(node),
+
            term::format::nid_human_compact(node),
            term::format::secondary(progress.preferred()),
            term::format::secondary(n_preferred_seeds),
            term::format::secondary(progress.synced()),
modified crates/radicle-cli/src/terminal/format.rs
@@ -8,7 +8,7 @@ pub use radicle_term::{style, Paint};
use radicle::cob::ObjectId;
use radicle::identity::Visibility;
use radicle::node::policy::Policy;
-
use radicle::node::{Alias, AliasStore, NodeId};
+
use radicle::node::{Address, Alias, AliasStore, HostName, NodeId};
use radicle::prelude::Did;
use radicle::profile::{env, Profile};
use radicle::storage::RefUpdate;
@@ -17,7 +17,7 @@ use radicle_term::element::Line;
use crate::terminal as term;

/// Format a node id to be more compact.
-
pub fn node(node: &NodeId) -> Paint<String> {
+
pub fn nid_human_compact(node: &NodeId) -> Paint<String> {
    let node = node.to_human();
    let start = node.chars().take(7).collect::<String>();
    let end = node.chars().skip(node.len() - 7).collect::<String>();
@@ -25,6 +25,32 @@ pub fn node(node: &NodeId) -> Paint<String> {
    Paint::new(format!("{start}…{end}"))
}

+
/// Format a node id.
+
pub fn nid_human(node: &NodeId) -> Paint<String> {
+
    Paint::new(node.to_human())
+
}
+

+
pub fn addr_compact(address: &Address) -> Paint<String> {
+
    let host = match address.host() {
+
        HostName::Ip(ip) => ip.to_string(),
+
        HostName::Dns(dns) => dns.clone(),
+
        HostName::Tor(onion) => {
+
            let onion = onion.to_string();
+
            let start = onion.chars().take(8).collect::<String>();
+
            let end = onion
+
                .chars()
+
                .skip(onion.len() - 8 - ".onion".len())
+
                .collect::<String>();
+
            format!("{start}…{end}")
+
        }
+
        _ => unreachable!(),
+
    };
+

+
    let port = address.port().to_string();
+

+
    Paint::new(format!("{host}:{port}"))
+
}
+

/// Format a git Oid.
pub fn oid(oid: impl Into<radicle::git::Oid>) -> Paint<String> {
    Paint::new(format!("{:.7}", oid.into()))
@@ -173,7 +199,7 @@ impl fmt::Display for Identity<'_> {
        let nid = self.profile.id();
        let alias = self.profile.aliases().alias(nid);
        let node_id = match self.short {
-
            true => self::node(nid).to_string(),
+
            true => self::nid_human_compact(nid).to_string(),
            false => nid.to_human(),
        };

@@ -231,13 +257,13 @@ impl<'a> Author<'a> {
    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))
+
            None if self.you => term::format::primary(term::format::nid_human_compact(self.nid))
                .dim()
                .into(),
            None => term::Label::blank(),
        };
        let author = self.you().unwrap_or_else(|| {
-
            term::format::primary(term::format::node(self.nid))
+
            term::format::primary(term::format::nid_human_compact(self.nid))
                .dim()
                .into()
        });
modified crates/radicle-protocol/src/wire/message.rs
@@ -2,7 +2,7 @@ use std::{mem, net};

use bytes::Buf;
use bytes::BufMut;
-
use cyphernet::addr::{tor, Addr, HostName, NetAddr};
+
use cyphernet::addr::{tor, HostName, NetAddr};
use radicle::crypto::Signature;
use radicle::git::Oid;
use radicle::identity::RepoId;
modified crates/radicle/src/node.rs
@@ -573,6 +573,14 @@ impl Address {
            _ => true,
        }
    }
+

+
    pub fn host(&self) -> &HostName {
+
        &self.0.host
+
    }
+

+
    pub fn port(&self) -> u16 {
+
        self.0.port
+
    }
}

impl cyphernet::addr::Host for Address {