Skip to content

Commit 045a63b

Browse files
authored
feat: add refreshinterval for bg refresh (#148)
add refreshinterval config option to set the background interval refresh in minutes calls fetchAllFeeds in background and sends a status message/refreshes feed list close #101
1 parent 2a82754 commit 045a63b

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ filtering:
7171
defaultIncludeFeedName: true
7272
```
7373

74+
### Refresh interval
75+
Background refresh interval in minutes. Setting this to anything but 0 will make `nom` refresh automatically.
76+
```yaml
77+
refreshinterval: 5
78+
```
79+
7480

7581
### Theme
7682
Theme allows some basic color overrides in the feed view and then setting a custom markdown render theme for the overall markdown view. `theme.glamour` can be one of "dark", "dracula", "light", "pink", "ascii" or "notty". See [here](https://github.com/charmbracelet/glamour/tree/master/styles/gallery) for previews and more info.

internal/commands/commands.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"regexp"
99
"strings"
1010
"sync"
11+
"time"
1112

1213
md "github.com/JohannesKaufmann/html-to-markdown"
1314
"github.com/charmbracelet/bubbles/list"
15+
tea "github.com/charmbracelet/bubbletea"
1416
"github.com/charmbracelet/glamour"
1517
"github.com/charmbracelet/glamour/ansi"
1618
"gopkg.in/yaml.v3"
@@ -99,8 +101,10 @@ func (c Commands) Refresh() error {
99101
if err != nil {
100102
return fmt.Errorf("commands Refresh: %w", err)
101103
}
104+
102105
return nil
103106
}
107+
104108
func (c Commands) ShowConfig() error {
105109
yaml, err := yaml.Marshal(&c.config)
106110
if err != nil {
@@ -189,6 +193,37 @@ func includes[T comparable](arr []T, item T) bool {
189193
return false
190194
}
191195

196+
func (c Commands) Monitor(prog *tea.Program) {
197+
if c.config.RefreshInterval == 0 {
198+
return
199+
}
200+
201+
go func() {
202+
t := time.NewTicker(time.Duration(c.config.RefreshInterval) * time.Minute)
203+
for _ = range t.C {
204+
err := c.Refresh()
205+
if err != nil {
206+
log.Println("Refresh failed: ", err)
207+
prog.Send(statusUpdate{
208+
status: "Refresh failed",
209+
})
210+
} else {
211+
items, err := c.GetAllFeeds()
212+
if err != nil {
213+
log.Println("Refresh failed: ", err)
214+
prog.Send(statusUpdate{
215+
status: "Refresh failed",
216+
})
217+
}
218+
prog.Send(listUpdate{
219+
items: convertItems(items),
220+
status: "Refreshed.",
221+
})
222+
}
223+
}
224+
}()
225+
}
226+
192227
func (c Commands) CountUnread() int {
193228
count, err := c.store.CountUnread()
194229
if err != nil {

internal/commands/list.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,13 @@ func updateList(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
166166

167167
switch msg := msg.(type) {
168168
case statusUpdate:
169-
m.list.NewStatusMessage(msg.status)
169+
cmds = append(cmds, m.list.NewStatusMessage(msg.status))
170170
case listUpdate:
171+
if m.list.SettingFilter() {
172+
break
173+
}
171174
m.list.SetItems(msg.items)
172-
m.list.NewStatusMessage(msg.status)
175+
cmds = append(cmds, m.list.NewStatusMessage(msg.status))
173176

174177
case tea.ResumeMsg:
175178
return m, nil

internal/commands/tui.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,22 @@ func (c *Commands) TUI() error {
216216
es = append(es, fmt.Sprintf("Error fetching %s: %s", e.FeedURL, e.Err))
217217
}
218218

219-
if err := Render(items, c, es, c.config); err != nil {
219+
prog, err := Render(items, c, es, c.config)
220+
221+
if err != nil {
220222
return fmt.Errorf("commands.TUI: %w", err)
221223
}
222224

225+
c.Monitor(prog)
226+
227+
if _, err := prog.Run(); err != nil {
228+
return fmt.Errorf("tui.Render: %w", err)
229+
}
230+
223231
return nil
224232
}
225233

226-
func Render(items []list.Item, cmds *Commands, errors []string, cfg *config.Config) error {
234+
func Render(items []list.Item, cmds *Commands, errors []string, cfg *config.Config) (*tea.Program, error) {
227235
const defaultWidth = 20
228236
_, ts, _ := term.GetSize(int(os.Stdout.Fd()))
229237
_, y := appStyle.GetFrameSize()
@@ -258,9 +266,7 @@ func Render(items []list.Item, cmds *Commands, errors []string, cfg *config.Conf
258266
viewport: vp,
259267
}
260268

261-
if _, err := tea.NewProgram(m, tea.WithAltScreen()).Run(); err != nil {
262-
return fmt.Errorf("tui.Render: %w", err)
263-
}
269+
prog := tea.NewProgram(m, tea.WithAltScreen())
264270

265-
return nil
271+
return prog, nil
266272
}

internal/config/config.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ type Config struct {
7070
Ordering string `yaml:"ordering"`
7171
Filtering FilterConfig `yaml:"filtering"`
7272
// Preview feeds are distinguished from Feeds because we don't want to inadvertenly write those into the config file.
73-
PreviewFeeds []Feed `yaml:"previewfeeds,omitempty"`
74-
Backends *Backends `yaml:"backends,omitempty"`
75-
ShowRead bool `yaml:"showread,omitempty"`
76-
AutoRead bool `yaml:"autoread,omitempty"`
77-
Openers []Opener `yaml:"openers,omitempty"`
78-
Theme Theme `yaml:"theme,omitempty"`
79-
HTTPOptions *HTTPOptions `yaml:"http,omitempty"`
73+
PreviewFeeds []Feed `yaml:"previewfeeds,omitempty"`
74+
Backends *Backends `yaml:"backends,omitempty"`
75+
ShowRead bool `yaml:"showread,omitempty"`
76+
AutoRead bool `yaml:"autoread,omitempty"`
77+
Openers []Opener `yaml:"openers,omitempty"`
78+
Theme Theme `yaml:"theme,omitempty"`
79+
HTTPOptions *HTTPOptions `yaml:"http,omitempty"`
80+
RefreshInterval int `yaml:"refreshinterval,omitempty"`
8081
}
8182

8283
func (c *Config) ToggleShowRead() {
@@ -125,7 +126,8 @@ func New(configPath string, pager string, previewFeeds []string, version string)
125126
FilterColor: "62",
126127
ReadIcon: "\u2713",
127128
},
128-
Ordering: constants.DefaultOrdering,
129+
RefreshInterval: 0,
130+
Ordering: constants.DefaultOrdering,
129131
Filtering: FilterConfig{
130132
DefaultIncludeFeedName: false,
131133
},
@@ -166,6 +168,7 @@ func (c *Config) Load() error {
166168
c.Openers = fileConfig.Openers
167169
c.ShowFavourites = fileConfig.ShowFavourites
168170
c.Filtering = fileConfig.Filtering
171+
c.RefreshInterval = fileConfig.RefreshInterval
169172

170173
if fileConfig.HTTPOptions != nil {
171174
if _, err := TLSVersion(fileConfig.HTTPOptions.MinTLSVersion); err != nil {

0 commit comments

Comments
 (0)