This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpomodoro.rs
126 lines (113 loc) · 2.9 KB
/
pomodoro.rs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use serde::de::Deserialize;
use std::convert::TryInto;
use std::time::Duration;
use tokio::sync::mpsc;
use chrono::offset::{Local, Utc};
use chrono::Locale;
use chrono_tz::Tz;
use super::{BlockEvent, BlockMessage};
use crate::click::MouseButton;
use crate::config::SharedConfig;
use crate::errors::*;
use crate::widgets::widget::Widget;
#[derive(serde_derive::Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
struct PomodoroConfig {
// FIXME
}
impl Default for PomodoroConfig {
fn default() -> Self {
Self {}
}
}
pub async fn run(
id: usize,
block_config: toml::Value,
shared_config: SharedConfig,
message_sender: mpsc::Sender<BlockMessage>,
mut events_receiver: mpsc::Receiver<BlockEvent>,
) -> Result<()> {
let _block_config = PomodoroConfig::deserialize(block_config).block_config_error("pomodoro")?;
let mut text = Widget::new(id, shared_config).with_icon("pomodoro")?;
// Send collaped block
message_sender
.send(BlockMessage {
id,
widgets: vec![text.get_data()],
})
.await
.internal_error("pomodoro", "failed to send message")?;
// Wait for left click
loop {
if let Some(BlockEvent::I3Bar(click)) = events_receiver.recv().await {
if click.button == MouseButton::Left {
break;
}
}
}
// Read task length
let task_len = read_usize(
id,
&mut text,
&message_sender,
&mut events_receiver,
25,
"Task length:",
)
.await?;
// Read break length
let break_len = read_usize(
id,
&mut text,
&message_sender,
&mut events_receiver,
5,
"Break length:",
)
.await?;
update(
id,
&mut text,
&&message_sender,
format!("tark_len={} and break_len={}", task_len, break_len),
)
.await?;
Ok(())
}
async fn read_usize(
id: usize,
widget: &mut Widget,
sender: &mpsc::Sender<BlockMessage>,
receiver: &mut mpsc::Receiver<BlockEvent>,
mut number: usize,
msg: &str,
) -> Result<usize> {
loop {
update(id, widget, sender, format!("{} {}", msg, number)).await?;
if let Some(BlockEvent::I3Bar(click)) = receiver.recv().await {
match click.button {
MouseButton::Left => break,
MouseButton::WheelUp => number += 1,
MouseButton::WheelDown => number = number.saturating_sub(1),
_ => (),
}
}
}
Ok(number)
}
async fn update(
id: usize,
widget: &mut Widget,
sender: &mpsc::Sender<BlockMessage>,
text: String,
) -> Result<()> {
widget.set_text(text);
sender
.send(BlockMessage {
id,
widgets: vec![widget.get_data()],
})
.await
.internal_error("pomodoro", "failed to send message")?;
Ok(())
}