Skip to content

Commit

Permalink
add support for more types of configurations (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 23, 2022
1 parent 92e082a commit 317e02a
Showing 1 changed file with 62 additions and 19 deletions.
81 changes: 62 additions & 19 deletions src/plumbing/progress.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,87 @@
use crosstermion::crossterm::style::Stylize;
use owo_colors::OwoColorize;
use std::fmt::{Display, Formatter};

#[derive(Clone)]
enum Usage {
InModule(&'static str),
NotApplicable,
Planned {
note: Option<&'static str>,
},
InModule {
name: &'static str,
deviation: Option<&'static str>,
},
}
use Usage::*;

impl Display for Usage {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
InModule(m) => write!(f, "mod {m}"),
NotApplicable => f.write_str("not applicable")?,
Planned { note } => {
write!(f, "{}", "planned".blink())?;
if let Some(note) = note {
write!(f, " ℹ {} ℹ", note.bright_white())?;
}
}
InModule { name, deviation } => {
write!(f, "mod {name}")?;
if let Some(deviation) = deviation {
write!(f, "{}", format!(" ❗️{deviation}❗️").bright_white())?
}
}
}
Ok(())
}
}

impl Usage {
pub fn icon(&self) -> &'static str {
match self {
NotApplicable => "❌",
Planned { .. } => "🕒",
InModule { deviation, .. } => deviation.is_some().then(|| "👌️").unwrap_or("✅"),
}
}
}

#[derive(Clone)]
struct Record {
config: &'static str,
usage: Usage,
deviation: Option<&'static str>,
}

static GIT_CONFIG: &[Record] = &[Record {
config: "pack.threads",
usage: InModule("remote::connection::fetch"),
deviation: Some("if unset, it uses all threads as opposed to just 1"),
}];
static GIT_CONFIG: &[Record] = &[
Record {
config: "fetch.output",
usage: NotApplicable,
},
Record {
config: "fetch.negotiationAlgorithm",
usage: Planned {
note: Some("Implements our own 'naive' algorithm, only"),
},
},
Record {
config: "pack.threads",
usage: InModule {
name: "remote::connection::fetch",
deviation: Some("if unset, it uses all threads as opposed to just 1"),
},
},
];

/// A programmatic way to record and display progress.
pub fn show_progress() -> anyhow::Result<()> {
for Record {
config,
usage,
deviation,
} in GIT_CONFIG
{
println!(
"{}: {usage}{}",
config.bold(),
deviation.map(|d| format!(" ({d})")).unwrap_or_default().dark_grey()
);
let sorted = {
let mut v: Vec<_> = GIT_CONFIG.into();
v.sort_by_key(|r| r.config);
v
};

for Record { config, usage } in sorted {
println!("{} {}: {usage}", usage.icon(), config.bold(),);
}
Ok(())
}

0 comments on commit 317e02a

Please sign in to comment.