Skip to content

config impl (pt.1) #1550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
1,182 changes: 507 additions & 675 deletions Cargo.lock

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
authors = ["o2sh <[email protected]>"]
edition = "2021"
license = "MIT"
version = "2.23.1"
version = "2.24.0"
repository = "https://github.com/o2sh/onefetch"

[workspace]
members = ["ascii", "image", "manifest"]

[workspace.dependencies]
owo-colors = "4.1.0"
owo-colors = "4.2.0"
anyhow = "1.0"
clap = { version = "4.5.26", features = ["derive"] }
clap = { version = "4.5.34", features = ["derive"] }
image = { version = "0.25.5", default-features = false, features = [
"color_quant",
"jpeg",
Expand All @@ -32,24 +32,25 @@ exclude = ["docs/vercel/*"]
keywords = ["git", "cli", "terminal"]
name = "onefetch"
homepage = "https://onefetch.dev"
rust-version = "1.81.0"
rust-version = "1.82.0"

[dependencies]
anyhow.workspace = true
askalono = "0.5.0"
byte-unit = "5.1.6"
clap.workspace = true
clap_complete = "4.5.42"
crossbeam-channel = "0.5.14"
gix = { version = "0.70.0", default-features = false, features = [
clap_complete = "4.5.47"
crossbeam-channel = "0.5.15"
dirs = "6.0.0"
gix = { version = "0.72.1", default-features = false, features = [
"max-performance-safe",
"blob-diff",
"mailmap",
"index",
"status",
] }
gix-features = { version = "0.40.0", features = ["zlib-ng"] }
globset = "0.4.15"
gix-features = { version = "0.42.1", features = ["zlib-ng"] }
globset = "0.4.16"
human-panic = "2.0.2"
image.workspace = true
num-format = "0.4.4"
Expand All @@ -67,13 +68,14 @@ strum.workspace = true
time = { version = "0.3.37", features = ["formatting"] }
time-humanize = { version = "0.1.3", features = ["time"] }
tokei = "13.0.0-alpha.8"
toml = "0.8.22"
typetag = "0.2"

[dev-dependencies]
criterion = "0.5.1"
gix-testtools = "0.15.0"
insta = { version = "1.42.1", features = ["json", "redactions"] }
rstest = "0.24.0"
gix-testtools = "0.16.1"
insta = { version = "1.42.2", features = ["json", "redactions"] }
rstest = "0.25.0"

[[bench]]
name = "repo"
Expand Down
95 changes: 63 additions & 32 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use num_format::CustomFormat;
use onefetch_image::ImageProtocol;
use onefetch_manifest::ManifestType;
use regex::Regex;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::env;
use std::io;
use std::path::PathBuf;
Expand All @@ -29,14 +29,16 @@ pub struct CliOptions {
#[command(flatten)]
pub info: InfoCliOptions,
#[command(flatten)]
pub text_formatting: TextForamttingCliOptions,
pub text_formatting: TextFormattingCliOptions,
#[command(flatten)]
pub ascii: AsciiCliOptions,
#[command(flatten)]
pub image: ImageCliOptions,
#[command(flatten)]
pub visuals: VisualsCliOptions,
#[command(flatten)]
pub config: ConfigCliOptions,
#[command(flatten)]
pub developer: DeveloperCliOptions,
#[command(flatten)]
pub other: OtherCliOptions,
Expand All @@ -54,19 +56,19 @@ pub struct InfoCliOptions {
value_enum,
value_name = "FIELD"
)]
pub disabled_fields: Vec<InfoType>,
pub disabled_fields: Option<Vec<InfoType>>,
/// Hides the title
#[arg(long)]
pub no_title: bool,
pub no_title: Option<bool>,
/// Maximum NUM of authors to be shown
#[arg(long, default_value_t = 3usize, value_name = "NUM")]
pub number_of_authors: usize,
#[arg(long, value_name = "NUM")]
pub number_of_authors: Option<usize>,
/// Maximum NUM of languages to be shown
#[arg(long, default_value_t = 6usize, value_name = "NUM")]
pub number_of_languages: usize,
#[arg(long, value_name = "NUM")]
pub number_of_languages: Option<usize>,
/// Maximum NUM of file churns to be shown
#[arg(long, default_value_t = 3usize, value_name = "NUM")]
pub number_of_file_churns: usize,
#[arg(long, value_name = "NUM")]
pub number_of_file_churns: Option<usize>,
/// Minimum NUM of commits from HEAD used to compute the churn summary
///
/// By default, the actual value is non-deterministic due to time-based computation
Expand All @@ -87,7 +89,7 @@ pub struct InfoCliOptions {
pub no_bots: Option<MyRegex>,
/// Ignores merge commits
#[arg(long)]
pub no_merges: bool,
pub no_merges: Option<bool>,
/// Show the email address of each author
#[arg(long, short = 'E')]
pub email: bool,
Expand All @@ -99,7 +101,7 @@ pub struct InfoCliOptions {
pub hide_token: bool,
/// Count hidden files and directories
#[arg(long)]
pub include_hidden: bool,
pub include_hidden: Option<bool>,
/// Filters output by language type
#[arg(
long,
Expand Down Expand Up @@ -145,7 +147,7 @@ pub struct AsciiCliOptions {
///
/// If set to auto: true color will be enabled if supported by the terminal
#[arg(long, default_value = "auto", value_name = "WHEN", value_enum)]
pub true_color: When,
pub true_color: Option<When>,
}

#[derive(Clone, Debug, Args, PartialEq, Eq)]
Expand All @@ -171,7 +173,7 @@ pub struct ImageCliOptions {

#[derive(Clone, Debug, Args, PartialEq, Eq)]
#[command(next_help_heading = "TEXT FORMATTING")]
pub struct TextForamttingCliOptions {
pub struct TextFormattingCliOptions {
/// Changes the text colors (X X X...)
///
/// Goes in order of title, ~, underline, subtitle, colon, and info
Expand All @@ -189,13 +191,13 @@ pub struct TextForamttingCliOptions {
pub text_colors: Vec<u8>,
/// Use ISO 8601 formatted timestamps
#[arg(long, short = 'z')]
pub iso_time: bool,
pub iso_time: Option<bool>,
/// Which thousands SEPARATOR to use
#[arg(long, value_name = "SEPARATOR", default_value = "plain", value_enum)]
pub number_separator: NumberSeparator,
#[arg(long, value_name = "SEPARATOR", value_enum)]
pub number_separator: Option<NumberSeparator>,
/// Turns off bold formatting
#[arg(long)]
pub no_bold: bool,
pub no_bold: Option<bool>,
}
#[derive(Clone, Debug, Args, PartialEq, Eq, Default)]
#[command(next_help_heading = "VISUALS")]
Expand All @@ -210,7 +212,19 @@ pub struct VisualsCliOptions {
///
/// Replaces language chips with Nerd Font icons
#[arg(long)]
pub nerd_fonts: bool,
pub nerd_fonts: Option<bool>,
}

#[derive(Clone, Debug, Args, PartialEq, Eq)]
#[command(next_help_heading = "CONFIG")]
pub struct ConfigCliOptions {
/// Path to the config file
#[arg(long, value_hint = ValueHint::FilePath)]
pub config_path: Option<PathBuf>,
/// Creates a default config file
/// Writes to $XDG_CONFIG_HOME/onefetch/config.toml but can be overridden with --config-path
#[arg(long)]
pub generate_config: bool,
}

#[derive(Clone, Debug, Args, PartialEq, Eq, Default)]
Expand Down Expand Up @@ -240,10 +254,11 @@ impl Default for CliOptions {
CliOptions {
input: PathBuf::from("."),
info: InfoCliOptions::default(),
text_formatting: TextForamttingCliOptions::default(),
text_formatting: TextFormattingCliOptions::default(),
visuals: VisualsCliOptions::default(),
ascii: AsciiCliOptions::default(),
image: ImageCliOptions::default(),
config: ConfigCliOptions::default(),
developer: DeveloperCliOptions::default(),
other: OtherCliOptions::default(),
}
Expand All @@ -253,9 +268,9 @@ impl Default for CliOptions {
impl Default for InfoCliOptions {
fn default() -> Self {
InfoCliOptions {
number_of_authors: 3,
number_of_languages: 6,
number_of_file_churns: 3,
number_of_authors: Some(3),
number_of_languages: Some(6),
number_of_file_churns: Some(3),
churn_pool_size: Option::default(),
exclude: Vec::default(),
no_bots: Option::default(),
Expand All @@ -265,18 +280,18 @@ impl Default for InfoCliOptions {
hide_token: Default::default(),
include_hidden: Default::default(),
r#type: vec![LanguageType::Programming, LanguageType::Markup],
disabled_fields: Vec::default(),
disabled_fields: Some(Vec::default()),
no_title: Default::default(),
}
}
}

impl Default for TextForamttingCliOptions {
impl Default for TextFormattingCliOptions {
fn default() -> Self {
TextForamttingCliOptions {
TextFormattingCliOptions {
text_colors: Default::default(),
iso_time: Default::default(),
number_separator: NumberSeparator::Plain,
number_separator: Some(NumberSeparator::Plain),
no_bold: Default::default(),
}
}
Expand All @@ -288,7 +303,7 @@ impl Default for AsciiCliOptions {
ascii_input: Option::default(),
ascii_colors: Vec::default(),
ascii_language: Option::default(),
true_color: When::Auto,
true_color: Some(When::Auto),
}
}
}
Expand All @@ -302,6 +317,20 @@ impl Default for ImageCliOptions {
}
}

impl Default for ConfigCliOptions {
fn default() -> Self {
ConfigCliOptions {
// Not sure about unwrap
config_path: Some(
dirs::config_dir()
.expect("Could not find $HOME!")
.join("onefetch/config.toml"),
),
generate_config: false,
}
}
}

pub fn print_supported_languages() -> Result<()> {
for l in Language::iter() {
println!("{l}");
Expand Down Expand Up @@ -337,15 +366,17 @@ pub fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug)]
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Default)]
pub enum When {
#[default]
Auto,
Never,
Always,
}

#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug, Serialize, Copy)]
#[derive(clap::ValueEnum, Clone, PartialEq, Eq, Debug, Deserialize, Serialize, Copy, Default)]
pub enum NumberSeparator {
#[default]
Plain,
Comma,
Space,
Expand Down Expand Up @@ -387,8 +418,8 @@ mod test {
input: PathBuf::from("/tmp/folder"),
info: InfoCliOptions {
number_of_authors: 4,
no_merges: true,
disabled_fields: vec![InfoType::Version, InfoType::URL],
no_merges: Some(true),
disabled_fields: vec![InfoType::Version, InfoType::URL].into(),
..Default::default()
},
ascii: AsciiCliOptions {
Expand Down
Loading