Skip to content

Commit

Permalink
tweak: fix cursor upon read/quit on last post, toggle read on same po…
Browse files Browse the repository at this point in the history
…st, and added read indication to post view
  • Loading branch information
coolsloth55 committed Jan 6, 2025
1 parent e770c61 commit 2b94370
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,13 @@ func getStyleConfigWithOverrides(theme config.Theme) (sc ansi.StyleConfig) {
func glamouriseItem(item store.Item, theme config.Theme) (string, error) {
var mdown string

mdown += "# " + item.Title
title := item.Title
// check mark indication post has been read
if item.Read() {
title = fmt.Sprintf("\u2713 - %s", item.Title)
}

mdown += "# " + title
mdown += "\n"
mdown += item.Author
if !item.PublishedAt.IsZero() {
Expand Down
2 changes: 2 additions & 0 deletions internal/commands/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type model struct {
list list.Model
help help.Model
viewport viewport.Model
lastRead *list.Item
lastReadIndex int
}

func (m model) Init() tea.Cmd {
Expand Down
30 changes: 29 additions & 1 deletion internal/commands/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func updateViewport(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
m.viewport.GotoBottom()

case key.Matches(msg, ViewportKeyMap.Escape):
// reset cursor if last post is read and quit
index := m.list.Index()
length := len(m.list.Items())
if index >= length && length >= 1 {
m.list.Select(index - 1)
}

m.selectedArticle = nil
cmds = append(cmds, m.UpdateList())

Expand Down Expand Up @@ -66,9 +73,30 @@ func updateViewport(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
}

if !m.commands.config.ShowRead {
m.list.RemoveItem(m.list.Index())
index := m.list.Index()

if m.lastRead != nil && current.ID == (*m.lastRead).(TUIItem).ID {
// un-read re-add post back to list
m.list.InsertItem(index, *m.lastRead)
m.lastReadIndex = index
m.lastRead = nil
} else {
// remove post and store backup for un-read
items := m.list.Items()
item := items[index]
m.list.RemoveItem(index)
m.lastReadIndex = index
m.lastRead = &item
}
}

// trigger refresh to update read indication
content, err := m.commands.GetGlamourisedArticle(*m.selectedArticle)
if err != nil {
return m, tea.Quit
}
m.viewport.SetContent(content)

case key.Matches(msg, ViewportKeyMap.Prev):
navIndex := getPrevIndex(&m)
items := m.list.Items()
Expand Down

0 comments on commit 2b94370

Please sign in to comment.