Skip to content

Commit 735e97e

Browse files
committed
for review
1 parent eebab11 commit 735e97e

File tree

6 files changed

+102
-5
lines changed

6 files changed

+102
-5
lines changed

src/cli.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub struct CliOptions {
3737
#[command(flatten)]
3838
pub visuals: VisualsCliOptions,
3939
#[command(flatten)]
40+
pub config: ConfigCliOptions,
41+
#[command(flatten)]
4042
pub developer: DeveloperCliOptions,
4143
#[command(flatten)]
4244
pub other: OtherCliOptions,
@@ -213,6 +215,18 @@ pub struct VisualsCliOptions {
213215
pub nerd_fonts: bool,
214216
}
215217

218+
#[derive(Clone, Debug, Args, PartialEq, Eq)]
219+
#[command(next_help_heading = "CONFIG")]
220+
pub struct ConfigCliOptions {
221+
/// Path to the config file
222+
#[arg(long, value_hint = ValueHint::FilePath)]
223+
pub config_path: PathBuf,
224+
/// Creates a default config file
225+
/// Writes to $XDG_CONFIG_HOME/onefetch/config.toml but can be overridden with --config-path
226+
#[arg(long)]
227+
pub generate_config: bool,
228+
}
229+
216230
#[derive(Clone, Debug, Args, PartialEq, Eq, Default)]
217231
#[command(next_help_heading = "DEVELOPER")]
218232
pub struct DeveloperCliOptions {
@@ -244,6 +258,7 @@ impl Default for CliOptions {
244258
visuals: VisualsCliOptions::default(),
245259
ascii: AsciiCliOptions::default(),
246260
image: ImageCliOptions::default(),
261+
config: ConfigCliOptions::default(),
247262
developer: DeveloperCliOptions::default(),
248263
other: OtherCliOptions::default(),
249264
}
@@ -302,6 +317,16 @@ impl Default for ImageCliOptions {
302317
}
303318
}
304319

320+
impl Default for ConfigCliOptions {
321+
fn default() -> Self {
322+
ConfigCliOptions {
323+
// Not sure about unwrap
324+
config_path: dirs::config_dir().unwrap().join("onefetch/config.toml"),
325+
generate_config: false,
326+
}
327+
}
328+
}
329+
305330
pub fn print_supported_languages() -> Result<()> {
306331
for l in Language::iter() {
307332
println!("{l}");

src/config.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use anyhow::Result;
2+
use serde::{Deserialize, Serialize};
3+
use std::{fs, path::Path};
4+
5+
use crate::info::utils::info_field::InfoType;
6+
7+
#[derive(Serialize, Deserialize)]
8+
pub struct ConfigOptions {
9+
#[serde(default)]
10+
pub disabled_fields: Vec<InfoType>,
11+
}
12+
13+
impl Default for ConfigOptions {
14+
fn default() -> Self {
15+
Self {
16+
disabled_fields: vec![],
17+
}
18+
}
19+
}
20+
21+
impl ConfigOptions {
22+
pub fn read<P>(path: P) -> Self
23+
// Is it even should panic?
24+
where
25+
P: AsRef<Path>,
26+
{
27+
let contents = fs::read_to_string(path);
28+
match contents {
29+
Err(_) => Self::default(),
30+
Ok(contents) => {
31+
// I wish to print exact error here, like syntax errors
32+
toml::from_str(&contents).unwrap()
33+
}
34+
}
35+
}
36+
37+
pub fn write_default<P>(path: P) -> Result<()>
38+
// I believe user would like to generate config with CLI flags
39+
// I mean to write disabled_fields with --disabled-fields flag
40+
where
41+
// Nah this feels really bad but rust-analyser told me to add ' + std::fmt::Display'
42+
P: AsRef<Path> + std::fmt::Display,
43+
{
44+
// I dont think this can panic so i simply unwrapped it
45+
let defaults = toml::to_string(&Self::default()).unwrap();
46+
match fs::create_dir_all(&path) {
47+
Ok(_) => match fs::write(&path, &defaults) {
48+
Ok(_) => println!("Default config file created at: {path}"),
49+
Err(e) => eprintln!("Failed to write default config file: {}", e),
50+
},
51+
Err(e) => {
52+
eprintln!("Failed to create config directory {path}: {e}");
53+
}
54+
}
55+
// Im not sure it should return simple Ok(())?
56+
Ok(())
57+
}
58+
}

src/info/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use self::url::UrlInfo;
2222
use self::utils::info_field::{InfoField, InfoType};
2323
use self::version::VersionInfo;
2424
use crate::cli::{is_truecolor_terminal, CliOptions, NumberSeparator, When};
25+
use crate::config::ConfigOptions;
2526
use crate::ui::get_ascii_colors;
2627
use crate::ui::text_colors::TextColors;
2728
use anyhow::{Context, Result};
@@ -109,7 +110,7 @@ impl std::fmt::Display for Info {
109110
}
110111
}
111112

112-
pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
113+
pub fn build_info(cli_options: &CliOptions, config_options: &ConfigOptions) -> Result<Info> {
113114
let mut repo = gix::ThreadSafeRepository::discover_opts(
114115
&cli_options.input,
115116
gix::discover::upwards::Options {
@@ -178,7 +179,7 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
178179
let globs_to_exclude = &cli_options.info.exclude;
179180
let show_email = cli_options.info.email;
180181

181-
Ok(InfoBuilder::new(cli_options)
182+
Ok(InfoBuilder::new(cli_options, config_options)
182183
.title(&repo, no_bold, &text_colors)
183184
.project(&repo, &repo_url, manifest.as_ref(), number_separator)?
184185
.description(manifest.as_ref())
@@ -217,10 +218,12 @@ pub fn build_info(cli_options: &CliOptions) -> Result<Info> {
217218
}
218219

219220
impl InfoBuilder {
220-
fn new(cli_options: &CliOptions) -> Self {
221+
fn new(cli_options: &CliOptions, config_options: &ConfigOptions) -> Self {
221222
Self {
222223
title: None,
223224
info_fields: Vec::new(),
225+
// I have totally NO idea how to properly override values
226+
// if cli_options.info.disabled_fields is NOT type of Option<T>
224227
disabled_fields: cli_options.info.disabled_fields.clone(),
225228
no_title: cli_options.info.no_title,
226229
}

src/info/utils/info_field.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{info::utils::get_style, ui::text_colors::TextColors};
22
use owo_colors::OwoColorize;
3+
use serde::{Deserialize, Serialize};
34
use std::fmt;
45

56
#[typetag::serialize]
@@ -55,7 +56,7 @@ pub trait InfoField {
5556
}
5657
}
5758

58-
#[derive(Clone, clap::ValueEnum, Debug, Eq, PartialEq)]
59+
#[derive(Clone, clap::ValueEnum, Debug, Eq, PartialEq, Serialize, Deserialize)]
5960
pub enum InfoType {
6061
Project,
6162
Description,

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
pub mod cli;
33
pub mod info;
44
pub mod ui;
5+
pub mod config;

src/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anyhow::Result;
44
use clap::{CommandFactory, Parser};
55
use human_panic::setup_panic;
66
use onefetch::cli::{self, CliOptions};
7+
use onefetch::config::ConfigOptions;
78
use onefetch::info::build_info;
89
use onefetch::ui::printer::Printer;
910
use std::io;
@@ -30,7 +31,15 @@ fn main() -> Result<()> {
3031
return Ok(());
3132
}
3233

33-
let info = build_info(&cli_options)?;
34+
if cli_options.config.generate_config {
35+
// @spenserblack fixme pls
36+
// it complans if i add .display(), and complains if i remove .display()
37+
return ConfigOptions::write_default(cli_options.config.config_path.display());
38+
}
39+
40+
let config_options = ConfigOptions::read(&cli_options.config.config_path);
41+
42+
let info = build_info(&cli_options, &config_options)?;
3443

3544
let mut printer = Printer::new(io::BufWriter::new(io::stdout()), info, cli_options)?;
3645

0 commit comments

Comments
 (0)