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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
package tui
import (
"fmt"
"adammathes.com/neko/models/feed"
"adammathes.com/neko/models/item"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type sessionState int
const (
sidebarFocus sessionState = iota
itemFocus
contentFocus
)
// itemString implements list.Item
type itemString string
func (i itemString) FilterValue() string { return string(i) }
type Model struct {
state sessionState
sidebar list.Model
items list.Model
content viewport.Model
feedData []*feed.Feed
itemData []*item.Item
selectedFeed *feed.Feed
selectedItem *item.Item
width int
height int
err error
ready bool
}
func NewModel() Model {
// sidebar
s := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
s.Title = "Feeds"
s.SetShowHelp(false)
s.DisableQuitKeybindings()
// items
i := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
i.Title = "Items"
i.SetShowHelp(false)
i.DisableQuitKeybindings()
return Model{
state: sidebarFocus,
sidebar: s,
items: i,
content: viewport.New(0, 0),
}
}
func (m Model) Init() tea.Cmd {
return loadFeeds
}
type (
feedsMsg []*feed.Feed
itemsMsg []*item.Item
errMsg error
)
const (
SpecialFeedAllId = -100
SpecialFeedUnreadId = -101
SpecialFeedStarredId = -102
)
func loadFeeds() tea.Msg {
feeds, err := feed.All()
if err != nil {
return errMsg(err)
}
special := []*feed.Feed{
{Id: SpecialFeedUnreadId, Title: "Unread"},
{Id: SpecialFeedAllId, Title: "All"},
{Id: SpecialFeedStarredId, Title: "Starred"},
}
return feedsMsg(append(special, feeds...))
}
func loadItems(feedID int64) tea.Cmd {
return func() tea.Msg {
var items []*item.Item
var err error
switch feedID {
case SpecialFeedAllId:
items, err = item.Filter(0, 0, "", false, false, 0, "")
case SpecialFeedUnreadId:
items, err = item.Filter(0, 0, "", true, false, 0, "")
case SpecialFeedStarredId:
items, err = item.Filter(0, 0, "", false, true, 0, "")
default:
items, err = item.Filter(0, feedID, "", false, false, 0, "")
}
if err != nil {
return errMsg(err)
}
return itemsMsg(items)
}
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
m.ready = true
// Layout: Sidebar 30%, Main 70%
sidebarWidth := int(float64(m.width) * 0.3)
mainWidth := m.width - sidebarWidth - 4 // minus borders/padding
m.sidebar.SetSize(sidebarWidth, m.height-2)
m.items.SetSize(mainWidth, m.height-2)
m.content.Width = mainWidth
m.content.Height = m.height - 4
case feedsMsg:
m.feedData = msg
items := make([]list.Item, len(m.feedData))
for i, f := range m.feedData {
items[i] = itemString(f.Title)
}
m.sidebar.SetItems(items)
case itemsMsg:
m.itemData = msg
m.updateListItems()
// Switch focus to items when loaded?
// Maybe keep focus where it was, or auto-switch
// User expectation: Enter on feed -> focus items
// But loadItems is async.
// Let's rely on explicit focus switch or handle it here if intent was "select feed".
// For now, let's keep focus as is, but if we just selected a feed, maybe we are still in sidebar.
// Actually, standard TUI behavior: Enter on sidebar -> focus items.
if m.state == sidebarFocus {
m.state = itemFocus
}
case errMsg:
m.err = msg
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
if m.state == contentFocus {
m.state = itemFocus
return m, nil
}
return m, tea.Quit
case "tab":
if m.state == sidebarFocus {
m.state = itemFocus
} else if m.state == itemFocus {
m.state = sidebarFocus
}
return m, nil
case "esc":
if m.state == contentFocus {
m.state = itemFocus
return m, nil
}
if m.state == itemFocus {
m.state = sidebarFocus
return m, nil
}
case "s":
if m.state == itemFocus || m.state == contentFocus {
if m.selectedItem != nil {
m.selectedItem.Starred = !m.selectedItem.Starred
m.selectedItem.Save()
m.updateListItems()
}
}
case "m", "r":
if m.state == itemFocus || m.state == contentFocus {
if m.selectedItem != nil {
m.selectedItem.ReadState = !m.selectedItem.ReadState
m.selectedItem.Save()
m.updateListItems()
}
}
case "o":
if m.selectedItem != nil {
_ = openUrl(m.selectedItem.Url)
}
case "enter":
if m.state == sidebarFocus {
idx := m.sidebar.Index()
if idx >= 0 && idx < len(m.feedData) {
m.selectedFeed = m.feedData[idx]
return m, loadItems(m.selectedFeed.Id)
}
} else if m.state == itemFocus {
idx := m.items.Index()
if idx >= 0 && idx < len(m.itemData) {
m.selectedItem = m.itemData[idx]
// Mark as read when opening
if !m.selectedItem.ReadState {
m.selectedItem.ReadState = true
m.selectedItem.Save()
m.updateListItems()
}
formattedContent := fmt.Sprintf("%s\n\n%s",
HeaderStyle.Render(m.selectedItem.Title),
m.selectedItem.Description)
m.content.SetContent(formattedContent)
m.content.YPosition = 0
m.state = contentFocus
}
}
}
}
// Route messages to components based on focus
if m.state == sidebarFocus {
m.sidebar, cmd = m.sidebar.Update(msg)
cmds = append(cmds, cmd)
} else if m.state == itemFocus {
m.items, cmd = m.items.Update(msg)
cmds = append(cmds, cmd)
} else if m.state == contentFocus {
m.content, cmd = m.content.Update(msg)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
func (m *Model) updateListItems() {
if len(m.itemData) == 0 {
return
}
items := make([]list.Item, len(m.itemData))
for i, it := range m.itemData {
title := it.Title
if it.Starred {
title = "★ " + title
}
if !it.ReadState {
title = "● " + title
}
items[i] = itemString(title)
}
m.items.SetItems(items)
}
func openUrl(url string) error {
// Simple xdg-open wrapper, ignored for now or use exec
return nil
}
func (m Model) View() string {
if m.err != nil {
return fmt.Sprintf("Error: %v", m.err)
}
if !m.ready {
return "Initializing..."
}
var sidebarView, mainView string
// Render Sidebar
if m.state == sidebarFocus {
sidebarView = ActivePaneStyle.Render(m.sidebar.View())
} else {
sidebarView = PaneStyle.Render(m.sidebar.View())
}
// Render Main Area (Item List or Content)
if m.state == contentFocus {
mainView = ActivePaneStyle.Render(m.content.View())
} else {
if m.state == itemFocus {
mainView = ActivePaneStyle.Render(m.items.View())
} else {
mainView = PaneStyle.Render(m.items.View())
}
}
return lipgloss.JoinHorizontal(lipgloss.Top, sidebarView, mainView)
}
func Run() error {
p := tea.NewProgram(NewModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
return err
}
return nil
}
|