refactor: extract constants
This commit is contained in:
+45
-11
@@ -1,5 +1,16 @@
|
||||
use std::env;
|
||||
|
||||
use crate::constants::{DEFAULT_BREAK_MINS, DEFAULT_WORK_MINS};
|
||||
|
||||
pub const FLAG_HELP: &str = "-h";
|
||||
pub const FLAG_HELP_LONG: &str = "--help";
|
||||
pub const FLAG_VERSION: &str = "-v";
|
||||
pub const FLAG_VERSION_LONG: &str = "--version";
|
||||
pub const FLAG_NOTIFY: &str = "-n";
|
||||
pub const FLAG_NOTIFY_LONG: &str = "--notify";
|
||||
pub const FLAG_VISUALIZER: &str = "-z";
|
||||
pub const FLAG_VISUALIZER_LONG: &str = "--visualizer";
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct CliConfig {
|
||||
pub work_mins: u32,
|
||||
@@ -30,10 +41,10 @@ impl Cli {
|
||||
|
||||
for arg in args.into_iter().skip(1) {
|
||||
match arg.as_str() {
|
||||
"-h" | "--help" => return CliAction::PrintHelp,
|
||||
"-v" | "--version" => return CliAction::PrintVersion,
|
||||
"-n" | "--notify" => enable_notifications = true,
|
||||
"-z" | "--visualizer" => enable_visualizer = true,
|
||||
FLAG_HELP | FLAG_HELP_LONG => return CliAction::PrintHelp,
|
||||
FLAG_VERSION | FLAG_VERSION_LONG => return CliAction::PrintVersion,
|
||||
FLAG_NOTIFY | FLAG_NOTIFY_LONG => enable_notifications = true,
|
||||
FLAG_VISUALIZER | FLAG_VISUALIZER_LONG => enable_visualizer = true,
|
||||
"--" => {}
|
||||
s if s.starts_with('-') => {
|
||||
return CliAction::Error(format!("Unknown option: '{}'", s));
|
||||
@@ -47,8 +58,8 @@ impl Cli {
|
||||
.filter_map(|s| s.parse::<u32>().ok())
|
||||
.collect();
|
||||
|
||||
let work_mins = numbers.first().copied().unwrap_or(25);
|
||||
let break_mins = numbers.get(1).copied().unwrap_or(5);
|
||||
let work_mins = numbers.first().copied().unwrap_or(DEFAULT_WORK_MINS);
|
||||
let break_mins = numbers.get(1).copied().unwrap_or(DEFAULT_BREAK_MINS);
|
||||
|
||||
if numbers.len() > 2 {
|
||||
return CliAction::Error(format!("Unexpected argument: '{}'", positional[2]));
|
||||
@@ -64,14 +75,37 @@ impl Cli {
|
||||
|
||||
pub fn print_help() {
|
||||
println!("Usage: termato [options] [work_minutes] [break_minutes]");
|
||||
|
||||
println!();
|
||||
|
||||
println!("Options:");
|
||||
println!(" -n, --notify Enable desktop notifications");
|
||||
println!(" -z, --visualizer Display visualizer of playing audio");
|
||||
println!(" -h, --help Print this help message");
|
||||
println!(" -v, --version Print the version number");
|
||||
|
||||
println!(
|
||||
" {:<19} Enable desktop notifications",
|
||||
format_args!("{}, {}", FLAG_NOTIFY, FLAG_NOTIFY_LONG)
|
||||
);
|
||||
|
||||
println!(
|
||||
" {:<19} Display visualizer of playing audio",
|
||||
format_args!("{}, {}", FLAG_VISUALIZER, FLAG_VISUALIZER_LONG)
|
||||
);
|
||||
|
||||
println!(
|
||||
" {:<19} Print this help message",
|
||||
format_args!("{}, {}", FLAG_HELP, FLAG_HELP_LONG)
|
||||
);
|
||||
|
||||
println!(
|
||||
" {:<19} Print the version number",
|
||||
format_args!("{}, {}", FLAG_VERSION, FLAG_VERSION_LONG)
|
||||
);
|
||||
|
||||
println!();
|
||||
println!("Defaults: work_minutes = 25, break_minutes = 5");
|
||||
|
||||
println!(
|
||||
"Defaults: work_minutes = {}, break_minutes = {}",
|
||||
DEFAULT_WORK_MINS, DEFAULT_BREAK_MINS
|
||||
);
|
||||
}
|
||||
|
||||
pub fn print_version() {
|
||||
|
||||
Reference in New Issue
Block a user