From a42e1130a1e4f11add8186a756664e562d0c6b37 Mon Sep 17 00:00:00 2001 From: shy Date: Fri, 9 Apr 2021 18:33:42 +0200 Subject: [PATCH] Some minor improvements. --- src/alarm.rs | 26 ++++++++++++-------------- src/main.rs | 23 ++++++++++++----------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/alarm.rs b/src/alarm.rs index 7e70c06..8575b34 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -86,8 +86,6 @@ impl AlarmRoster { if let Some(i) = input.find('/') { label = input[(i + 1)..].trim().to_string(); - // TODO: Make decision yes/no. - //label.truncate(24); time_str = &input[..i].trim(); } else { label = input.clone(); @@ -162,17 +160,19 @@ impl AlarmRoster { pub fn check(&mut self, clock: &mut Clock, layout: &Layout, - countdown: &mut Countdown) -> Option<(u32, String)> { - - let mut ret: Option<(u32, String)> = None; + countdown: &mut Countdown, + ) -> Option<(u32, &String)> + { + let mut ret = None; let mut index = 0; + let size = self.list.len() as u16; for alarm in &mut self.list { // Ignore alarms marked exceeded. if !alarm.exceeded { if alarm.time <= clock.elapsed { // Found alarm to raise. - ret = Some((alarm.time, alarm.label.clone())); + ret = Some((alarm.time, &alarm.label)); alarm.exceeded = true; clock.color_index = Some(alarm.color_index); countdown.value = 0; @@ -194,9 +194,7 @@ impl AlarmRoster { // Compensate for "hidden" items in the alarm roster. // TODO: Make this more elegant and robust. - if let Some(offset) = (self.list.len() as u16) - .checked_sub(layout.roster_height + 1) { - + if let Some(offset) = size.checked_sub(layout.roster_height + 1) { if index <= offset{ // Draw next to placeholder ("[...]"). line = layout.roster.line; @@ -248,7 +246,7 @@ impl AlarmRoster { color::Bg(COLOR[alarm.color_index]), color::Bg(color::Reset), style::Bold, - alarm.label, + &alarm.label, style::Reset) .unwrap(); } else { @@ -257,7 +255,7 @@ impl AlarmRoster { cursor::Goto(layout.roster.col, layout.roster.line + index), color::Bg(COLOR[alarm.color_index]), color::Bg(color::Reset), - alarm.label) + &alarm.label) .unwrap(); } index += 1; @@ -271,8 +269,8 @@ impl AlarmRoster { let length = str_length(&alarm.label); if length > width { width = length }; } - // Actual width is 3 columns wider if it's not 0. - if width == 0 { 0 } else { width.saturating_add(3) } + // Actual width is 4 columns wider if it's not 0. + if width == 0 { 0 } else { width.saturating_add(4) } } // Reset every alarm. @@ -284,7 +282,7 @@ impl AlarmRoster { } // Execute the command given on the command line. -pub fn exec_command(config: &Config, elapsed: u32, label: String) -> Option { +pub fn exec_command(config: &Config, elapsed: u32, label: &String) -> Option { let mut args: Vec = Vec::new(); let time: String; diff --git a/src/main.rs b/src/main.rs index 17a1402..f9f0053 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,10 +26,11 @@ use common::{Config, str_length}; const NAME: &str = env!("CARGO_PKG_NAME"); const VERSION: &str = env!("CARGO_PKG_VERSION"); const USAGE: &str = concat!("USAGE: ", env!("CARGO_PKG_NAME"), -" [-h|-v] [-e|--exec COMMAND] [-p] [-q] [ALARM TIME(s)] +" [-h|-v] [-e|--exec COMMAND] [-p] [-q] [ALARM[/LABEL]] PARAMETERS: - [ALARM TIME] None or multiple alarm times (HH:MM:SS). + [ALARM TIME[/LABEL]] Any number of alarm times (HH:MM:SS) with optional + label. OPTIONS: -h, --help Show this help. @@ -280,7 +281,7 @@ fn main() { // Run command if configured. if config.command.is_some() { if spawned.is_none() { - spawned = exec_command(&config, time, label); + spawned = exec_command(&config, time, &label); } else { // The last command is still running. eprintln!("Not executing command, as its predecessor is still running"); @@ -444,7 +445,7 @@ fn parse_args(config: &mut Config, alarm_roster: &mut AlarmRoster) { // for process::Command::new(). fn parse_to_command(input: &str) -> Vec { let mut command: Vec = Vec::new(); - let mut subs: String = String::new(); + let mut buffer: String = String::new(); let mut quoted = false; let mut escaped = false; @@ -455,22 +456,22 @@ fn parse_to_command(input: &str) -> Vec { escaped = true; continue; }, - ' ' if escaped || quoted => { &subs.push(' '); }, + ' ' if escaped || quoted => { &buffer.push(' '); }, ' ' => { - if !&subs.is_empty() { - command.push(subs.clone()); - &subs.clear(); + if !&buffer.is_empty() { + command.push(buffer.clone()); + &buffer.clear(); } }, '"' | '\'' if !escaped => quoted = !quoted, _ => { - if escaped { &subs.push('\\'); } - &subs.push(byte); + if escaped { &buffer.push('\\'); } + &buffer.push(byte); }, } escaped = false; } - command.push(subs); + command.push(buffer); command.shrink_to_fit(); command }