-
Notifications
You must be signed in to change notification settings - Fork 4
/
tui_dialog_delete.go
111 lines (87 loc) · 2.47 KB
/
tui_dialog_delete.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
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type (
TuiDialogYes struct{}
TuiDialogNo struct{}
)
type TuiDialogYesNo struct {
Title string
Message string
selectedButton int
}
func NewTuiDialogYesNo(title, message string) TuiDialogYesNo {
return TuiDialogYesNo{Title: title, Message: message}
}
func (m TuiDialogYesNo) Init() tea.Cmd {
return nil
}
func (m TuiDialogYesNo) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyRunes:
switch string(msg.Runes) {
case "Y", "y":
return m, func() tea.Msg { return TuiDialogYes{} }
case "N", "n":
return m, func() tea.Msg { return TuiDialogNo{} }
}
case tea.KeyRight:
m.selectedButton = 1
return m, nil
case tea.KeyLeft:
m.selectedButton = 0
return m, nil
case tea.KeyTab:
if m.selectedButton == 0 {
m.selectedButton = 1
} else {
m.selectedButton = 0
}
return m, nil
case tea.KeyEscape:
return m, func() tea.Msg {
return TuiDialogNo{}
}
case tea.KeyEnter:
if m.selectedButton == 0 {
return m, func() tea.Msg {
return TuiDialogYes{}
}
} else {
return m, func() tea.Msg {
return TuiDialogNo{}
}
}
}
}
return m, cmd
}
func (m TuiDialogYesNo) View() string {
redBg := lipgloss.NewStyle().Background(lipgloss.Color("1"))
frameStyle := redBg.Foreground(lipgloss.Color("0")).Width(44).Padding(1, 2)
titleStyle := redBg.Foreground(lipgloss.Color("11")).Width(40).Align(0.5)
msgStyle := redBg.Foreground(lipgloss.Color("15")).Width(40).Align(0.5).Padding(0, 0, 1, 0)
hStyle := lipgloss.NewStyle().Background(lipgloss.Color("7")).Foreground(lipgloss.Color("0"))
hBright := lipgloss.NewStyle().Background(lipgloss.Color("7")).Foreground(lipgloss.Color("15"))
redYellow := redBg.Foreground(lipgloss.Color("11"))
btnStyle := redBg.Width(40).Align(.5)
yes := redBg.Render("[ ") + redYellow.Render("Y") + redBg.Render("es ]")
no := redBg.Render("[ ") + redYellow.Render("N") + redBg.Render("o ]")
if m.selectedButton == 0 {
yes = hStyle.Render("[ ") + hBright.Render("Y") + hStyle.Render("es ]")
} else {
no = hStyle.Render("[ ") + hBright.Render("N") + hStyle.Render("o ]")
}
return frameStyle.Render(
lipgloss.JoinVertical(0,
titleStyle.Render(m.Title),
msgStyle.Render(m.Message),
btnStyle.Render(lipgloss.JoinHorizontal(0, yes, redBg.Render(" "), no)),
),
)
}