1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/* 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 std::env;
use bincode::{decode_from_std_read, config};
use crate::daemon::pomo::{Pomo,State};
struct Format {
format_work: String,
format_break: String,
format_long_break: String,
format_work_paused: String,
format_break_paused: String,
format_long_break_paused: String,
format_work_idle: String,
format_break_idle: String,
format_long_break_idle: String,
}
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);
let f = Format {
format_work: get_envvar("FORMAT_WORK", "WORK {counter} - {time}"),
format_break: get_envvar("FORMAT_BREAK", "BREAK {counter} - {time}"),
format_long_break: get_envvar("FORMAT_LONG_BREAK", "LONG BREAK {counter} - {time}"),
format_work_paused: get_envvar("FORMAT_WORK_PAUSED", "[WORK] {counter} - {time}"),
format_break_paused: get_envvar("FORMAT_BREAK_PAUSED", "[BREAK] {counter} - {time}"),
format_long_break_paused: get_envvar("FORMAT_LONG_BREAK_PAUSED", "[LONG BREAK] {counter} - {time}"),
format_work_idle: get_envvar("FORMAT_WORK_IDLE", "(WORK) {counter}"),
format_break_idle: get_envvar("FORMAT_BREAK_IDLE", "(BREAK) {counter}"),
format_long_break_idle: get_envvar("FORMAT_LONG_BREAK_IDLE", "(LONG BREAK) {counter}"),
};
loop {
// blocks until a struct is received
match decode_from_std_read::<Pomo, _, _>(&mut reader, config::standard()) {
Ok(p) => {
pretty_print(&f, p);
}
Err(e) => {
eprintln!("polydoro listener error: {}", e);
std::process::exit(1);
}
}
}
}
fn format_time(elapsed_secs: u16, total_secs: u16) -> String {
let secs = total_secs - elapsed_secs;
format!("{:02}:{:02}", secs / 60, secs % 60)
}
fn get_envvar(key: &str, def: &str) -> String {
env::var(key).unwrap_or_else(|_| def.to_string())
}
fn pretty_print(f: &Format, p: Pomo) {
let format = match p.current_state {
State::Work => &f.format_work,
State::Break => &f.format_break,
State::LongBreak => &f.format_long_break,
State::WorkPaused => &f.format_work_paused,
State::BreakPaused => &f.format_break_paused,
State::LongBreakPaused => &f.format_long_break_paused,
State::WorkIdle => &f.format_work_idle,
State::BreakIdle => &f.format_break_idle,
State::LongBreakIdle => &f.format_long_break_idle,
};
let total_secs: u16 = match p.current_state {
State::WorkIdle | State::Work | State::WorkPaused => p.work_duration,
State::BreakIdle | State::Break | State::BreakPaused => p.break_duration,
State::LongBreakIdle | State::LongBreak | State::LongBreakPaused => p.long_break_duration,
};
println!("{}", format
.replace("{time}", format_time(p.secs_elapsed, total_secs).as_str())
.replace("{counter}", format!("{}", p.counter).as_str()));
}
|