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
|
package menu
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/term"
"github.com/spf13/pflag"
"os"
"pik/model"
"pik/motd"
"pik/spool"
"pik/viewport"
)
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
Motd string
}
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)
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() {
if m.Index < 0 {
m.Index = 0
}
if m.Index > len(m.Indices)-1 {
m.Index = len(m.Indices) - 1
}
}
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(),
}
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
}
|