| 1 | package tui |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os/exec" |
| 6 | "runtime" |
| 7 | |
| 8 | tea "charm.land/bubbletea/v2" |
| 9 | "github.com/atotto/clipboard" |
| 10 | ) |
| 11 | |
| 12 | // CopyToClipboardCmd copies text to the OS clipboard and emits a StatusMsg. |
| 13 | func CopyToClipboardCmd(text, okStatus string) tea.Cmd { |
| 14 | return func() tea.Msg { |
| 15 | if err := clipboard.WriteAll(text); err != nil { |
| 16 | return StatusMsg{Text: "clipboard: " + err.Error()} |
| 17 | } |
| 18 | return StatusMsg{Text: okStatus, OK: true} |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // OpenURLCmd opens url in the default browser and emits a StatusMsg. |
| 23 | func OpenURLCmd(url string) tea.Cmd { |
| 24 | return func() tea.Msg { |
| 25 | if err := openURL(url); err != nil { |
| 26 | return StatusMsg{Text: "open: " + err.Error()} |
| 27 | } |
| 28 | return StatusMsg{Text: "opened " + url, OK: true} |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func openURL(url string) error { |
| 33 | var cmd *exec.Cmd |
| 34 | switch runtime.GOOS { |
| 35 | case "linux": |
| 36 | cmd = exec.Command("xdg-open", url) |
| 37 | case "darwin": |
| 38 | cmd = exec.Command("open", url) |
| 39 | case "windows": |
| 40 | cmd = exec.Command("rundll32", "url.dll,FileProtocolHandler", url) |
| 41 | default: |
| 42 | return fmt.Errorf("unsupported platform %s", runtime.GOOS) |
| 43 | } |
| 44 | return cmd.Start() |
| 45 | } |