cli: Support colors in git helper output

This commit is contained in:
cloudhead 2023-12-20 16:49:15 +01:00
parent 2020e8531e
commit 48843434c7
No known key found for this signature in database
5 changed files with 30 additions and 17 deletions

View File

@ -9,10 +9,9 @@ Switched to a new branch 'feature/1'
$ git commit -a -m "Add things" -q --allow-empty $ 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 $ git push -o patch.message="Add things #1" -o patch.message="See commits for details." rad HEAD:refs/patches
✓ Patch 82faae29b2a2f11bf45bbba4c4787d6b32a12447 opened ✓ Patch 82faae29b2a2f11bf45bbba4c4787d6b32a12447 opened
hint: to update, run `git push`, hint: to update, run `git push` or `git push rad -f HEAD:patches/82faae29b2a2f11bf45bbba4c4787d6b32a12447`
or `git push rad -f HEAD:patches/82faae29b2a2f11bf45bbba4c4787d6b32a12447`
hint: offline push, your node is not running 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 To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new reference] HEAD -> refs/patches * [new reference] HEAD -> refs/patches
``` ```

View File

@ -9,7 +9,7 @@ mod push;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
use std::{env, io}; use std::{env, fmt, io};
use radicle_cli::git::Rev; use radicle_cli::git::Rev;
use thiserror::Error; use thiserror::Error;
@ -86,6 +86,10 @@ pub struct Options {
/// Run the radicle remote helper using the given profile. /// Run the radicle remote helper using the given profile.
pub fn run(profile: radicle::Profile) -> Result<(), Error> { 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 url: Url = {
let args = env::args().skip(1).take(2).collect::<Vec<_>>(); let args = env::args().skip(1).take(2).collect::<Vec<_>>();
@ -227,3 +231,8 @@ pub(crate) fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Resu
Ok(tokens) Ok(tokens)
} }
/// Write a hint to the user.
pub(crate) fn hint(s: impl fmt::Display) {
eprintln!("{}", cli::format::hint(format!("hint: {s}")));
}

View File

@ -21,7 +21,7 @@ use radicle::Profile;
use radicle::{git, rad}; use radicle::{git, rad};
use radicle_cli::terminal as cli; 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. /// Default timeout for syncing to the network after a push.
const DEFAULT_SYNC_TIMEOUT: time::Duration = time::Duration::from_secs(9); const DEFAULT_SYNC_TIMEOUT: time::Duration = time::Duration::from_secs(9);
@ -248,14 +248,14 @@ pub fn run(
&& !rollback && !rollback
{ {
if hints { if hints {
eprintln!( hint(
"hint: you are attempting to push a commit that would \ "you are attempting to push a commit that would cause \
cause your upstream to diverge from the canonical head" 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)); 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. // Nb. allow this to fail. The push to local storage was still successful.
sync(stored.id, node).ok(); sync(stored.id, node).ok();
} else if hints { } else if hints {
eprintln!("hint: offline push, your node is not running"); hint("offline push, your node is not running");
eprintln!(" to sync with the network, run `rad node start`"); hint("to sync with the network, run `rad node start`");
} }
} }
} }
@ -397,8 +397,9 @@ fn patch_open<G: Signer>(
// Remove the remote portion of the name, i.e. // Remove the remote portion of the name, i.e.
// rad/patches/deadbeef -> patches/deadbeef // rad/patches/deadbeef -> patches/deadbeef
let name = name.split('/').skip(1).collect::<Vec<_>>().join("/"); let name = name.split('/').skip(1).collect::<Vec<_>>().join("/");
eprintln!("hint: to update, run `git push`,"); hint(format!(
eprintln!(" or `git push rad -f HEAD:{name}`"); "to update, run `git push` or `git push rad -f HEAD:{name}`"
));
} }
} }
} }

View File

@ -32,6 +32,10 @@ pub fn yellow<D: std::fmt::Display>(msg: D) -> Paint<D> {
Paint::yellow(msg) Paint::yellow(msg)
} }
pub fn hint<D: std::fmt::Display>(msg: D) -> Paint<D> {
Paint::yellow(msg)
}
pub fn faint<D: std::fmt::Display>(msg: D) -> Paint<D> { pub fn faint<D: std::fmt::Display>(msg: D) -> Paint<D> {
Paint::fixed(236, msg) Paint::fixed(236, msg)
} }

View File

@ -178,7 +178,7 @@ pub fn error(error: impl fmt::Display) {
} }
pub fn hint(hint: 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<D: fmt::Display>(prompt: D, default: bool) -> bool { pub fn ask<D: fmt::Display>(prompt: D, default: bool) -> bool {