From 48843434c701cae9a7dbdbf0e08e3c42b5dee2cc Mon Sep 17 00:00:00 2001 From: cloudhead Date: Wed, 20 Dec 2023 16:49:15 +0100 Subject: [PATCH] cli: Support colors in git helper output --- radicle-cli/examples/rad-patch-via-push.md | 5 ++--- radicle-remote-helper/src/lib.rs | 11 +++++++++- radicle-remote-helper/src/push.rs | 25 +++++++++++----------- radicle-term/src/format.rs | 4 ++++ radicle-term/src/io.rs | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/radicle-cli/examples/rad-patch-via-push.md b/radicle-cli/examples/rad-patch-via-push.md index 7b4038df..d2a0c24f 100644 --- a/radicle-cli/examples/rad-patch-via-push.md +++ b/radicle-cli/examples/rad-patch-via-push.md @@ -9,10 +9,9 @@ Switched to a new branch 'feature/1' $ git commit -a -m "Add things" -q --allow-empty $ git push -o patch.message="Add things #1" -o patch.message="See commits for details." rad HEAD:refs/patches ✓ Patch 82faae29b2a2f11bf45bbba4c4787d6b32a12447 opened -hint: to update, run `git push`, - or `git push rad -f HEAD:patches/82faae29b2a2f11bf45bbba4c4787d6b32a12447` +hint: to update, run `git push` or `git push rad -f HEAD:patches/82faae29b2a2f11bf45bbba4c4787d6b32a12447` hint: offline push, your node is not running - to sync with the network, run `rad node start` +hint: to sync with the network, run `rad node start` To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi * [new reference] HEAD -> refs/patches ``` diff --git a/radicle-remote-helper/src/lib.rs b/radicle-remote-helper/src/lib.rs index 3721dfe6..f9b7f308 100644 --- a/radicle-remote-helper/src/lib.rs +++ b/radicle-remote-helper/src/lib.rs @@ -9,7 +9,7 @@ mod push; use std::path::PathBuf; use std::str::FromStr; -use std::{env, io}; +use std::{env, fmt, io}; use radicle_cli::git::Rev; use thiserror::Error; @@ -86,6 +86,10 @@ pub struct Options { /// Run the radicle remote helper using the given profile. pub fn run(profile: radicle::Profile) -> Result<(), Error> { + // Since we're going to be writing user output to `stderr`, make sure the paint + // module is aware of that. + cli::Paint::set_terminal(io::stderr()); + let url: Url = { let args = env::args().skip(1).take(2).collect::>(); @@ -227,3 +231,8 @@ pub(crate) fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Resu Ok(tokens) } + +/// Write a hint to the user. +pub(crate) fn hint(s: impl fmt::Display) { + eprintln!("{}", cli::format::hint(format!("hint: {s}"))); +} diff --git a/radicle-remote-helper/src/push.rs b/radicle-remote-helper/src/push.rs index d8f6a4a5..5037d9ce 100644 --- a/radicle-remote-helper/src/push.rs +++ b/radicle-remote-helper/src/push.rs @@ -21,7 +21,7 @@ use radicle::Profile; use radicle::{git, rad}; use radicle_cli::terminal as cli; -use crate::{read_line, Options}; +use crate::{hint, read_line, Options}; /// Default timeout for syncing to the network after a push. const DEFAULT_SYNC_TIMEOUT: time::Duration = time::Duration::from_secs(9); @@ -248,14 +248,14 @@ pub fn run( && !rollback { if hints { - eprintln!( - "hint: you are attempting to push a commit that would \ - cause your upstream to diverge from the canonical head" + hint( + "you are attempting to push a commit that would cause \ + your upstream to diverge from the canonical head", + ); + hint( + "to integrate the remote changes, run `git pull --rebase` \ + and try again", ); - eprintln!( - "hint: to integrate the remote changes, run `git pull --rebase` \ - and try again" - ); } return Err(Error::HeadsDiverge(head.into(), *canonical_oid)); } @@ -291,8 +291,8 @@ pub fn run( // Nb. allow this to fail. The push to local storage was still successful. sync(stored.id, node).ok(); } else if hints { - eprintln!("hint: offline push, your node is not running"); - eprintln!(" to sync with the network, run `rad node start`"); + hint("offline push, your node is not running"); + hint("to sync with the network, run `rad node start`"); } } } @@ -397,8 +397,9 @@ fn patch_open( // Remove the remote portion of the name, i.e. // rad/patches/deadbeef -> patches/deadbeef let name = name.split('/').skip(1).collect::>().join("/"); - eprintln!("hint: to update, run `git push`,"); - eprintln!(" or `git push rad -f HEAD:{name}`"); + hint(format!( + "to update, run `git push` or `git push rad -f HEAD:{name}`" + )); } } } diff --git a/radicle-term/src/format.rs b/radicle-term/src/format.rs index 62e0a994..223a7646 100644 --- a/radicle-term/src/format.rs +++ b/radicle-term/src/format.rs @@ -32,6 +32,10 @@ pub fn yellow(msg: D) -> Paint { Paint::yellow(msg) } +pub fn hint(msg: D) -> Paint { + Paint::yellow(msg) +} + pub fn faint(msg: D) -> Paint { Paint::fixed(236, msg) } diff --git a/radicle-term/src/io.rs b/radicle-term/src/io.rs index 7629e71a..3fa811ed 100644 --- a/radicle-term/src/io.rs +++ b/radicle-term/src/io.rs @@ -178,7 +178,7 @@ pub fn error(error: impl fmt::Display) { } pub fn hint(hint: impl fmt::Display) { - println!("{ERROR_HINT_PREFIX} {}", Paint::yellow(hint)); + println!("{ERROR_HINT_PREFIX} {}", format::hint(hint)); } pub fn ask(prompt: D, default: bool) -> bool {