-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (193 loc) Β· 4.9 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/iamhectorsosa/sqlite-shell/internal/database"
"github.com/iamhectorsosa/sqlite-shell/internal/help"
"github.com/iamhectorsosa/sqlite-shell/internal/helpers"
"github.com/iamhectorsosa/sqlite-shell/internal/styles"
)
func main() {
if len(os.Args) < 2 {
log.Fatal("Required: <databasePath>")
}
databasePath := os.Args[1]
p := tea.NewProgram(initialModel(databasePath), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}
type errMsg error
type keyMap struct {
Tab key.Binding
Quit key.Binding
}
var keys = keyMap{
Tab: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "toggle input"),
),
Quit: key.NewBinding(
key.WithKeys("esc", "ctrl+c"),
key.WithHelp("esc", "quit"),
),
}
type model struct {
databasePath string
styles *styles.Styles
help string
err error
textInput textinput.Model
table table.Model
viewportWidth int
viewportHeight int
ready bool
}
func initialModel(databasePath string) model {
styles := styles.New()
textInput := textinput.New()
textInput.Placeholder = "Write SQL..."
textInput.PromptStyle = styles.Highlight
textInput.Cursor.Style = styles.Highlight
textInput.Focus()
return model{
databasePath: databasePath,
styles: styles,
err: nil,
ready: false,
textInput: textInput,
help: help.New(),
}
}
func (m model) appBoundaryText(text string) string {
return lipgloss.PlaceHorizontal(
m.viewportWidth,
lipgloss.Left,
m.styles.BoundaryText.Render(text+" "),
lipgloss.WithWhitespaceChars("β’"),
m.styles.WhitespaceStyle,
)
}
func (m model) appErrorText(text string) string {
return lipgloss.Place(m.viewportWidth, m.viewportHeight,
lipgloss.Center, lipgloss.Center,
m.styles.ErrorText.Render(lipgloss.NewStyle().
Padding(0, 2).
Width(50).
Align(lipgloss.Center).
Render(text),
),
lipgloss.WithWhitespaceChars("η«εͺ"),
m.styles.WhitespaceBackgroundStyle,
)
}
func (m model) Init() tea.Cmd {
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !m.ready {
m.ready = true
}
m.viewportWidth = msg.Width - 2
m.viewportHeight = msg.Height - 8
case tea.KeyMsg:
switch msg.Type {
case tea.KeyTab:
if m.table.Focused() {
m.table.Blur()
cmd := m.textInput.Focus()
m.textInput.PromptStyle = m.styles.Highlight
cmds = append(cmds, cmd)
} else {
m.table.Focus()
m.textInput.Blur()
m.textInput.PromptStyle = m.styles.Background
}
case tea.KeyEnter:
query := strings.TrimSpace(m.textInput.Value())
if query == "" {
return m, nil
}
headers, rows, err := database.ExecCmd(m.databasePath, query)
if err != nil {
m.err = err
m.styles.Error()
m.textInput.PromptStyle = m.styles.Highlight
m.textInput.Cursor.Style = m.styles.Highlight
if len(m.table.Rows()) > 0 {
s := table.DefaultStyles()
s.Header = m.styles.TableHeader
s.Selected = m.styles.TableRow
m.table.SetStyles(s)
}
return m, nil
} else {
m.err = nil
m.styles.Reset()
m.textInput.PromptStyle = m.styles.Highlight
m.textInput.Cursor.Style = m.styles.Highlight
}
m.textInput.Blur()
m.textInput.PromptStyle = m.styles.Background
height := len(rows) + 1
if height > m.viewportHeight {
height = m.viewportHeight
}
t := table.New(
table.WithColumns(helpers.CreateColumns(headers, rows, m.viewportWidth)),
table.WithRows(helpers.CreateRows(rows)),
table.WithFocused(true),
table.WithHeight(height),
)
s := table.DefaultStyles()
s.Header = m.styles.TableHeader
s.Selected = m.styles.TableRow
t.SetStyles(s)
m.table = t
m.table.Focus()
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case errMsg:
m.err = msg
return m, nil
}
var cmdTextInput tea.Cmd
var cmdTable tea.Cmd
m.textInput, cmdTextInput = m.textInput.Update(msg)
m.table, cmdTable = m.table.Update(msg)
cmds = append(cmds, cmdTextInput, cmdTable)
return m, tea.Batch(cmds...)
}
func (m model) View() string {
if !m.ready {
return "Initializing..."
}
header := m.appBoundaryText("SQLite Shell")
if m.err != nil {
header = m.appBoundaryText("An error has occured")
}
content := m.styles.TextBorder.Render(m.textInput.View())
if len(m.table.Columns()) > 0 && m.err == nil {
content = fmt.Sprintf("%s\n\n%s", content, m.table.View())
}
if m.err != nil {
content = fmt.Sprintf("%s\n\n%s", content, m.appErrorText(m.err.Error()))
}
return m.styles.Base.Render(fmt.Sprintf(
"%s\n\n%s\n\n%s",
header,
content,
m.appBoundaryText(m.help),
))
}