Skip to content

Commit

Permalink
src: use clap's default value mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelohdez committed Jun 12, 2024
1 parent 8b0ad1a commit f1338c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::{process, thread, time::Duration};

use anyhow::{anyhow, Context};
use clap::Parser;
use dim_screen::{
dim::DimData,
opts::{DimOpts, DEFAULT_ALPHA, DEFAULT_DURATION},
};
use dim_screen::{dim::DimData, opts::DimOpts};
use smithay_client_toolkit::{
compositor::CompositorState,
reexports::client::{globals::registry_queue_init, Connection},
Expand All @@ -30,10 +27,9 @@ fn main() -> anyhow::Result<()> {
let compositor = CompositorState::bind(&globals, &qh).context("Compositor not available")?;
let layer_shell = LayerShell::bind(&globals, &qh).context("Layer shell failed?")?;

let alpha = args.alpha.unwrap_or(DEFAULT_ALPHA);
let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha);
let alpha = args.alpha;
let duration = args.duration;

let duration = args.duration.unwrap_or(DEFAULT_DURATION);
// A duration of 0 is considered as infinite, not starting the timer:
if duration > 0 {
thread::spawn(move || {
Expand All @@ -42,6 +38,7 @@ fn main() -> anyhow::Result<()> {
});
}

let mut data = DimData::new(compositor, &globals, &qh, layer_shell, alpha);
while !data.should_exit() {
event_queue
.blocking_dispatch(&mut data)
Expand Down
16 changes: 8 additions & 8 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ use std::{
use clap::{CommandFactory, Parser, ValueEnum};
use clap_complete::{generate_to, Shell};

/// Default duration in seconds
pub const DEFAULT_DURATION: u64 = 30;

pub const DEFAULT_ALPHA: f32 = 0.5;
const DEFAULT_DURATION: u64 = 30;
const DEFAULT_ALPHA: f32 = 0.5;

#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct DimOpts {
#[arg(
short,
long,
help = format!("Duration in seconds, 0 is infinite, default is {DEFAULT_DURATION}")
default_value_t = DEFAULT_DURATION,
help = "Duration in seconds, 0 is infinite"
)]
pub duration: Option<u64>,
pub duration: u64,

#[arg(
short,
long,
help = format!("0.0 is transparent, 1.0 is opaque, default is {DEFAULT_ALPHA}")
default_value_t = DEFAULT_ALPHA,
help = "0.0 is transparent, 1.0 is opaque"
)]
pub alpha: Option<f32>,
pub alpha: f32,

#[arg(long, value_name = "PATH", help = "Generate completions at given path")]
pub gen_completions: Option<PathBuf>,
Expand Down

0 comments on commit f1338c2

Please sign in to comment.