-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdialog.go
84 lines (70 loc) · 1.99 KB
/
dialog.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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
)
var (
dialogBoxStyle = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), true).
BorderForeground(lipgloss.Color("#874BFD")).
Padding(1, 0)
buttonStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFF7DB")).
Background(lipgloss.Color("#888B7E")).
Padding(0, 3).
MarginTop(1).
MarginRight(2)
activeButtonStyle = buttonStyle.Copy().
Foreground(lipgloss.Color("#FFF7DB")).
Background(lipgloss.Color("#F25D94")).
MarginRight(2).
Underline(true)
)
type dialog struct {
id string
height int
width int
active string
question string
}
func (m dialog) Init() tea.Cmd {
return nil
}
func (m dialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
case tea.MouseMsg:
if msg.Action != tea.MouseActionRelease || msg.Button != tea.MouseButtonLeft {
return m, nil
}
if zone.Get(m.id + "confirm").InBounds(msg) {
m.active = "confirm"
} else if zone.Get(m.id + "cancel").InBounds(msg) {
m.active = "cancel"
}
return m, nil
}
return m, nil
}
func (m dialog) View() string {
var okButton, cancelButton string
if m.active == "confirm" {
okButton = activeButtonStyle.Render("Yes")
cancelButton = buttonStyle.Render("Maybe")
} else {
okButton = buttonStyle.Render("Yes")
cancelButton = activeButtonStyle.Render("Maybe")
}
question := lipgloss.NewStyle().Width(27).Align(lipgloss.Center).Render("Are you sure you want to eat marmalade?")
buttons := lipgloss.JoinHorizontal(
lipgloss.Top,
zone.Mark(m.id+"confirm", okButton),
zone.Mark(m.id+"cancel", cancelButton),
)
return dialogBoxStyle.Render(lipgloss.JoinVertical(lipgloss.Center, question, buttons))
}