change to subcommand mode

This commit is contained in:
race604 2022-07-19 13:58:25 +08:00
parent a08b687a87
commit b4e7886a9c

View file

@ -1,4 +1,5 @@
use chrono::Duration; use chrono::Duration;
use clap::Subcommand;
use crossterm::event::KeyCode; use crossterm::event::KeyCode;
use regex::Regex; use regex::Regex;
use tui::backend::Backend; use tui::backend::Backend;
@ -12,21 +13,26 @@ use self::modes::Timer;
pub(crate) mod modes; pub(crate) mod modes;
#[derive(clap::ArgEnum, Clone)] #[derive(Debug, Subcommand)]
pub(crate) enum Mode { pub(crate) enum Mode {
/// The clock mode displays the current time, the default mode.
Clock, Clock,
Timer, /// The timer mode displays the remaining time until the timer is finished.
Timer {
#[clap(short, long, value_parser = parse_duration, default_value = "5m")]
duration: Duration,
},
/// The stopwatch mode displays the elapsed time since it was started.
Stopwatch, Stopwatch,
} }
#[derive(clap::Parser)] #[derive(clap::Parser)]
#[clap(about = "A clock app in terminal", long_about = None)]
pub(crate) struct App { pub(crate) struct App {
#[clap(short, long, value_parser, arg_enum, default_value = "clock")] #[clap(subcommand)]
pub mode: Mode, pub mode: Option<Mode>,
#[clap(short, long, value_parser = parse_color, default_value = "green")] #[clap(short, long, value_parser = parse_color, default_value = "green")]
pub color: Color, pub color: Color,
#[clap(short, long, value_parser = parse_duration, default_value = "5m")]
pub duration: Duration,
#[clap(short, long, value_parser, default_value = "1")] #[clap(short, long, value_parser, default_value = "1")]
pub size: u16, pub size: u16,
@ -41,7 +47,8 @@ pub(crate) struct App {
impl App { impl App {
pub fn init_app(&mut self) { pub fn init_app(&mut self) {
let style = Style::default().fg(self.color); let style = Style::default().fg(self.color);
match self.mode { let mode = self.mode.as_ref().unwrap_or(&Mode::Clock);
match mode {
Mode::Clock => { Mode::Clock => {
self.clock = Some(Clock { self.clock = Some(Clock {
size: self.size, size: self.size,
@ -49,8 +56,8 @@ impl App {
long: false, long: false,
}); });
} }
Mode::Timer => { Mode::Timer { duration } => {
self.timer = Some(Timer::new(self.duration, self.size, style)); self.timer = Some(Timer::new(duration.to_owned(), self.size, style));
} }
Mode::Stopwatch => { Mode::Stopwatch => {
self.stopwatch = Some(Stopwatch::new(self.size, style)); self.stopwatch = Some(Stopwatch::new(self.size, style));