Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
term: Color Node IDs
Draft lorenz opened 11 months ago

Though Node IDs technically are not hashes, we can use similar techniques to “fingerprint” them.

This seems to break tables.

See also:

2 files changed +36 -2 41f9048d 52e99633
modified radicle-cli/src/terminal/format.rs
@@ -18,9 +18,34 @@ use crate::terminal as term;

/// Format a node id to be more compact.
pub fn node(node: &NodeId) -> Paint<String> {
+
    const CHARS_PER_BLOCK: usize = 7;
+
    const BYTES_PER_CHAR: usize = 3;
+

+
    let pk = node.0;
    let node = node.to_human();
-
    let start = node.chars().take(7).collect::<String>();
-
    let end = node.chars().skip(node.len() - 7).collect::<String>();
+

+
    fn colored_char(r: u8, g: u8, b: u8, c: char) -> String {
+
        let fg = term::Color::RGB(r, g, b);
+
        format!(
+
            "{}",
+
            Paint::from(c).fg(fg).bg(fg
+
                .complimentary()
+
                .expect("RGB color has complimentary color"))
+
        )
+
    }
+

+
    let start = pk
+
        .windows(BYTES_PER_CHAR)
+
        .zip(node.chars().take(CHARS_PER_BLOCK))
+
        .map(|(b, c)| colored_char(b[0], b[1], b[2], c))
+
        .collect::<String>();
+

+
    let end = pk
+
        .windows(BYTES_PER_CHAR)
+
        .rev()
+
        .zip(node.chars().skip(node.len() - CHARS_PER_BLOCK))
+
        .map(|(b, c)| colored_char(b[0], b[1], b[2], c))
+
        .collect::<String>();

    Paint::new(format!("{start}…{end}"))
}
modified radicle-term/src/ansi/color.rs
@@ -45,6 +45,15 @@ impl Color {
        Style::new(self)
    }

+
    pub fn complimentary(&self) -> Option<Color> {
+
        match *self {
+
            Color::Unset => Some(Color::Unset),
+
            Color::White => Some(Color::Black),
+
            Color::RGB(r, g, b) => Some(Color::RGB(u8::MAX - r, u8::MAX - g, u8::MAX - b)),
+
            _ => None,
+
        }
+
    }
+

    pub(crate) fn ansi_fmt(&self, f: &mut dyn fmt::Write) -> fmt::Result {
        match *self {
            Color::Unset => Ok(()),