Skip to content

Commit

Permalink
Add custom argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lunacookies committed Oct 16, 2023
1 parent d58ced8 commit e611603
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 227 deletions.
153 changes: 0 additions & 153 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/pipes-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ version = "1.6.2"

[dependencies]
anyhow = "1.0.70"
clap = { version = "4.2.1", features = ["derive", "help", "wrap_help"] }
etcetera = "0.8.0"
mimalloc = { version = "0.1.36", default-features = false }
model = { path = "../model" }
Expand Down
66 changes: 3 additions & 63 deletions crates/pipes-rs/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,29 @@
use anyhow::Context;
use clap::Parser;
use etcetera::app_strategy::{AppStrategy, AppStrategyArgs, Xdg};
use model::pipe::{ColorMode, Kind, KindSet, Palette};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use std::time::Duration;

#[derive(Serialize, Deserialize, Default, Parser)]
#[command(
name = "pipes-rs",
version,
about = "An over-engineered rewrite of pipes.sh in Rust."
)]
#[derive(Serialize, Deserialize, Default)]
pub struct Config {
/// what kind of terminal coloring to use
#[arg(short, long)]
pub color_mode: Option<ColorMode>,

/// the color palette used assign colors to pipes
#[arg(long)]
pub palette: Option<Palette>,

/// cycle hue of pipes
#[arg(long, value_name = "DEGREES")]
pub rainbow: Option<u8>,

/// delay between frames in milliseconds
#[arg(short, long = "delay")]
pub delay_ms: Option<u64>,

/// number of frames of animation that are displayed in a second; use 0 for unlimited
#[arg(short, long)]
pub fps: Option<f32>,

/// portion of screen covered before resetting (0.0–1.0)
#[arg(short, long)]
pub reset_threshold: Option<f32>,

/// kinds of pipes separated by commas, e.g. heavy,curved
#[arg(short, long)]
pub kinds: Option<KindSet>,

/// whether to use bold
#[arg(short, long, value_name = "BOOL")]
pub bold: Option<bool>,

/// whether pipes should retain style after hitting the edge
#[arg(short, long, value_name = "BOOL")]
pub inherit_style: Option<bool>,

/// number of pipes
#[arg(name = "pipe-num", short, long, value_name = "NUM")]
pub num_pipes: Option<u32>,

/// chance of a pipe turning (0.0–1.0)
#[arg(short, long)]
pub turn_chance: Option<f32>,

/// Print license
#[arg(long)]
#[serde(default)]
pub license: bool,
}

impl Config {
pub fn read() -> anyhow::Result<Self> {
let config = Self::read_from_disk_with_default()?.combine(Self::parse());
let config = Self::read_from_disk_with_default()?;
config.validate()?;

Ok(config)
Expand Down Expand Up @@ -98,7 +55,7 @@ impl Config {
toml::from_str(&contents).context("failed to read config")
}

fn validate(&self) -> anyhow::Result<()> {
pub fn validate(&self) -> anyhow::Result<()> {
if let Some(reset_threshold) = self.reset_threshold() {
if !(0.0..=1.0).contains(&reset_threshold) {
anyhow::bail!("reset threshold should be within 0 and 1")
Expand Down Expand Up @@ -172,21 +129,4 @@ impl Config {
pub fn turn_chance(&self) -> f32 {
self.turn_chance.unwrap_or(0.15)
}

fn combine(self, other: Self) -> Self {
Self {
color_mode: other.color_mode.or(self.color_mode),
palette: other.palette.or(self.palette),
rainbow: other.rainbow.or(self.rainbow),
delay_ms: other.delay_ms.or(self.delay_ms),
fps: other.fps.or(self.fps),
reset_threshold: other.reset_threshold.or(self.reset_threshold),
kinds: other.kinds.or(self.kinds),
bold: other.bold.or(self.bold),
inherit_style: other.inherit_style.or(self.inherit_style),
num_pipes: other.num_pipes.or(self.num_pipes),
turn_chance: other.turn_chance.or(self.turn_chance),
license: self.license || other.license,
}
}
}
Loading

0 comments on commit e611603

Please sign in to comment.