-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (86 loc) · 1.92 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
package main
import (
"fmt"
"os"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
)
type menuOption string
const (
optionLock menuOption = "Lock"
optionReboot menuOption = "Reboot"
optionPowerOff menuOption = "Power Off"
)
var options = []menuOption{optionLock, optionReboot, optionPowerOff}
type model struct {
cursor int
quitting bool
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc":
m.quitting = true
return m, tea.Quit
case "up":
if m.cursor > 0 {
m.cursor--
}
case "down":
if m.cursor < len(options) - 1 {
m.cursor++
}
case "enter":
return m, executeOption(options[m.cursor])
}
}
return m, nil
}
func (m model) View() string {
if m.quitting {
return "Goodbye!\n"
}
s := "Power Menu\n\n"
for i, option := range options {
cursor := " "
if m.cursor == i {
cursor = ">"
}
s += fmt.Sprintf("%s %s\n", cursor, option)
}
s += "\n[↑/↓] Navigate [Enter] Select [q/Esc] Quit\n"
return s
}
func executeOption(option menuOption) tea.Cmd {
return func() tea.Msg {
var cmd *exec.Cmd
switch option {
case optionLock:
cmd = exec.Command("swaylock", "-f", "-i", "~/.dotfiles-swayzy/wallpapers/sakura-rose-blur.png")
case optionReboot:
cmd = exec.Command("systemctl", "reboot")
case optionPowerOff:
cmd = exec.Command("systemctl", "poweroff")
default:
return nil
}
// run command and check for errors
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing %s: %v\n", option, err)
}
return tea.Quit()
}
}
func main() {
p := tea.NewProgram(model{})
_, err := p.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}