Skip to content

Commit

Permalink
feat: add config option banner.quote.enable
Browse files Browse the repository at this point in the history
  • Loading branch information
threadexio committed Jan 2, 2025
1 parent 1a4ac83 commit 5104520
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cbundl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ deterministic = false
[banner]
enable = true

[banner.quote]
enable = true

[formatter]
# enable = true
# path = "clang-format"
26 changes: 15 additions & 11 deletions src/banner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::quotes::Quotes;

#[derive(Debug, Clone)]
pub struct Banner {
pub quotes: Quotes,
pub quotes: Option<Quotes>,
pub deterministic: bool,
}

Expand All @@ -35,12 +35,6 @@ impl Banner {
format_date(Local::now())
};

let quote = if self.deterministic {
self.quotes.get(0).expect("we dont have a single quote :'(")
} else {
self.quotes.random()
};

let line1 = formatcp!("{CRATE_NAME} {SHORT_VERSION}");
let line2 = formatcp!("{CRATE_REPOSITORY}");
let line3 = format!("Generated at: {}", generated_at);
Expand All @@ -60,10 +54,20 @@ impl Banner {
writeln!(out, " *")?;
writeln!(out, " * {:^1$}", line3, banner_width)?;
writeln!(out, " *")?;
writeln!(out, " *")?;
quote.lines().try_for_each(|x| writeln!(out, " * {x}"))?;
writeln!(out, " * - {}", quote.author())?;
writeln!(out, " *")?;

if let Some(quotes) = self.quotes.as_ref() {
let quote = if self.deterministic {
quotes.get(0).expect("we dont have a single quote :'(")
} else {
quotes.random()
};

writeln!(out, " *")?;
quote.lines().try_for_each(|x| writeln!(out, " * {x}"))?;
writeln!(out, " * - {}", quote.author())?;
writeln!(out, " *")?;
};

writeln!(out, " */")?;
writeln!(out)?;

Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn run() -> Result<()> {
let mut pipeline = Pipeline {
bundler: Bundler {},
banner: (!config.no_banner).then_some(Banner {
quotes: Quotes {},
quotes: config.enable_quote.then_some(Quotes {}),
deterministic: config.deterministic,
}),
formatter: (!config.no_format).then_some(Formatter {
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ struct BundleSection {
#[derive(Debug, Clone, Deserialize)]
struct BannerSection {
enable: Option<bool>,
quote: Option<QuoteSection>,
}

#[derive(Debug, Clone, Deserialize)]
struct QuoteSection {
enable: Option<bool>,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -161,6 +167,7 @@ pub struct Config {
pub output_file: Option<PathBuf>,

pub no_banner: bool,
pub enable_quote: bool,

pub no_format: bool,
pub formatter: PathBuf,
Expand Down Expand Up @@ -217,6 +224,13 @@ impl Config {
})
.unwrap_or(false);

let enable_quote = file
.as_ref()
.and_then(|x| x.banner.as_ref())
.and_then(|x| x.quote.as_ref())
.and_then(|x| x.enable)
.unwrap_or(true);

let no_format = args
.flag("no_format")
.or_else(|| {
Expand Down Expand Up @@ -245,6 +259,7 @@ impl Config {
output_file,

no_banner,
enable_quote,

no_format,
formatter,
Expand Down

0 comments on commit 5104520

Please sign in to comment.