-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui.go
166 lines (150 loc) · 3.95 KB
/
tui.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/charmbracelet/bubbles/filepicker"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
helpHeight = 5
)
func newTextarea() textarea.Model {
t := textarea.New()
t.Prompt = ""
t.Placeholder = "~these are not the droids you are looking for~"
t.ShowLineNumbers = true
t.Cursor.Style = cursorStyle
t.FocusedStyle.Placeholder = focusedPlaceholderStyle
t.BlurredStyle.Placeholder = placeholderStyle
t.FocusedStyle.CursorLine = cursorLineStyle
t.FocusedStyle.Base = focusedBorderStyle
t.BlurredStyle.Base = blurredBorderStyle
t.FocusedStyle.EndOfBuffer = endOfBufferStyle
t.BlurredStyle.EndOfBuffer = endOfBufferStyle
t.KeyMap.DeleteWordBackward.SetEnabled(false)
t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down"))
t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up"))
t.CharLimit = 0
t.Blur()
return t
}
type model struct {
original, rendered textarea.Model
filepicker filepicker.Model
fpvisible bool
selectedFile string
quitting bool
keymap keymap
help help.Model
cwd string
tmpldata map[string]any
}
func (m model) Init() tea.Cmd {
return tea.Batch(tea.EnterAltScreen, m.filepicker.Init())
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmdList = []tea.Cmd{}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keymap.quit):
m.quitting = true
return m, tea.Quit
case key.Matches(msg, m.keymap.file):
m.fpvisible = !m.fpvisible
cmdList = append(cmdList, func() tea.Msg {
return focusTexMsg(!m.fpvisible)
})
}
case focusTexMsg:
if msg {
m.original.Focus()
m.rendered.Focus()
} else {
m.original.Blur()
m.rendered.Blur()
}
case tea.WindowSizeMsg:
m.original.SetHeight(msg.Height - helpHeight)
m.original.SetWidth(msg.Width / 2)
m.rendered.SetHeight(msg.Height - helpHeight)
m.rendered.SetWidth(msg.Width / 2)
}
var fcmd, tcmd, icmd tea.Cmd
cmdList = append(cmdList, fcmd, tcmd, icmd)
if m.fpvisible || m.quitting {
m.filepicker, fcmd = m.filepicker.Update(msg)
cmdList = append(cmdList, fcmd)
}
m.original, tcmd = m.original.Update(msg)
m.rendered, tcmd = m.rendered.Update(msg)
// Did the user select a file?
if didSelect, path := m.filepicker.DidSelectFile(msg); didSelect {
// Get the path of the selected file.
rpath, err := filepath.Rel(m.cwd, path)
if err != nil {
// in case of error we just use the abs path
rpath = path
}
m.selectedFile = rpath
fil, tmpl, _ := loadFile(rpath, m.tmpldata)
m.original.SetValue(fil)
m.rendered.SetValue(tmpl)
}
return m, tea.Batch(cmdList...)
}
type focusTexMsg bool
func (m model) View() string {
if m.quitting {
return ""
}
help := m.help.ShortHelpView([]key.Binding{
m.keymap.file,
m.keymap.quit,
})
var views []string
var s strings.Builder
if m.fpvisible {
s.WriteString("\n ")
if m.selectedFile == "" {
s.WriteString("Pick a file:")
} else {
s.WriteString("Selected file: " + m.filepicker.Styles.Selected.Render(m.selectedFile))
}
s.WriteString("\n\n" + m.filepicker.View() + "\n")
}
views = append(views, s.String(), m.original.View(), m.rendered.View())
return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help
}
func tui() {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
tea.LogToFile("tmpltui.log", "tmpltui")
fp := filepicker.New()
fp.AllowedTypes = []string{}
fp.CurrentDirectory, _ = os.Getwd()
m := model{
filepicker: fp,
original: newTextarea(),
rendered: newTextarea(),
cwd: cwd,
tmpldata: loadTmplData(),
fpvisible: true,
keymap: newkeymap(),
help: help.New(),
}
_, err = tea.NewProgram(&m, tea.WithOutput(os.Stderr)).Run()
if err != nil {
fmt.Println(err)
}
fmt.Println("bye")
}