Skip to content

Commit af2d96c

Browse files
committed
rewritten in rust
1 parent 50bce94 commit af2d96c

File tree

5 files changed

+323
-156
lines changed

5 files changed

+323
-156
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "waybar_timer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
serde_dispatch = { git = "https://github.com/jbirnick/serde-dispatch" }
8+
clap = { version = "4.5", features = ["derive"] }
9+
time = { version = "0.3.37", features = ["formatting", "local-offset", "macros"] }
10+
serde = { version = "1.0.217", features = ["derive"] }
11+
notify-rust = "4.11.3"
12+
bincode = "1.3.3"

README.md

+44-53
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# waybar-timer
22

3-
This script implements a **simple** and **customizable** timer for your bar.
4-
5-
- specify a command to execute when the timer expires (e.g. notify-send, shell script, ...)
6-
- interactive:
7-
* e.g. scroll to increase / decrease timer
8-
* click to start predefined timers
9-
* while changing a timer a notification displays when the timer will expire
10-
* pause timer
3+
> [!NOTE]
4+
> This used to be a shell script. Now it is a binary.
5+
> The CLI arguments have changed only slightly but the underlying architecture is completely different.
6+
> Therefore, if you switch from the shell script version to the binary, please make sure to **fully adapt the new default config**.
7+
> In particular you need to remove `interval`, set `exec-on-event` to false, change the actions of your timers to just names (no more actions), and change `increase -60` to `decrease 60`.
8+
9+
This script implements a **simple** and **interactive** timer for your bar:
10+
- e.g. scroll to increase / decrease timer
11+
- click to start predefined timers
12+
- while changing a timer a notification displays when the timer will expire
13+
- pause timer
1114

1215
![screenshot set timer](screenshots/setTimer.gif) (set a timer)
1316

@@ -25,37 +28,30 @@ You can **customize behaviour and appearance in a simple way**.
2528

2629
Use cases: pomodoro timer, self-reminder when next meeting begins, tea/pasta timer, ...
2730

28-
## Dependencies
29-
30-
This script works perfectly **without any dependencies**.
31-
3231
## Installation
3332

