-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtui_dialog_msg.go
74 lines (62 loc) · 1.69 KB
/
tui_dialog_msg.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
package main
import (
"github.com/andrianbdn/wg-cmd/theme"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type TuiDialogMsgResult struct{}
type TuiDialogMsg struct {
Title string
Message string
IsError bool
}
func NewTuiDialogMsg(title, message string, isError bool) TuiDialogMsg {
return TuiDialogMsg{Title: title, Message: message, IsError: isError}
}
func (m TuiDialogMsg) Init() tea.Cmd {
return nil
}
func (m TuiDialogMsg) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyEscape, tea.KeyEnter:
return m, func() tea.Msg {
return TuiDialogMsgResult{}
}
}
}
return m, cmd
}
func (m TuiDialogMsg) View() string {
if m.IsError {
return ErrorView(m.Title, m.Message)
} else {
return InfoView(m.Title, m.Message)
}
}
func InfoView(title, message string) string {
frameStyle := theme.Current.DialogBackground.Width(44).Padding(1, 2)
titleStyle := theme.Current.DialogTitle.Width(40).Align(lipgloss.Left)
msgStyle := theme.Current.DialogBackground.Width(40).Align(lipgloss.Left).Padding(1, 0, 0, 0)
return frameStyle.Render(
lipgloss.JoinVertical(0,
titleStyle.Render(title),
msgStyle.Render(message),
),
)
}
func ErrorView(title, message string) string {
padLR := 2
w := 34
frameStyle := theme.Current.DialogErrorBackground.Width(w+padLR*2).Padding(1, 2)
titleStyle := theme.Current.DialogErrorTitle.Width(w).Align(lipgloss.Center)
msgStyle := theme.Current.DialogErrorMessage.Width(w).Align(lipgloss.Center).Padding(1, 0)
return frameStyle.Render(
lipgloss.JoinVertical(0,
titleStyle.Render(title),
msgStyle.Render(message),
),
)
}