cli/terminal: Formatting Functions for Ranges

These allow convenient formatting of long/full object identifiers,
as well as formatting ranges.
This commit is contained in:
Richard Levitte 2026-03-23 12:37:19 +01:00 committed by Fintan Halpenny
parent b54fc820e9
commit b8f8cfb319
2 changed files with 37 additions and 2 deletions

View File

@ -32,11 +32,44 @@ pub fn node_id_human(node: &NodeId) -> Paint<String> {
Paint::new(node.to_human()) Paint::new(node.to_human())
} }
/// Format a git Oid. /// Format a Git object identifier.
/// To format a Git object identifier in short form, see [`oid`].
pub fn oid_long(oid: impl Into<radicle::git::Oid>) -> Paint<String> {
Paint::new(format!("{}", oid.into()))
}
/// Format a Git object identifier, shortened to the first 7 characters.
/// To format a Git object identifier in long form, see [`oid_long`].
pub fn oid(oid: impl Into<radicle::git::Oid>) -> Paint<String> { pub fn oid(oid: impl Into<radicle::git::Oid>) -> Paint<String> {
Paint::new(format!("{:.7}", oid.into())) Paint::new(format!("{:.7}", oid.into()))
} }
fn double_dot(base: impl std::fmt::Display, head: impl std::fmt::Display) -> Paint<String> {
Paint::new(format!("{}..{}", base, head))
}
/// Format a range between Git object identifiers (usually commits).
/// Both object identifiers are formatted in their long form,
/// see [`oid_long`].
/// To format a range in short form, see [`range`].
pub fn range_long<IntoOid>(base: IntoOid, head: IntoOid) -> Paint<String>
where
IntoOid: Into<radicle::git::Oid>,
{
double_dot(oid_long(base), oid_long(head))
}
/// Format a range between Git object identifiers (usually commits).
/// Both object identifiers are formatted in short form,
/// see [`oid`].
/// To format a range in long form, see [`range_long`].
pub fn range<IntoOid>(base: IntoOid, head: IntoOid) -> Paint<String>
where
IntoOid: Into<radicle::git::Oid>,
{
double_dot(oid(base), oid(head))
}
/// Wrap parenthesis around styled input, eg. `"input"` -> `"(input)"`. /// Wrap parenthesis around styled input, eg. `"input"` -> `"(input)"`.
pub fn parens<D: fmt::Display>(input: Paint<D>) -> Paint<String> { pub fn parens<D: fmt::Display>(input: Paint<D>) -> Paint<String> {
Paint::new(format!("({})", input.item)).with_style(input.style) Paint::new(format!("({})", input.item)).with_style(input.style)

View File

@ -209,7 +209,9 @@ impl Update<'_> {
line.pad( line.pad(
2 // alignment 2 // alignment
+ 2 // parens + 2 // parens
+ LENGTH_OF_SHORT_COMMIT_HASH + LENGTH_OF_SHORT_COMMIT_HASH // base
+ 2 // ..
+ LENGTH_OF_SHORT_COMMIT_HASH // head
+ LENGTH_OF_SPACES, + LENGTH_OF_SPACES,
); );
} }