-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
196 lines (169 loc) · 4.01 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
termWidth, termHight int
heading = lipgloss.NewStyle().Bold(true).Margin(1, 0)
notChoosen = lipgloss.NewStyle().Bold(true).Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("12")).Width(14).Padding(1)
choosen = lipgloss.NewStyle().Foreground(lipgloss.Color("5")).Bold(true).Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("5")).Width(14).Padding(1)
)
const (
Suspend = iota
Lock = iota
Logout = iota
Shutdown = iota
Restart = iota
Hibernate = iota
)
const (
x11 = iota
wayland = iota
)
func getDisplay() int {
display := os.Getenv("XDG_SESSION_TYPE")
if display == "x11" {
return x11
} else if display == "wayland" {
return wayland
} else {
return -1
}
}
type model struct {
cursor int
choices []string
selected map[int]struct{}
}
func end(choice string) {
switch choice {
case "Suspend":
cmd := exec.Command("systemctl", "suspend")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
case "Lock":
if getDisplay() == x11 {
cmd := exec.Command("xdg-screensaver", "lock")
xerr := cmd.Run()
if xerr != nil {
log.Fatal(xerr)
}
} else {
cmd := exec.Command("swaylock", "-c", "000000", "-e")
werr := cmd.Run()
if werr != nil {
log.Fatal(werr)
}
}
case "Logout":
cmd := exec.Command("kill", "-9", "-1")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
case "Shutdown":
cmd := exec.Command("shutdown", "now")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
case "Restart":
cmd := exec.Command("shutdown", "-r")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
case "Hibernate":
cmd := exec.Command("systemctl", "hibernate")
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
}
func initialModel() model {
return model{
choices: []string{"Suspend", "Lock", "Logout", "Shutdown", "Restart", "Hibernate"},
// A map which indicates which choices are selected. We're using
// the map like a mathematical set. The keys refer to the indexes
// of the `choices` slice, above.
selected: make(map[int]struct{}),
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(nil, tea.EnterAltScreen)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
termWidth, termHight = msg.Width, msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc":
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
}
case "down", "j":
if m.cursor < len(m.choices)-1 {
m.cursor++
}
case strconv.Itoa(Suspend + 1):
end("Suspend")
return m, tea.Quit
case strconv.Itoa(Lock + 1):
end("Lock")
return m, tea.Quit
case strconv.Itoa(Logout + 1):
end("Logout")
return m, tea.Quit
case strconv.Itoa(Shutdown + 1):
end("Shutdown")
return m, tea.Quit
case strconv.Itoa(Restart + 1):
end("Restart")
return m, tea.Quit
case strconv.Itoa(Hibernate + 1):
end("Hibernate")
return m, tea.Quit
case "enter":
end(m.choices[m.cursor])
return m, tea.Quit
}
}
return m, nil
}
func (m model) View() string {
s := heading.Render("Where do you want to GO?")
for i, choice := range m.choices {
cursor := " "
var line string
if m.cursor == i {
cursor = "❯"
line = choosen.Render(fmt.Sprintf("%s %s", cursor, choice))
} else {
line = notChoosen.Render(fmt.Sprintf("%s %s", cursor, choice))
}
s = lipgloss.JoinVertical(lipgloss.Center, s, line)
}
s = lipgloss.JoinVertical(lipgloss.Center, s, heading.Render("Press q to quit."))
textWidth, textHeight := lipgloss.Size(s)
marginW, marginH := (termWidth-textWidth)/2, (termHight-textHeight)/2
return lipgloss.NewStyle().Margin(marginH, marginW).Render(s)
}
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}