Skip to content

Commit

Permalink
hackernews: add TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 26, 2023
1 parent 7e05424 commit bcef8de
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
8 changes: 7 additions & 1 deletion cmd/hackerview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/azimut/cli-view/internal/hackernews"
"github.com/azimut/cli-view/internal/tui"
"github.com/fatih/color"
)

Expand Down Expand Up @@ -60,7 +61,12 @@ func run(args []string, stdout io.Writer) error {
thread.LeftPadding = opts.leftPadding
thread.ShowDate = opts.showDate

fmt.Println(thread)
if opts.useTUI {
tui.RenderLoop(hackernews.NewProgram(*thread))
} else {
fmt.Println(thread)
}

return nil
}

Expand Down
41 changes: 41 additions & 0 deletions internal/hackernews/tui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package hackernews

import (
"fmt"

"github.com/azimut/cli-view/internal/tui"
tea "github.com/charmbracelet/bubbletea"
)

const rightPadding = 10

type Model struct {
render tui.Model
Thread
}

func NewProgram(thread Thread) *tea.Program {
return tea.NewProgram(Model{Thread: thread},
tea.WithAltScreen())
}

func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) View() string {
return m.render.View()
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
m.render, cmd = m.render.Update(msg)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.op.thread.Width = 200
m.render.RawContent = fmt.Sprint(m)
m.op.thread.Width = uint(msg.Width) - rightPadding
m.render.Viewport.SetContent(fmt.Sprint(m))
}
return m, cmd
}
8 changes: 1 addition & 7 deletions internal/tui/spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
)

// Useful if we want to do something with the error message returned by the spawned process
type commandFinishedMsg struct{ err error }

func doSpawn(url string) (tea.Cmd, error) {
Expand All @@ -15,13 +16,6 @@ func doSpawn(url string) (tea.Cmd, error) {
return nil, err
}
cmd := exec.Command(spawner, url)
// if err = cmd.Start(); err != nil {
// return nil, err
// }
// _, err = cmd.Process.Wait()
// if err != nil {
// return nil, err
// }
return tea.ExecProcess(cmd, func(err error) tea.Msg {
return commandFinishedMsg{err}
}), nil
Expand Down

0 comments on commit bcef8de

Please sign in to comment.