34-
1. Download [waybar-timer.sh](https://raw.githubusercontent.com/jbirnick/waybar-timer/master/waybar-timer.sh) from this repo.
35-
2. Make it executable. (`chmod +x waybar-timer.sh`)
36-
3. Copy-paste the [example configuration](#example-configuration) from below into your waybar config and style it.
37-
4. Customize. (see [Customization section](#customization))
33+
1. [Download the binary](TODO) from the releases (or build it yourself with cargo) and put it in a directory of your choice (e.g. `~/.scripts/`).
34+
2. Copy-paste the [example configuration](#example-configuration) from below into your waybar config and style it.
35+
3. Customize. (see [Customization section](#customization))
3836

3937
## Example Configuration
4038

4139
```json
4240
"custom/timer": {
43-
"exec": "/path/to/waybar-timer.sh updateandprint",
44-
"exec-on-event": true,
41+
"exec": "/path/to/waybar-timer tail",
42+
"exec-on-event": false,
4543
"return-type": "json",
46-
"interval": 5,
47-
"signal": 4,
4844
"format": "{icon} {0}",
4945
"format-icons": {
5046
"standby": "STANDBY",
5147
"running": "RUNNING",
5248
"paused": "PAUSE"
5349
},
54-
"on-click": "/path/to/waybar-timer.sh new 25 'notify-send \"Session finished\"'",
55-
"on-click-middle": "/path/to/waybar-timer.sh cancel",
56-
"on-click-right": "/path/to/waybar-timer.sh togglepause",
57-
"on-scroll-up": "/path/to/waybar-timer.sh increase 60 || /path/to/waybar-timer.sh new 1 'notify-send -u critical \"Timer expired.\"'",
58-
"on-scroll-down": "/path/to/waybar-timer.sh increase -60"
50+
"on-click": "/path/to/waybar-timer new 25 Pomodoro",
51+
"on-click-middle": "/path/to/waybar-timer cancel",
52+
"on-click-right": "/path/to/waybar-timer togglepause",
53+
"on-scroll-up": "/path/to/waybar-timer increase 60 || /path/to/waybar-timer new 1",
54+
"on-scroll-down": "/path/to/waybar-timer decrease 60"
5955
}
6056
```
6157
The first modification you probably want to make is to replace the `format-icons` by some actually stylish icons.
@@ -72,53 +68,48 @@ Furthermore you can style the module using the `timer` class, for example:
7268

7369
## Customization
7470

75-
The example configuration implements a 25min "pomodoro session" timer with left click, pausing with right click, canceling with middle click, and a normal timer by just scrolling up from the standby mode.
71+
The example configuration implements a 25min "pomodoro session" timer with left click, pausing with right click, canceling with middle click, and an unnamed timer by just scrolling up from the standby mode.
7672

77-
You can customize the different strings, numbers and actions to your own flavor and needs. To understand what the commands do and to implement some different behaviour see the [documentation](#documentation).
73+
You can customize the different numbers and names to your own flavor and needs. To understand what the commands do and to implement some different behaviour see the [documentation](#documentation).
7874

79-
If you want to do some really specific stuff and add some functionality, just edit the script. It is really simple. Just take your 10 minutes to understand what it does and then customize it.
75+
If you need a specific functionality feel free to open an issue and maybe we can make it happen.
8076

8177
## Documentation
8278

83-
Notation: `<...>` are necessary arguments. `[...=DEFAULTVALUE]` are optional arguments,
84-
and if you do not specify them their `DEFAULTVALUE` is used.
79+
Notation: `<...>` are necessary arguments and `[...]` are optional arguments.
8580

8681
The main command of the script is:
8782

88-
- #### `updateandprint`
89-
This routine will return the current the output (i.e. what you see on the bar) and handle the `ACTION` if the timer expired.
90-
Namely:
91-
1. If there is a timer running and its expiry time is <= now, then it executes `ACTION` and kills the timer.
92-
2. It prints the output info for waybar, in particular the number of remaining minutes.
83+
- #### `tail`
84+
This is the command which you want to put in your waybar `exec` field.
85+
It keeps the state of the timer and regularly outputs it in JSON, so that waybar can render it.
86+
We will call the process which runs this `tail` routine the *tail process*.
9387

9488
Now the following commands allow you to control the timer.
9589

96-
- #### `new <MINUTES> [ACTION=""]`
97-
1. If there is a timer already running this timer gets killed.
98-
2. Creates a timer of length `MINUTES` minutes and sets its action to `ACTION`. (`ACTION` will be executed once the timer expires.)
90+
- #### `new <MINUTES> [NAME]`
91+
Creates a new timer of length `MINUTES` minutes.
9992

10093
- #### `increase <SECONDS>`
101-
If there is no timer set, nothing happens and it exits with 1.
102-
If there is a timer set, it is extended by `SECONDS` seconds. `SECONDS` can also be negative, in which case it shortens the timer. Then it exits
103-
with 0.
94+
Extend the current timer by `SECONDS` seconds.
95+
96+
- #### `decrease <SECONDS>`
97+
Shorten the current timer by `SECONDS` seconds.
10498

10599
- #### `togglepause`
106-
If there is no timer set at all, it exits with 1. If there is a timer running, the timer gets paused and it exits with 0. If there is a timer set which is already paused, the timer gets resumed and it exits with 0.
100+
Pause the current timer.
107101

108102
- #### `cancel`
109-
If there is a timer running, the timer gets canceled. The `ACTION` will _not_ be
110-
executed.
103+
Cancel the current timer.
111104

112105
## Tips & Tricks
113106

114-
Note, when there is no timer active, then [`increase`](#increase-seconds) does nothing.
115-
So you might want to use the following command as a replacement for [`increase`](#increase-seconds).
107+
When there is no timer active, then [`increase`](#increase-seconds) does nothing, i.e. it doesn't change the state of the timer.
108+
However, you might want it to _start a new timer_.
109+
You can implement this because `increase` will exit with code 1 when there is no current timer, so you can do:
116110
```
117-
waybar-timer.sh increase 60 || waybar-timer.sh new 1 'notify-send "Timer expired."'
111+
waybar-timer increase 60 || waybar-timer new 1'
118112
```
119-
It increases the existing timer if it's active, and creates a new one minute timer if there is no timer currently running.
120-
So now e.g. scrolling up also does something when there is no timer active - it starts a new timer!
121-
122-
## Known Issues
123-
124-
If you don't (want to) use `dunstify` please see the [dependencies section](#dependencies).
113+
Then if there is an existing timer it gets increased, otherwise a new one minute timer is created.
114+
This is also implemented in the [example configuration](#example-configuration).
115+
Just try to scroll up when there is no timer running!

0 commit comments

Comments
 (0)