Skip to content

Commit

Permalink
vichan: add tui
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 27, 2023
1 parent 358c0a2 commit ac7a433
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 33 deletions.
31 changes: 19 additions & 12 deletions cmd/vichanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"time"

"github.com/azimut/cli-view/internal/tui"
"github.com/azimut/cli-view/internal/vichan"
"github.com/fatih/color"
)
Expand All @@ -21,6 +22,7 @@ type options struct {
showDate bool
showId bool
userAgent string
useTUI bool
width uint
}

Expand All @@ -31,10 +33,11 @@ func init() {
flag.BoolVar(&opts.showDate, "d", false, "print date on comments")
flag.BoolVar(&opts.showAuthor, "a", true, "print author on comments")
flag.BoolVar(&opts.showId, "i", true, "print id on comments")
flag.BoolVar(&opts.useTUI, "x", false, "use TUI")
flag.DurationVar(&opts.timeout, "t", time.Second*5, "timeout in seconds")
flag.StringVar(&opts.userAgent, "A", "VichanView/1.0", "user agent to send")
flag.UintVar(&opts.width, "w", 100, "terminal width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding on comments")
}

func usage() {
Expand All @@ -50,21 +53,25 @@ func run(args []string, stdout io.Writer) error {
flag.Usage()
return errors.New("missing URL argument")
}

url := flag.Args()[0]
thread, err := vichan.Fetch(
url,
opts.userAgent,
opts.width,
opts.leftPadding,
opts.timeout,
opts.showAuthor,
opts.showDate,
opts.showId,
)
thread, err := vichan.Fetch(url, opts.userAgent, opts.timeout)
if err != nil {
return errors.New("could not fetch url")
}
fmt.Println(thread)

thread.LeftPadding = opts.leftPadding
thread.ShowAuthor = opts.showAuthor
thread.ShowDate = opts.showDate
thread.ShowId = opts.showId
thread.Width = opts.width

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

return nil
}

Expand Down
7 changes: 0 additions & 7 deletions internal/vichan/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (

func Fetch(
rawUrl, userAgent string,
width, leftPadding uint,
timeout time.Duration,
showAuthor, showDate, showId bool,
) (*Thread, error) {
effectiveUrl, err := parseUrl(rawUrl)
if err != nil {
Expand All @@ -36,12 +34,7 @@ func Fetch(
}

thread.op.thread = thread
thread.leftPadding = leftPadding
thread.showAuthor = showAuthor
thread.showDate = showDate
thread.showId = showId
thread.url = rawUrl
thread.width = width

return thread, nil
}
Expand Down
4 changes: 3 additions & 1 deletion internal/vichan/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (

func TestFetch(t *testing.T) {
url := "https://lainchan.org/sec/res/18084.html"
thread, err := Fetch(url, "LainView/1.0", 100, 3, time.Second*5, true, false, true)
thread, err := Fetch(url, "LainView/1.0", time.Second*5)
if err != nil {
t.Errorf("%v", err)
}
thread.Width = 80
thread.LeftPadding = 3
got := thread.op.id
expected := 18084
fmt.Println(len(thread.comments))
Expand Down
14 changes: 7 additions & 7 deletions internal/vichan/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func (op Op) String() (ret string) {
)
}
if op.message != "" {
ret += "\n" + formatText(op.message, int(op.thread.width), int(op.thread.leftPadding))
ret += "\n" + formatText(op.message, int(op.thread.Width), int(op.thread.LeftPadding))
ret += "\n\n"
}
ret += " " + humanize.Time(op.createdAt)
if op.thread.showAuthor {
if op.thread.ShowAuthor {
ret += " by " + op.author
}
ret += "\n\n\n"
Expand All @@ -59,21 +59,21 @@ func (comment Comment) String() (ret string) {
if comment.message != "" {
ret += formatText(
comment.message,
int(comment.thread.width),
comment.depth*int(comment.thread.leftPadding)+1,
int(comment.thread.Width),
comment.depth*int(comment.thread.LeftPadding)+1,
)
ret += "\n"
}

ret += strings.Repeat(" ", comment.depth*3)
ret += ">>"
if comment.thread.showDate {
if comment.thread.ShowDate {
ret += " " + humanize.Time(comment.createdAt)
}
if comment.thread.showAuthor {
if comment.thread.ShowAuthor {
ret += " " + comment.author
}
if comment.thread.showId {
if comment.thread.ShowId {
ret += " " + fmt.Sprintf("%d", comment.id)
}
ret += "\n"
Expand Down
41 changes: 41 additions & 0 deletions internal/vichan/tui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package vichan

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.Width = 200
m.render.RawContent = fmt.Sprint(m)
m.Width = uint(msg.Width) - rightPadding
m.render.Viewport.SetContent(fmt.Sprint(m))
}
return m, cmd
}
12 changes: 6 additions & 6 deletions internal/vichan/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
)

type Thread struct {
op Op
comments []Comment
leftPadding uint
width uint
op Op
LeftPadding uint
ShowAuthor bool
ShowDate bool
ShowId bool
Width uint
url string
showAuthor bool
showDate bool
showId bool
}

type Attachment struct {
Expand Down

0 comments on commit ac7a433

Please sign in to comment.