Skip to content

Commit

Permalink
tui: move more common stuff to internal/tui.go
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 25, 2023
1 parent 221f4b4 commit ee3e6c3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/fourchanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type options struct {
var opts options

func init() {
flag.BoolVar(&opts.useTui, "u", false, "use tui")
flag.BoolVar(&opts.useTui, "x", 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 Down
35 changes: 9 additions & 26 deletions internal/fourchan/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,38 @@ package fourchan

import (
"fmt"
"os"

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

const rightPadding = 10

type Model struct {
viewport viewport.Model
ready bool
render tui.Model
Thread
}

func NewProgram(thread Thread) *tea.Program {
return tea.NewProgram(Model{Thread: thread}, tea.WithAltScreen())
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()
return m.render.Viewport.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:
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
}
m.op.thread.width = uint(msg.Width) - rightPadding
m.render.Viewport.SetContent(fmt.Sprint(m))
}
// Handle keyboard and mouse events in the viewport
var cmd tea.Cmd
m.viewport, cmd = m.viewport.Update(msg)
return m, cmd
}
68 changes: 59 additions & 9 deletions internal/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ import (
"os"

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

type Model struct {
keymap KeyMap
Viewport viewport.Model
onLinkScreen bool
IsReady bool
}

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
Top key.Binding
Bottom key.Binding
Next key.Binding
Prev key.Binding
Quit key.Binding
}

var DefaultKeyMap = KeyMap{
var DefaultViewportKeyMap = viewport.KeyMap{
Up: key.NewBinding(
key.WithKeys("k", "up", "ctrl+p"),
key.WithHelp("↑/k", "move up"),
Expand All @@ -36,6 +41,9 @@ var DefaultKeyMap = KeyMap{
key.WithKeys("pgup", "b", "alt+v"),
key.WithHelp("b/pgup", "page up"),
),
}

var DefaultKeyMap = KeyMap{
Top: key.NewBinding(
key.WithKeys("g"),
key.WithHelp("g", "jump to top"),
Expand All @@ -52,6 +60,48 @@ var DefaultKeyMap = KeyMap{
key.WithKeys("p"),
key.WithHelp("p", "next comment"),
),
Quit: key.NewBinding(
key.WithKeys("q", "esc", "ctrl-c"),
key.WithHelp("q", "quit"),
),
}

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

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

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
// TODO: not using useHighPerformanceRenderer
case tea.WindowSizeMsg:
if !m.IsReady {
m.Viewport = viewport.Model{
Width: msg.Width,
Height: msg.Height,
KeyMap: DefaultViewportKeyMap,
}
m.IsReady = true
} else {
m.Viewport.Height = msg.Height
m.Viewport.Width = msg.Width
}
case tea.KeyMsg:
switch {
case key.Matches(msg, DefaultKeyMap.Top):
m.Viewport.GotoTop()
case key.Matches(msg, DefaultKeyMap.Bottom):
m.Viewport.GotoBottom()
case key.Matches(msg, DefaultKeyMap.Quit):
return m, tea.Quit
}
}
var cmd tea.Cmd
m.Viewport, cmd = m.Viewport.Update(msg)
return m, cmd
}

func RenderLoop(p *tea.Program) {
Expand Down

0 comments on commit ee3e6c3

Please sign in to comment.