This commit is contained in:
Dr. Matthias Ratajczak
2023-03-02 17:28:56 +01:00
parent 870becc70b
commit 0b1514d496
2 changed files with 38 additions and 34 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ pub(crate) enum Interval {
impl Interval { impl Interval {
const FIVE_MINUTES: Duration = Duration::from_secs(5 * 60); const FIVE_MINUTES: Duration = Duration::from_secs(5 * 60);
const FIFTEEN_MINUTES: Duration = Duration::from_secs(15 * 60); const FIFTEEN_MINUTES: Duration = Duration::from_secs(15 * 60);
const TWENTY_FIVE_MINUTES: Duration = Duration::from_secs(25 * 60); const TWENTY_FIVE_MINUTES: Duration = Duration::from_secs(5);
pub(crate) fn default_sequence() -> [Self; 8] { pub(crate) fn default_sequence() -> [Self; 8] {
[ [
+37 -33
View File
@@ -62,50 +62,28 @@ impl Pomodoro {
pub(crate) async fn main_loop(mut self) -> Result<()> { pub(crate) async fn main_loop(mut self) -> Result<()> {
loop { loop {
'inner: loop { 'current_interval: loop {
self.show_status()?; self.show_status()?;
let should_quit = self.run_inner_tasks().await?;
tokio::select! (
_ = sleep(ONE_SECOND) => {
if !self.paused {
self.remaining -= ONE_SECOND;
}
},
message = self.receiver.recv() => {
if let Ok(m) = message{
match m {
Message::Quit => {
return Ok(());
},
Message::TogglePause => {
self.toggle_pause();
continue;
}
}
} else {
bail!("<terminate>");
}
}
);
if self.remaining.is_zero() { if self.remaining.is_zero() {
self.show_status()?; break 'current_interval;
break 'inner; }
if should_quit {
return Ok(());
} }
} }
self.next_interval(); let current_interval = self.next_interval();
self.remaining = self.current_interval().get_duration(); self.remaining = current_interval.get_duration();
self.send_notification(self.current_interval().get_message())?; self.send_notification(current_interval.get_message())?;
} }
} }
fn next_interval(&mut self) { fn next_interval(&mut self) -> Interval {
self.interval_idx += 1; self.interval_idx += 1;
self.interval_idx %= self.intervals.len(); self.interval_idx %= self.intervals.len();
}
fn current_interval(&self) -> Interval {
self.intervals[self.interval_idx] self.intervals[self.interval_idx]
} }
@@ -115,6 +93,32 @@ impl Pomodoro {
command.status()?; command.status()?;
Ok(()) Ok(())
} }
async fn run_inner_tasks(&mut self) -> Result<bool> {
tokio::select! (
_ = sleep(ONE_SECOND) => {
if !self.paused {
self.remaining = self.remaining.saturating_sub(ONE_SECOND);
}
return Ok(false);
},
message = self.receiver.recv() => {
if let Ok(m) = message{
match m {
Message::Quit => {
return Ok(true);
},
Message::TogglePause => {
self.toggle_pause();
return Ok(false);
}
}
} else {
bail!("<terminate>");
}
}
)
}
} }
fn format(dur: Duration) -> String { fn format(dur: Duration) -> String {