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
|
package menu
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/term"
"github.com/ewy1/pik/model"
"github.com/ewy1/pik/motd"
"github.com/ewy1/pik/spool"
"github.com/ewy1/pik/viewport"
"github.com/spf13/pflag"
"os"
"strings"
)
type Model struct {
*model.HydratedState
Index int
Indices map[int]model.HydratedTarget
SourceIndices map[int]*model.HydratedSource
Cancel bool
Done bool
Height int
Alt bool
AutoAlt bool
Search textinput.Model
Motd string
}
func (m *Model) Highlights(src *model.HydratedSource, t model.HydratedTarget) bool {
val := m.Search.Value()
if val == "" {
return false
}
if strings.Contains(t.Target().Label(), val) {
return true
}
if strings.Contains(t.Description(src), val) {
return true
}
if strings.Contains(strings.Join(append(t.Target().Sub(), t.Target().Label()), " "), val) {
return true
}
return false
}
func (m *Model) Init() tea.Cmd {
_, h, err := term.GetSize(0)
if err != nil {
_, _ = spool.Warn("%v\n", err)
}
m.Height = h
wantsAlt := viewport.NeedsViewport(m.State(), m.Height)
if m.AutoAlt && wantsAlt {
return tea.EnterAltScreen
}
return nil
}
func (m *Model) HandleResize(msg tea.WindowSizeMsg) tea.Cmd {
if !m.AutoAlt {
return nil
}
m.Height = msg.Height
if viewport.NeedsViewport(m.State(), msg.Height) {
m.Alt = true
return tea.EnterAltScreen
} else {
m.Alt = false
return tea.ExitAltScreen
}
}
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var err error
var result tea.Cmd
switch mt := msg.(type) {
case tea.WindowSizeMsg:
result = m.HandleResize(mt)
case tea.KeyMsg:
result, err = m.HandleInput(mt)
case tea.Cmd:
result, err = m.HandleSignal(mt)
}
if err != nil {
_, _ = spool.Warn("%v\n", err)
}
return m, result
}
func (m *Model) HandleSignal(cmd tea.Cmd) (tea.Cmd, error) {
return nil, nil
}
func (m *Model) View() string {
if m.Cancel || m.Done {
return ""
}
result := m.State()
result = viewport.Process(result, m.Height)
if m.Search.Focused() {
result = m.AddSearch(result)
}
return result
}
func (m *Model) Result() (*model.HydratedSource, model.HydratedTarget) {
if m.Cancel {
return nil, nil
}
return m.SourceIndices[m.Index], m.Indices[m.Index]
}
func (m *Model) Validate() (clamped bool) {
if m.Index < 0 {
m.Index = 0
return true
}
if m.Index > len(m.Indices)-1 {
m.Index = len(m.Indices) - 1
return true
}
return false
}
var ForcedInlineTerminals = map[string]string{
"TERMINAL_EMULATOR": "JetBrains-JediTerm",
}
func NewModel(st *model.State, hydrators []model.Modder) *Model {
isBanned := false
for k, v := range ForcedInlineTerminals {
if os.Getenv(k) == v {
isBanned = true
break
}
}
m := &Model{
HydratedState: Hydrate(st, hydrators),
Index: 0,
Indices: make(map[int]model.HydratedTarget),
SourceIndices: make(map[int]*model.HydratedSource),
AutoAlt: !pflag.Lookup("inline").Changed && !isBanned,
Motd: motd.One(),
Search: textinput.New(),
}
idx := 0
for _, src := range st.Sources {
hydSrc := src.Hydrate(hydrators)
for _, target := range src.Targets {
if !target.Visible() {
continue
}
hydTarget, err := target.Hydrate(src)
m.Indices[idx] = hydTarget
if err != nil {
spool.Warn("%v\n", err)
}
m.SourceIndices[idx] = hydSrc
idx++
}
}
return m
}
|