refactor: extract constants
This commit is contained in:
+6
-20
@@ -1,8 +1,7 @@
|
|||||||
|
use crate::constants::SECONDS_PER_MIN;
|
||||||
use crate::msg::Message;
|
use crate::msg::Message;
|
||||||
use crate::notification::send_desktop_notification;
|
use crate::notification::send_desktop_notification;
|
||||||
|
|
||||||
const SECONDS_PER_MIN: u32 = 60;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
pub enum TimerMode {
|
pub enum TimerMode {
|
||||||
Work,
|
Work,
|
||||||
@@ -18,8 +17,7 @@ pub struct Termato {
|
|||||||
pub time_remaining_in_sec: u32,
|
pub time_remaining_in_sec: u32,
|
||||||
pub show_help: bool,
|
pub show_help: bool,
|
||||||
pub enable_notifications: bool,
|
pub enable_notifications: bool,
|
||||||
pub enable_visualizer: bool,
|
should_quit: bool,
|
||||||
pub should_quit: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Termato {
|
impl Termato {
|
||||||
@@ -33,18 +31,16 @@ impl Termato {
|
|||||||
time_remaining_in_sec: work_mins * SECONDS_PER_MIN,
|
time_remaining_in_sec: work_mins * SECONDS_PER_MIN,
|
||||||
show_help: false,
|
show_help: false,
|
||||||
enable_notifications: false,
|
enable_notifications: false,
|
||||||
enable_visualizer: false,
|
|
||||||
should_quit: false,
|
should_quit: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_notifications(mut self, enable: bool) -> Self {
|
pub fn should_quit(&self) -> bool {
|
||||||
self.enable_notifications = enable;
|
self.should_quit
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_visualizer(mut self, enable: bool) -> Self {
|
pub fn with_notifications(mut self, enable: bool) -> Self {
|
||||||
self.enable_visualizer = enable;
|
self.enable_notifications = enable;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +116,6 @@ mod tests {
|
|||||||
assert_eq!(result.time_remaining_in_sec, 1500);
|
assert_eq!(result.time_remaining_in_sec, 1500);
|
||||||
assert!(!result.show_help);
|
assert!(!result.show_help);
|
||||||
assert!(!result.enable_notifications);
|
assert!(!result.enable_notifications);
|
||||||
assert!(!result.enable_visualizer);
|
|
||||||
assert!(!result.should_quit);
|
assert!(!result.should_quit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,13 +268,4 @@ mod tests {
|
|||||||
let result = result.with_notifications(true);
|
let result = result.with_notifications(true);
|
||||||
assert!(result.enable_notifications);
|
assert!(result.enable_notifications);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn termato_visualizer_when_called_it_should_verify_opt_in_behavior() {
|
|
||||||
let result = Termato::new(25, 5);
|
|
||||||
assert!(!result.enable_visualizer);
|
|
||||||
|
|
||||||
let result = result.with_visualizer(true);
|
|
||||||
assert!(result.enable_visualizer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+45
-11
@@ -1,5 +1,16 @@
|
|||||||
use std::env;
|
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)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct CliConfig {
|
pub struct CliConfig {
|
||||||
pub work_mins: u32,
|
pub work_mins: u32,
|
||||||
@@ -30,10 +41,10 @@ impl Cli {
|
|||||||
|
|
||||||
for arg in args.into_iter().skip(1) {
|
for arg in args.into_iter().skip(1) {
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"-h" | "--help" => return CliAction::PrintHelp,
|
FLAG_HELP | FLAG_HELP_LONG => return CliAction::PrintHelp,
|
||||||
"-v" | "--version" => return CliAction::PrintVersion,
|
FLAG_VERSION | FLAG_VERSION_LONG => return CliAction::PrintVersion,
|
||||||
"-n" | "--notify" => enable_notifications = true,
|
FLAG_NOTIFY | FLAG_NOTIFY_LONG => enable_notifications = true,
|
||||||
"-z" | "--visualizer" => enable_visualizer = true,
|
FLAG_VISUALIZER | FLAG_VISUALIZER_LONG => enable_visualizer = true,
|
||||||
"--" => {}
|
"--" => {}
|
||||||
s if s.starts_with('-') => {
|
s if s.starts_with('-') => {
|
||||||
return CliAction::Error(format!("Unknown option: '{}'", s));
|
return CliAction::Error(format!("Unknown option: '{}'", s));
|
||||||
@@ -47,8 +58,8 @@ impl Cli {
|
|||||||
.filter_map(|s| s.parse::<u32>().ok())
|
.filter_map(|s| s.parse::<u32>().ok())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let work_mins = numbers.first().copied().unwrap_or(25);
|
let work_mins = numbers.first().copied().unwrap_or(DEFAULT_WORK_MINS);
|
||||||
let break_mins = numbers.get(1).copied().unwrap_or(5);
|
let break_mins = numbers.get(1).copied().unwrap_or(DEFAULT_BREAK_MINS);
|
||||||
|
|
||||||
if numbers.len() > 2 {
|
if numbers.len() > 2 {
|
||||||
return CliAction::Error(format!("Unexpected argument: '{}'", positional[2]));
|
return CliAction::Error(format!("Unexpected argument: '{}'", positional[2]));
|
||||||
@@ -64,14 +75,37 @@ impl Cli {
|
|||||||
|
|
||||||
pub fn print_help() {
|
pub fn print_help() {
|
||||||
println!("Usage: termato [options] [work_minutes] [break_minutes]");
|
println!("Usage: termato [options] [work_minutes] [break_minutes]");
|
||||||
|
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
println!("Options:");
|
println!("Options:");
|
||||||
println!(" -n, --notify Enable desktop notifications");
|
|
||||||
println!(" -z, --visualizer Display visualizer of playing audio");
|
println!(
|
||||||
println!(" -h, --help Print this help message");
|
" {:<19} Enable desktop notifications",
|
||||||
println!(" -v, --version Print the version number");
|
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!();
|
||||||
println!("Defaults: work_minutes = 25, break_minutes = 5");
|
|
||||||
|
println!(
|
||||||
|
"Defaults: work_minutes = {}, break_minutes = {}",
|
||||||
|
DEFAULT_WORK_MINS, DEFAULT_BREAK_MINS
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_version() {
|
pub fn print_version() {
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
pub const SECONDS_PER_MIN: u32 = 60;
|
||||||
|
pub const DEFAULT_WORK_MINS: u32 = 25;
|
||||||
|
pub const DEFAULT_BREAK_MINS: u32 = 5;
|
||||||
|
pub const VIZ_NUM_BARS: u16 = 64;
|
||||||
+6
-4
@@ -1,6 +1,7 @@
|
|||||||
mod app;
|
mod app;
|
||||||
mod args;
|
mod args;
|
||||||
mod audio;
|
mod audio;
|
||||||
|
mod constants;
|
||||||
mod events;
|
mod events;
|
||||||
mod msg;
|
mod msg;
|
||||||
mod notification;
|
mod notification;
|
||||||
@@ -27,6 +28,8 @@ use ui::render_app;
|
|||||||
|
|
||||||
use args::{Cli, CliAction};
|
use args::{Cli, CliAction};
|
||||||
|
|
||||||
|
use crate::constants::VIZ_NUM_BARS;
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
let config = match Cli::parse() {
|
let config = match Cli::parse() {
|
||||||
CliAction::PrintHelp => {
|
CliAction::PrintHelp => {
|
||||||
@@ -52,14 +55,13 @@ fn main() -> Result<(), io::Error> {
|
|||||||
let font = Toilet::smblock().unwrap();
|
let font = Toilet::smblock().unwrap();
|
||||||
|
|
||||||
let visualizer = if config.enable_visualizer {
|
let visualizer = if config.enable_visualizer {
|
||||||
Some(AudioVisualizer::new(64))
|
Some(AudioVisualizer::new(VIZ_NUM_BARS as usize))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut termato = Termato::new(config.work_mins, config.break_mins)
|
let mut termato = Termato::new(config.work_mins, config.break_mins)
|
||||||
.with_notifications(config.enable_notifications)
|
.with_notifications(config.enable_notifications);
|
||||||
.with_visualizer(config.enable_visualizer);
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
|
|
||||||
@@ -72,7 +74,7 @@ fn main() -> Result<(), io::Error> {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
while !termato.should_quit {
|
while !termato.should_quit() {
|
||||||
terminal.draw(|f| render_app(&termato, f, &font, visualizer.as_ref()))?;
|
terminal.draw(|f| render_app(&termato, f, &font, visualizer.as_ref()))?;
|
||||||
|
|
||||||
if event::poll(Duration::from_millis(16))? {
|
if event::poll(Duration::from_millis(16))? {
|
||||||
|
|||||||
@@ -6,8 +6,11 @@ use ratatui::{
|
|||||||
widgets::{Block, Borders, Clear, Paragraph},
|
widgets::{Block, Borders, Clear, Paragraph},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::app::{Termato, TimerMode};
|
use crate::{audio::AudioVisualizer, constants::VIZ_NUM_BARS};
|
||||||
use crate::audio::AudioVisualizer;
|
use crate::{
|
||||||
|
app::{Termato, TimerMode},
|
||||||
|
constants::SECONDS_PER_MIN,
|
||||||
|
};
|
||||||
|
|
||||||
// use unicode blocks for rendering
|
// use unicode blocks for rendering
|
||||||
// Empty -> " "
|
// Empty -> " "
|
||||||
@@ -52,8 +55,8 @@ pub fn render_app(
|
|||||||
) {
|
) {
|
||||||
let size = f.area();
|
let size = f.area();
|
||||||
|
|
||||||
let minutes = app.time_remaining_in_sec / 60;
|
let minutes = app.time_remaining_in_sec / SECONDS_PER_MIN;
|
||||||
let seconds = app.time_remaining_in_sec % 60;
|
let seconds = app.time_remaining_in_sec % SECONDS_PER_MIN;
|
||||||
let time_str = format!("{:02}:{:02}", minutes, seconds);
|
let time_str = format!("{:02}:{:02}", minutes, seconds);
|
||||||
|
|
||||||
let time_color = if !app.is_running {
|
let time_color = if !app.is_running {
|
||||||
@@ -76,7 +79,7 @@ pub fn render_app(
|
|||||||
let text_height = text_lines.len() as u16;
|
let text_height = text_lines.len() as u16;
|
||||||
|
|
||||||
let viz_height = if visualizer.is_some() { 1 } else { 0 };
|
let viz_height = if visualizer.is_some() { 1 } else { 0 };
|
||||||
let viz_width = 64;
|
let viz_width = VIZ_NUM_BARS;
|
||||||
|
|
||||||
let content_height = text_height + viz_height;
|
let content_height = text_height + viz_height;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user