From 9503962dd5507afd97d26a796a2a3fe7dac9bf50 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 27 Apr 2020 16:24:33 +0200 Subject: [PATCH] Allow for time adjustments with +/- MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I much prefer this solution to the old one. Parsing times was a mistake. I wonder if rustc realizes that I only ever pass 5 or -5 as arguments to that function and then creates two `time::Duration`s for 5 and -5 minutes respectively to avoid the allocations on each keypress. Also, I had to satisfy the OCD meme by rounding all adjusted times to %5. It would be too annoying to change a number without being able to always set it to a “nice” number. --- src/timesheet.rs | 10 ++++++++++ src/tracc.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/timesheet.rs b/src/timesheet.rs index 753707e..f53df77 100644 --- a/src/timesheet.rs +++ b/src/timesheet.rs @@ -77,6 +77,16 @@ impl TimeSheet { self.times.iter().map(TimePoint::to_string).collect() } + /** + * Adjust the current time by `minutes` and round the result to a multiple of `minutes`. + * This is so I can adjust in steps of 5 but still get nice, even numbers in the output. + */ + pub fn shift_current(&mut self, minutes: i64) { + let time = &mut self.times[self.selected].time; + let current_minute = time.minute() as i64; + *time += Duration::minutes(minutes - current_minute % minutes); + } + fn current(&self) -> &TimePoint { &self.times[self.selected] } diff --git a/src/tracc.rs b/src/tracc.rs index 8691217..d7b88d2 100644 --- a/src/tracc.rs +++ b/src/tracc.rs @@ -69,6 +69,8 @@ impl Tracc { } Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?, Key::Char(' ') if self.focus == Focus::Top => self.todos.toggle_current(), + Key::Char('-') if self.focus == Focus::Bottom => self.times.shift_current(-5), + Key::Char('+') if self.focus == Focus::Bottom => self.times.shift_current(5), // dd Key::Char('d') => { if let Some(Ok(Key::Char('d'))) = inputs.next() {