xonecas.io
5119
Lh

lazy-history / commit 0cc75f5

feat: execute and print output

Sean2025-05-12 0cc75f5bd34f11426ddef97cff77e616e0fb9ea2 parent 4cead04

2 files changed

.gitignore+1−0
@@ -1 +1,2 @@
11 *.log
2+debug.log
internal/list/list.go+35−5
@@ -1,12 +1,16 @@
11 package internal_list
22
33 import (
4+ "bytes"
45 "fmt"
56 "io"
7+ "os/exec"
8+ "strings"
69
710 "github.com/charmbracelet/bubbles/list"
811 tea "github.com/charmbracelet/bubbletea"
912 "github.com/charmbracelet/lipgloss"
13+ "github.com/sacenox/lazy-history/internal/debug"
1014 "github.com/samber/lo"
1115 )
1216
@@ -37,7 +41,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
3741 }
3842
3943 if index == m.Index() {
40- fmt.Fprint(w, "→ ")
44+ fmt.Fprint(w, " → ")
4145 }
4246
4347 fmt.Fprint(w, i)
@@ -46,14 +50,37 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list
4650 type model struct {
4751 list list.Model
4852 choice string
53+ output *bytes.Buffer
54+}
55+
56+type ExecuteMessage struct {
57+ Error error
4958 }
5059
5160 func (m model) Init() tea.Cmd {
5261 return nil
5362 }
5463
64+func (m model) Execute(command string) tea.Cmd {
65+ parts := strings.Split(command, " ")
66+ cmd := exec.Command(parts[0], parts[1:]...)
67+ cmd.Stdout = m.output
68+
69+ return tea.ExecProcess(cmd, func(err error) tea.Msg {
70+ return ExecuteMessage{Error: err}
71+ })
72+}
73+
5574 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5675 switch msg := msg.(type) {
76+
77+ case ExecuteMessage:
78+ if msg.Error != nil {
79+ debug.Debugf("Error executing command: %v", msg.Error)
80+ return m, tea.Quit
81+ }
82+ return m, tea.Quit
83+
5784 case tea.WindowSizeMsg:
5885 m.list.SetWidth(msg.Width)
5986 return m, nil
@@ -68,7 +95,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
6895 if ok {
6996 m.choice = string(i)
7097 }
71- return m, tea.Quit
98+ return m, m.Execute(m.choice)
7299 }
73100 }
74101
@@ -78,8 +105,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
78105 }
79106
80107 func (m model) View() string {
81- if m.choice != "" {
82- return ""
108+ debug.Debugf("view called")
109+ output := m.output.String()
110+ if output != "" {
111+ return output
83112 }
84113
85114 return "\n" + m.list.View()
@@ -101,6 +130,7 @@ func New(lines []string) model {
101130 l.SetShowStatusBar(false)
102131
103132 return model{
104- list: l,
133+ list: l,
134+ output: &bytes.Buffer{},
105135 }
106136 }