generated from ntk148v/golang-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
goignore.go
171 lines (147 loc) · 4.23 KB
/
goignore.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2021 Kien Nguyen-Tuan <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var (
appStyle = lipgloss.NewStyle().Padding(1, 2)
titleStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFDF5")).
Background(lipgloss.Color("#25A065")).
Padding(0, 1)
statusMessageStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#04B575", Dark: "#04B575"}).
Render
errorMessageStyle = lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{Light: "#BD2109", Dark: "#BD2109"}).
Render
)
type item struct {
title string
description string
path string
}
func (i item) Title() string { return i.title }
func (i item) Path() string { return i.path }
func (i item) Description() string { return i.description }
func (i item) FilterValue() string { return i.title }
type listKeyMap struct {
updateTemplates key.Binding
}
func newListKeyMap() *listKeyMap {
return &listKeyMap{
updateTemplates: key.NewBinding(
key.WithKeys("u"),
key.WithHelp("u", "update gitignore templates"),
),
}
}
type model struct {
list list.Model
spinner spinner.Model
keys *listKeyMap
delegateKeys *delegateKeyMap
}
func newModel() model {
var (
delegateKeys = newDelegateKeyMap()
listKeys = newListKeyMap()
)
// Get gitignore repository
items, err := updateTemplateList()
if err != nil {
panic(err)
}
// Setup list
delegate := newItemDelegate(delegateKeys)
ignoreList := list.New(items, delegate, 0, 0)
ignoreList.Title = "Ignore templates"
ignoreList.Styles.Title = titleStyle
ignoreList.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
listKeys.updateTemplates,
}
}
ignoreList.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{
listKeys.updateTemplates,
}
}
// Setup spinner
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))
return model{
list: ignoreList,
spinner: s,
keys: listKeys,
delegateKeys: delegateKeys,
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(tea.EnterAltScreen, m.spinner.Tick)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
topGap, rightGap, bottomGap, leftGap := appStyle.GetPadding()
m.list.SetSize(msg.Width-leftGap-rightGap, msg.Height-topGap-bottomGap)
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
case tea.KeyMsg:
// Don't match any of the keys below if we're actively filtering.
if m.list.FilterState() == list.Filtering {
break
}
switch {
case key.Matches(msg, m.keys.updateTemplates):
cmd := m.list.NewStatusMessage(m.spinner.View() + statusMessageStyle("Pull newest templates"))
if err := pullTemplateUpdates(); err != nil {
cmd = m.list.NewStatusMessage(errorMessageStyle(err.Error()))
}
return m, tea.Batch(cmd)
}
}
// This will also call our delegate's update function.
newListModel, cmd := m.list.Update(msg)
m.list = newListModel
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
return appStyle.Render(m.list.View())
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if err := initTemplates(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
if _, err := tea.NewProgram(newModel()).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}