-
Notifications
You must be signed in to change notification settings - Fork 18
/
ui_commit_selector.go
133 lines (109 loc) · 3.49 KB
/
ui_commit_selector.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"io"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
selectorTitleStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#2E2E2E", Dark: "#DDDDDD"}).
Background(lipgloss.AdaptiveColor{Light: "#19A04B", Dark: "#25A065"}).
Bold(true).
Padding(0, 1)
selectorNormalStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#1A1A1A", Dark: "#DDDDDD"}).
Padding(0, 0, 0, 2)
selectorSelectedStyle = lipgloss.NewStyle().
Border(lipgloss.NormalBorder(), false, false, false, true).
BorderForeground(lipgloss.AdaptiveColor{Light: "#9F72FF", Dark: "#AD58B4"}).
Foreground(lipgloss.AdaptiveColor{Light: "#9A4AFF", Dark: "#EE6FF8"}).
Bold(true).
Padding(0, 0, 0, 1)
selectorPaginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
selectorHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#6F6C6C", Dark: "#7A7A7A"})
)
type selectorItem struct {
ct string
title string
}
func (cti selectorItem) FilterValue() string { return cti.title }
type selectorDelegate struct{}
func (d selectorDelegate) Height() int { return 1 }
func (d selectorDelegate) Spacing() int { return 0 }
func (d selectorDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d selectorDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(selectorItem)
if !ok {
return
}
str := fmt.Sprintf("%d. %s", index+1, i.title)
if index == m.Index() {
_, _ = fmt.Fprintf(w, selectorSelectedStyle.Render(str))
} else {
_, _ = fmt.Fprintf(w, selectorNormalStyle.Render(str))
}
}
type selectorModel struct {
list list.Model
choice string
}
func (m selectorModel) Init() tea.Cmd {
return nil
}
func (m selectorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
return m, nil
case tea.KeyMsg:
switch keypress := msg.String(); keypress {
case "ctrl+c", "esc":
return m, tea.Quit
case "enter":
m.choice = m.list.SelectedItem().(selectorItem).ct
return m, func() tea.Msg { return done{nextView: INPUTS} }
default:
if !m.list.SettingFilter() && (keypress == "q" || keypress == "esc") {
return m, tea.Quit
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
default:
return m, nil
}
}
func (m selectorModel) View() string {
if m.choice != "" {
return m.choice
}
return "\n" + m.list.View()
}
func newSelectorModel() selectorModel {
l := list.NewModel([]list.Item{
selectorItem{ct: feat, title: featDesc},
selectorItem{ct: fix, title: fixDesc},
selectorItem{ct: docs, title: docsDesc},
selectorItem{ct: style, title: styleDesc},
selectorItem{ct: refactor, title: refactorDesc},
selectorItem{ct: test, title: testDesc},
selectorItem{ct: chore, title: choreDesc},
selectorItem{ct: perf, title: perfDesc},
selectorItem{ct: hotfix, title: hotfixDesc},
}, selectorDelegate{}, 20, 12)
l.Title = "Select Commit Type"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(false)
l.Styles.Title = selectorTitleStyle
l.Styles.PaginationStyle = selectorPaginationStyle
h := help.NewModel()
h.Styles.ShortDesc = selectorHelpStyle
h.Styles.ShortSeparator = selectorHelpStyle
h.Styles.ShortKey = selectorHelpStyle
l.Help = h
return selectorModel{list: l}
}