Allow for time adjustments with +/-

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.
This commit is contained in:
kageru 2020-04-27 16:24:33 +02:00
parent a94d8b77ea
commit 9503962dd5
2 changed files with 12 additions and 0 deletions

@ -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]
}

@ -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() {