Skip to content

Commit

Permalink
tui: init direct viewport usage with fourchan
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 25, 2023
1 parent e17bc92 commit 221f4b4
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 40 deletions.
9 changes: 8 additions & 1 deletion cmd/fourchanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import (
"os"

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

type options struct {
leftPadding uint
showColors bool
useTui bool
width uint
}

var opts options

func init() {
flag.BoolVar(&opts.useTui, "u", false, "use tui")
flag.BoolVar(&opts.showColors, "C", true, "show colors")
flag.UintVar(&opts.width, "w", 80, "fixed with")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding for child comments")
Expand All @@ -44,7 +47,11 @@ func run(args []string, stdout io.Writer) error {
if err != nil {
return err
}
fmt.Println(thread)
if opts.useTui {
tui.RenderLoop(fourchan.NewProgram(*thread))
} else {
fmt.Println(thread)
}
return nil
}

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

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
)

const rightPadding = 10

type Model struct {
viewport viewport.Model
ready bool
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.viewport.View()
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !m.ready {
m.viewport = viewport.Model{Width: msg.Width, Height: msg.Height}
m.op.thread.width = uint(msg.Width) - rightPadding
m.viewport.SetContent(m.String())
m.ready = true
} else {
fmt.Fprintln(os.Stderr, msg.Width, "x", msg.Height)
fmt.Fprintln(os.Stderr, m.width)
m.viewport.Height = msg.Height
m.viewport.Width = msg.Width
m.op.thread.width = uint(msg.Width) - rightPadding
m.viewport.SetContent(fmt.Sprint(m))
}
case tea.KeyMsg:
if k := msg.String(); k == "q" {
return m, tea.Quit
}
}
// Handle keyboard and mouse events in the viewport
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}
89 changes: 50 additions & 39 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
package tui

import (
"github.com/charmbracelet/bubbles/viewport"
"fmt"
"os"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)

const verticalPadding = 2

type model struct {
content string
ready bool
viewport viewport.Model
type KeyMap struct {
Top key.Binding
Bottom key.Binding
PageDown key.Binding
PageUp key.Binding
Down key.Binding
Up key.Binding
Next key.Binding
Prev key.Binding
}

func (m model) Init() tea.Cmd {
return nil
var DefaultKeyMap = KeyMap{
Up: key.NewBinding(
key.WithKeys("k", "up", "ctrl+p"),
key.WithHelp("↑/k", "move up"),
),
Down: key.NewBinding(
key.WithKeys("j", "down", "ctrl+n"),
key.WithHelp("↓/j", "move down"),
),
PageDown: key.NewBinding(
key.WithKeys("pgdown", " ", "f", "ctrl+v"),
key.WithHelp("f/pgdn", "page down"),
),
PageUp: key.NewBinding(
key.WithKeys("pgup", "b", "alt+v"),
key.WithHelp("b/pgup", "page up"),
),
Top: key.NewBinding(
key.WithKeys("g"),
key.WithHelp("g", "jump to top"),
),
Bottom: key.NewBinding(
key.WithKeys("G"),
key.WithHelp("G", "jump to bottom"),
),
Next: key.NewBinding(
key.WithKeys("n"),
key.WithHelp("n", "next comment"),
),
Prev: key.NewBinding(
key.WithKeys("p"),
key.WithHelp("p", "next comment"),
),
}

func (m model) View() string {
if !m.ready {
return "\n Initializing..."
func RenderLoop(p *tea.Program) {
if _, err := p.Run(); err != nil {
fmt.Printf("error at last: %v", err)
os.Exit(1)
}
return m.viewport.View()
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !m.ready {
m.viewport = viewport.Model{Width: msg.Width, Height: msg.Height - verticalPadding}
m.viewport.SetContent(m.content)
m.ready = true
} else {
m.viewport.Width = msg.Width
m.viewport.Height = msg.Height - verticalPadding
}
case tea.KeyMsg:
if k := msg.String(); k == "q" {
return m, tea.Quit
}
}
return m, nil
}

func NewProgram(content string) *tea.Program {
p := tea.NewProgram(
model{content: content},
tea.WithAltScreen())
return p
}

0 comments on commit 221f4b4

Please sign in to comment.