Skip to content

Commit

Permalink
tui: add option to open explicitly open with "xdg-open"
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 26, 2023
1 parent 6dcd5d5 commit 726b332
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
import (
"fmt"
"os"
"os/exec"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
Expand All @@ -20,13 +21,14 @@ type Model struct {
}

type KeyMap struct {
Top key.Binding
Bottom key.Binding
Next key.Binding
Prev key.Binding
Quit key.Binding
LinksView key.Binding
LinksOpen key.Binding // TODO: move this elsewhere
Top key.Binding
Bottom key.Binding
Next key.Binding
Prev key.Binding
Quit key.Binding
LinksView key.Binding
LinksOpen key.Binding // TODO: move this elsewhere
LinksOpenXDG key.Binding
}

var DefaultKeyMap = KeyMap{
Expand Down Expand Up @@ -58,6 +60,10 @@ var DefaultKeyMap = KeyMap{
key.WithKeys("o", "enter"),
key.WithHelp("o", "open link"),
),
LinksOpenXDG: key.NewBinding(
key.WithKeys("O"),
key.WithHelp("O", "open link with xdg-open"),
),
}

func (m Model) Init() tea.Cmd {
Expand Down Expand Up @@ -108,6 +114,19 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
m.onLinkScreen = false
case key.Matches(msg, DefaultKeyMap.Quit):
m.onLinkScreen = false
case key.Matches(msg, DefaultKeyMap.LinksOpenXDG):
i, ok := m.list.SelectedItem().(item)
if ok {
url := string(i)
binary, err := exec.LookPath("xdg-open")
if err != nil {
panic(err)
}
err = exec.Command(binary, url).Start()
if err != nil {
panic(err)
}
}
case key.Matches(msg, DefaultKeyMap.LinksOpen):
i, ok := m.list.SelectedItem().(item)
if ok {
Expand Down

0 comments on commit 726b332

Please sign in to comment.