Skip to content

Commit 06d7733

Browse files
committed
Add until helper
1 parent 75e7534 commit 06d7733

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ The following colours are available: `black`, `white`, `red`, `green`, `yellow`,
5454

5555
Helpers create dynamic output in your status bar. You can use one by adding it to your bar format config. The following is a list of available helpers.
5656

57-
| Helper | Description | Example Config | Example Output |
58-
|---------|---------------------------------------------------|------------------|----------------|
59-
| time | Show current time | {time} | 11:58:17 |
60-
| cpu | Show current CPU usage | {cpu} | 20% |
61-
| memory | Show current memory usage % | {memory} | 20% |
62-
| battery | Show current battery charge % | {battery} | 20% |
63-
| bash | Run a custom bash command | {bash:echo hi} | hi |
64-
| weather | Show current weather (provided by http://wttr.in) | {weather:1} | 🌧 +6°C |
57+
| Helper | Description | Example Config | Example Output |
58+
|---------|---------------------------------------------------|--------------------|----------------|
59+
| time | Show current time | {time} | 11:58:17 |
60+
| cpu | Show current CPU usage | {cpu} | 20% |
61+
| memory | Show current memory usage % | {memory} | 20% |
62+
| battery | Show current battery charge % | {battery} | 20% |
63+
| bash | Run a custom bash command | {bash:echo hi} | hi |
64+
| weather | Show current weather (provided by http://wttr.in) | {weather:1} | 🌧 +6°C |
65+
| until | Show time remaining until a given unix timestamp | {until:1583255109} | 13m26s |
66+
6567

6668
Ideally this list would be much longer - please feel free to PR more helpers! You can see simple examples [here](https://github.com/liamg/shox/tree/master/pkg/helpers).
6769

pkg/helpers/until.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package helpers
2+
3+
import (
4+
"strconv"
5+
"time"
6+
)
7+
8+
// TimeHelper shows the current time
9+
type UntilHelper struct {
10+
}
11+
12+
// UpdateInterval returns the minimum time period before the helper should run again
13+
func (h *UntilHelper) UpdateInterval() time.Duration {
14+
return time.Second
15+
}
16+
17+
// Run returns the current time
18+
func (h *UntilHelper) Run(config string) string {
19+
ts, err := strconv.ParseInt(config, 10, 64)
20+
if err != nil {
21+
return "bad timestamp"
22+
}
23+
remaining := time.Until(time.Unix(ts, 0)).Truncate(time.Second)
24+
if remaining < 0 {
25+
return "0s"
26+
}
27+
return remaining.String()
28+
}
29+
30+
func init() {
31+
Register("until", &UntilHelper{})
32+
}

0 commit comments

Comments
 (0)