diff options
Diffstat (limited to 'src/client')
-rw-r--r-- | src/client/command.rs | 37 | ||||
-rw-r--r-- | src/client/listener.rs | 73 | ||||
-rw-r--r-- | src/client/mod.rs | 19 |
3 files changed, 129 insertions, 0 deletions
diff --git a/src/client/command.rs b/src/client/command.rs new file mode 100644 index 0000000..030d33a --- /dev/null +++ b/src/client/command.rs @@ -0,0 +1,37 @@ +/* Polydoro - Pomodoro widget for polybar and friends + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +use std::os::unix::net::UnixStream; +use std::io::Write; + +pub fn handle_command(c: String) { + // check if the sock exists or not + let sock = "/tmp/polydorocmd.sock"; + if !std::fs::metadata(sock).is_ok() { + eprintln!("Could not connect to socket {}, is the daemon running?", sock); + std::process::exit(1); + } + + let mut stream = UnixStream::connect(sock) + .unwrap_or_else(|_| panic!("could not connect to daemon at {}", sock)); + + if let Err(e) = stream.write_all(format!("{}\n", c).as_bytes()) { + eprintln!("error: {}", e); + std::process::exit(1); + } + let _ = stream.flush(); +} diff --git a/src/client/listener.rs b/src/client/listener.rs new file mode 100644 index 0000000..2e223f6 --- /dev/null +++ b/src/client/listener.rs @@ -0,0 +1,73 @@ +/* Polydoro - Pomodoro widget for polybar and friends + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +use std::os::unix::net::UnixStream; +use std::io::{BufReader}; + +use bincode::{decode_from_std_read, config}; +use crate::daemon::pomo::{Pomo,State}; + +pub fn start_listener() { + let sock_path = "/tmp/polydorod.sock"; + let stream = UnixStream::connect(sock_path).unwrap_or_else(|_| panic!("could not connect to daemon")); + let mut reader = BufReader::new(stream); + + loop { + // blocks until a struct is received + match decode_from_std_read::<Pomo, _, _>(&mut reader, config::standard()) { + Ok(p) => { + pretty_print(p); + } + Err(e) => { + eprintln!("polydoro listener error: {}", e); + std::process::exit(1); + } + } + } +} + +pub fn format_time(secs: u64) -> String { + format!("{:02}:{:02}", secs / 60, secs % 60) +} + +pub fn pretty_print(p: Pomo) { + let format_work = "work"; + let format_work_paused = "[work]"; + let format_work_idle = "(work)"; + let format_break = "break"; + let format_break_paused = "[break]"; + let format_break_idle = "(break)"; + let format_long_break = "long break"; + let format_long_break_paused = "[long break]"; + let format_long_break_idle = "(long break)"; + + let f = match p.current_state { + State::Work => format_work, + State::Break => format_break, + State::LongBreak => format_long_break, + + State::WorkPaused => format_work_paused, + State::BreakPaused => format_break_paused, + State::LongBreakPaused => format_long_break_paused, + + State::WorkIdle => format_work_idle, + State::BreakIdle => format_break_idle, + State::LongBreakIdle => format_long_break_idle, + }; + + println!("{} {} - {}", f, p.counter, format_time(p.secs_elapsed)); +} diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 0000000..b86d042 --- /dev/null +++ b/src/client/mod.rs @@ -0,0 +1,19 @@ +/* Polydoro - Pomodoro widget for polybar and friends + * Copyright (C) 2025 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +pub mod command; +pub mod listener; |