-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtui.go
More file actions
93 lines (81 loc) · 2.22 KB
/
tui.go
File metadata and controls
93 lines (81 loc) · 2.22 KB
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
package main
import (
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/creack/pty"
"golang.org/x/term"
)
type model struct {
prompt string
affirmative string
negative string
selection bool
selectedStyle lipgloss.Style
unselectedStyle lipgloss.Style
promptStyle lipgloss.Style
borderStyle lipgloss.Style
}
func DefaultModel() model {
return model{
prompt: "Press Enter to continue...",
affirmative: "Yes",
negative: "No",
selection: true,
selectedStyle: lipgloss.NewStyle().Background(lipgloss.Color("212")).Foreground(lipgloss.Color("232")).Padding(0, 3).Margin(0, 1),
unselectedStyle: lipgloss.NewStyle().Background(lipgloss.Color("235")).Foreground(lipgloss.Color("254")).Padding(0, 3).Margin(0, 1),
promptStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("#7571F9")).Bold(true),
borderStyle: lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("#7571F9")).Padding(1, 2),
}
}
func (model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
if msg.Type == tea.KeyEnter {
return m, tea.Quit
}
switch msg.String() {
case "y":
m.selection = true
return m, nil
case "n":
m.selection = false
return m, nil
case "up", "down", "left", "right", "h", "j", "k", "l":
m.selection = !m.selection
return m, nil
}
}
return m, nil
}
func (m model) View() string {
var aff, neg string
if m.selection {
aff = m.selectedStyle.Render(m.affirmative)
neg = m.unselectedStyle.Render(m.negative)
} else {
aff = m.unselectedStyle.Render(m.affirmative)
neg = m.selectedStyle.Render(m.negative)
}
return m.borderStyle.Render(lipgloss.JoinVertical(
lipgloss.Left,
m.promptStyle.Render(m.prompt)+"\n",
lipgloss.JoinHorizontal(lipgloss.Left, aff, neg),
))
}
func setPtySize(ptyFile *os.File, width, height int) error {
return pty.Setsize(ptyFile, &pty.Winsize{
Rows: uint16(height),
Cols: uint16(width),
})
}
func getTerminalSize() (int, int, error) {
width, height, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
return 0, 0, err
}
return width, height, nil
}