/
snippets.go
77 lines (61 loc) · 1.64 KB
/
snippets.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
package viewer
import (
"fmt"
"io"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mathaou/termdbms/list"
"github.com/mathaou/termdbms/tuiutil"
)
var (
style = lipgloss.NewStyle()
)
func (s SQLSnippet) Title() string {
return s.Name
}
func (s SQLSnippet) Description() string {
return s.Query
}
func (s SQLSnippet) FilterValue() string {
return s.Name
}
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
return nil
}
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
localStyle := style.Copy()
i, ok := listItem.(SQLSnippet)
if !ok {
return
}
digits := len(fmt.Sprintf("%d", len(m.Items()))) + 1
incomingDigits := len(fmt.Sprintf("%d", index+1))
if !tuiutil.Ascii {
localStyle = style.Copy().Faint(true)
}
str := fmt.Sprintf("%d) %s%s | ", index+1, strings.Repeat(" ", digits-incomingDigits),
i.Title())
query := localStyle.Render(i.Query[0:Min(TUIWidth-10, Max(len(i.Query)-1, len(i.Query)-1-len(str)))]) // padding + tab + padding
str += strings.ReplaceAll(query, "\n", "")
localStyle = style.Copy().PaddingLeft(4)
fn := localStyle.Render
if index == m.Index() {
fn = func(s string) string {
localStyle = style.Copy().
PaddingLeft(2)
if !tuiutil.Ascii {
localStyle = localStyle.
Foreground(lipgloss.Color(tuiutil.HeaderTopForeground()))
}
return lipgloss.JoinHorizontal(lipgloss.Left,
localStyle.
Render("> "),
style.Render(s))
}
}
fmt.Fprintf(w, fn(str))
}