Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
cli/terminal: Formatting Functions for Ranges
Richard Levitte committed 1 month ago
commit d72eafbb54c7740d42735093b448266660d6a316
parent f223afd9d7eb4d1301c7f6fe98bc06418c35c3f5
1 file changed +34 -1
modified crates/radicle-cli/src/terminal/format.rs
@@ -32,11 +32,44 @@ pub fn node_id_human(node: &NodeId) -> Paint<String> {
    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> {
    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)"`.
pub fn parens<D: fmt::Display>(input: Paint<D>) -> Paint<String> {
    Paint::new(format!("({})", input.item)).with_style(input.style)