Skip to content

Commit

Permalink
feat: add takeover openers
Browse files Browse the repository at this point in the history
allows usage of blocking openers such as lynx browser to
be used within the tui, taking over from nom

related #73
  • Loading branch information
guyfedwards authored Jun 11, 2024
1 parent d084c92 commit 02d80fa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,14 @@ backends:
By default links are opened in the browser, you can specify commands to open certain links based on a regex string.
`regex` can be any valid golang regex string, it will be matched against the feed item link.
`cmd` is run as a child command. The `%s` denotes the position of the link in the command.
`takeover` dictates if the command should takeover the tty from nom. E.g. for opening links in lynx or other TUI.
```yaml
openers:
- regex: "youtube"
cmd: "mpv %s"
- regex: ".*"
cmd: "lynx %s"
takeover: true
```

## Store
Expand Down
23 changes: 17 additions & 6 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,36 @@ func convertItems(its []store.Item) []list.Item {
return items
}

func (c Commands) OpenLink(url string) error {
func (c Commands) OpenLink(url string) tea.Cmd {
for _, o := range c.config.Openers {
match, err := regexp.MatchString(o.Regex, url)
if err != nil {
return fmt.Errorf("OpenLink: regex: %w", err)
return tea.Quit
}

if match {
c := fmt.Sprintf(o.Cmd, url)
parts := strings.Fields(c)

cmd := exec.Command(parts[0], parts[1:]...)
if err := cmd.Run(); err != nil {
return fmt.Errorf("OpenLink: exec: %w", err)

if o.Takeover {
return tea.ExecProcess(cmd, func(err error) tea.Msg {
log.Println("OpenLink: takeover exec:", err)
return nil
})
} else {
if err := cmd.Run(); err != nil {
log.Println("OpenLink: exec: ", err)
return tea.Quit
}
return nil
}
}
}

return c.OpenInBrowser(url)
c.OpenInBrowser(url)

return nil
}

func (c Commands) OpenInBrowser(url string) error {
Expand Down
15 changes: 4 additions & 11 deletions internal/commands/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,8 @@ func updateList(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
}

current := item.(TUIItem)
err := m.commands.OpenLink(current.URL)
if err != nil {
return m, tea.Quit
}
cmd = m.commands.OpenLink(current.URL)
cmds = append(cmds, cmd)

case key.Matches(msg, ListKeyMap.Open):
if m.list.SettingFilter() {
Expand All @@ -296,9 +294,6 @@ func updateList(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
}
case key.Matches(msg, ListKeyMap.EditConfig):
// open editor with config file
// take output of editor and put into file
//
filePath := m.cfg.ConfigPath

cmd := strings.Split(getEditor("NOMEDITOR", "EDITOR"), " ")
Expand Down Expand Up @@ -350,10 +345,8 @@ func updateViewport(msg tea.Msg, m model) (tea.Model, tea.Cmd) {

case key.Matches(msg, ViewportKeyMap.OpenInBrowser):
current := m.list.SelectedItem().(TUIItem)
err := m.commands.OpenLink(current.URL)
if err != nil {
return m, tea.Quit
}
cmd = m.commands.OpenLink(current.URL)
cmds = append(cmds, cmd)

case key.Matches(msg, ViewportKeyMap.Prev):
current := m.list.Index()
Expand Down
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Backends struct {
}

type Opener struct {
Regex string `yaml:"regex"`
Cmd string `yaml:"cmd"`
Regex string `yaml:"regex"`
Cmd string `yaml:"cmd"`
Takeover bool `yaml:"takeover"`
}

type Theme struct {
Expand Down

0 comments on commit 02d80fa

Please sign in to comment.