-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.go
150 lines (128 loc) Β· 3.15 KB
/
main.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* This file is part of tz.
*
* tz 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.
*
* tz 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 tz. If not, see <https://www.gnu.org/licenses/>.
**/
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
)
// CurrentVersion represents the current build version.
const CurrentVersion = "0.7.0"
var (
term = termenv.ColorProfile()
hasDarkBackground = termenv.HasDarkBackground()
// Now is used around tz to share/set the current time.
Now *Clock = NewClock(0)
)
type tickMsg time.Time
// Send a tickMsg every minute, on the minute.
func tick() tea.Cmd {
return tea.Every(time.Minute, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
type model struct {
zones []*Zone
now time.Time
hour int
showDates bool
interactive bool
isMilitary bool
}
func (m model) Init() tea.Cmd {
// If -q flag is passed, send quit message after first render.
if !m.interactive {
return tea.Quit
}
// Fire initial tick command to begin receiving ticks on the minute.
return tick()
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "left", "h":
if m.hour == 0 {
m.hour = 23
Now.AddDays(-1)
} else {
m.hour--
}
case "right", "l":
if m.hour > 22 {
m.hour = 0
Now.AddDays(1)
} else {
m.hour++
}
case "d":
m.showDates = !m.showDates
}
case tickMsg:
m.now = time.Time(msg)
return m, tick()
}
return m, nil
}
func main() {
exitQuick := flag.Bool("q", false, "exit immediately")
showVersion := flag.Bool("v", false, "show version")
when := flag.Int64("when", 0, "time in seconds since unix epoch")
doSearch := flag.Bool("list", false, "list zones by name")
military := flag.Bool("m", false, "use 24-hour time")
flag.Parse()
if *showVersion == true {
fmt.Printf("tz %s\n", CurrentVersion)
os.Exit(0)
}
if *doSearch {
q := ""
if arg := flag.Arg(0); arg != "" {
q = arg
}
results := SearchZones(strings.ToLower(q))
results.Print(os.Stdout)
os.Exit(0)
}
if *when != 0 {
Now = NewClock(*when)
}
config, err := LoadConfig(flag.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %s\n", err)
os.Exit(2)
}
var initialModel = model{
zones: config.Zones,
now: Now.Time(),
hour: Now.Time().Hour(),
showDates: false,
isMilitary: *military,
}
initialModel.interactive = !*exitQuick
p := tea.NewProgram(initialModel)
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}