From 726b332e4c7993fd301e2eed239ee6df2468b545 Mon Sep 17 00:00:00 2001 From: azimut Date: Wed, 26 Apr 2023 16:19:21 -0300 Subject: [PATCH] tui: add option to open explicitly open with "xdg-open" --- internal/tui/tui.go | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 01d2d59..2978ce0 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -3,6 +3,7 @@ package tui import ( "fmt" "os" + "os/exec" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/list" @@ -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{ @@ -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 { @@ -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 {