Skip to content

Commit 5046f62

Browse files
committed
feat: Add command to change current timer to specific value
1 parent fcca9d0 commit 5046f62

File tree

11 files changed

+76
-0
lines changed

11 files changed

+76
-0
lines changed

src/ipc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ pub enum ClientToServerMsg {
6767

6868
/// Command the server to postpone the current break, if possible
6969
PostPone,
70+
71+
/// Sets current timer to a specific time (in seconds)
72+
SetTimer(u64)
7073
}
7174

7275
/// Service handling communication between processes over the zentime socket.

src/main.rs

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use subcommands::{
1111
query_server_once::query_server_once,
1212
reset_timer::reset_timer,
1313
server::{start_daemonized, status, stop},
14+
set_timer::set_timer,
1415
skip_timer::skip_timer,
1516
toggle_timer::toggle_timer,
1617
};
@@ -165,6 +166,9 @@ enum Commands {
165166
/// Postpones the current break (if possible)
166167
Postpone,
167168

169+
/// Sets current timer to a specific time in seconds
170+
SetTimer { time: u64 },
171+
168172
/// Interact with the zentime server
169173
Server {
170174
#[command(subcommand)]
@@ -233,6 +237,10 @@ fn main() {
233237
reset_timer(config.view.silent);
234238
}
235239

240+
Some(Commands::SetTimer { time }) => {
241+
set_timer(config.view.silent, time.to_owned());
242+
}
243+
236244
None => default_cmd(&cli.common_args, config),
237245
}
238246
}

src/server/start.rs

+7
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ async fn handle_client_to_server_msg(
227227
ClientToServerMsg::Sync => {
228228
info!("Client synced with server");
229229
}
230+
231+
// Set timer to a specific time
232+
ClientToServerMsg::SetTimer(time) => {
233+
timer_input_sender
234+
.send(PomodoroTimerAction::SetTimer(time))
235+
.context("Could not send SetTimer to timer")?;
236+
},
230237
}
231238

232239
Ok(CloseConnection::No)

src/subcommands.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod postpone;
22
pub mod query_server_once;
33
pub mod reset_timer;
44
pub mod server;
5+
pub mod set_timer;
56
pub mod skip_timer;
67
pub mod toggle_timer;

src/subcommands/set_timer.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use futures::io::BufReader;
2+
use zentime_rs::client::one_shot_connection::one_shot_connection;
3+
use zentime_rs::ipc::ClientToServerMsg;
4+
use zentime_rs::ipc::InterProcessCommunication;
5+
use zentime_rs::ipc::ServerToClientMsg;
6+
7+
#[tokio::main]
8+
pub async fn set_timer(silent: bool, time: u64) {
9+
let (reader, mut writer) = match one_shot_connection().await {
10+
Ok(c) => c,
11+
Err(error) => panic!("Could not connect to server: {}", error),
12+
};
13+
14+
let mut reader = BufReader::new(reader);
15+
16+
if let Err(err) =
17+
InterProcessCommunication::send_ipc_message(ClientToServerMsg::SetTimer(time), &mut writer).await
18+
{
19+
panic!("Could not send to the server: {}", err)
20+
};
21+
22+
let msg_result =
23+
InterProcessCommunication::recv_ipc_message::<ServerToClientMsg>(&mut reader).await;
24+
25+
if !silent {
26+
if let Ok(ServerToClientMsg::Timer(state)) = msg_result {
27+
println!(
28+
"{} {} {}",
29+
state.round,
30+
state.time,
31+
if state.is_break { "Break" } else { "Focus" }
32+
);
33+
}
34+
}
35+
36+
InterProcessCommunication::send_ipc_message(ClientToServerMsg::Detach, &mut writer)
37+
.await
38+
.ok();
39+
}

timer/src/pomodoro_timer/long_break.rs

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl PomodoroActionHandler<LongBreak> for LongBreakTickHandler {
5959
}
6060
PomodoroTimerAction::PlayPause => Some(TimerAction::PlayPause),
6161
PomodoroTimerAction::Skip => Some(TimerAction::End),
62+
PomodoroTimerAction::SetTimer(time) => Some(TimerAction::SetTimer(time)),
6263

6364
PomodoroTimerAction::ResetTimer => {
6465
PomodoroTimer::<Interval>::reset(config, callbacks).init();

timer/src/pomodoro_timer/on_tick_handler.rs

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub trait PomodoroActionHandler<S: PomodoroState> {
3737
None
3838
}
3939

40+
PomodoroTimerAction::SetTimer(time) => Some(TimerAction::SetTimer(time)),
41+
4042
_ => None,
4143
}
4244
}

timer/src/pomodoro_timer/short_break.rs

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ impl PomodoroActionHandler<ShortBreak> for ShortBreakTickHandler {
6565
None
6666
}
6767

68+
PomodoroTimerAction::SetTimer(time) => Some(TimerAction::SetTimer(time)),
69+
6870
_ => None,
6971
}
7072
}

timer/src/pomodoro_timer_action.rs

+3
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ pub enum PomodoroTimerAction {
1717

1818
/// Postpone a break
1919
PostponeBreak,
20+
21+
/// Set current timer to a specific time in seconds
22+
SetTimer(u64),
2023
}

timer/src/timer.rs

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl Timer<Paused> {
160160
current_time: CurrentTime(seconds_to_time(time)),
161161
}) {
162162
match action {
163+
TimerAction::SetTimer(time) => {
164+
self.internal_state.remaining_time = Duration::from_secs(time)
165+
}
166+
163167
TimerAction::PlayPause => {
164168
self.unpause();
165169
break;
@@ -240,6 +244,9 @@ impl Timer<Running> {
240244
// Returns from the blocking loop, so that the calling code
241245
// can resume execution
242246
TimerAction::End => return,
247+
TimerAction::SetTimer(time) => {
248+
self.internal_state.target_time = Instant::now() + Duration::from_secs(time)
249+
}
243250
}
244251
}
245252
}

timer/src/timer_action.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
/// Various control actions to transition into new states
44
#[derive(Debug, Copy, Clone)]
55
pub enum TimerAction {
6+
/// Set current timer to a specific time in seconds
7+
SetTimer(u64),
8+
69
/// Either start or pause the current timer
710
PlayPause,
811

0 commit comments

Comments
 (0)