Skip to content

Commit

Permalink
fourchanview: add comment width, fix tui width updates
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Apr 29, 2023
1 parent adabd29 commit 7cb30bc
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 30 deletions.
21 changes: 14 additions & 7 deletions cmd/fourchanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import (
)

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

var opts options

func init() {
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.lineWidth, "w", 100, "max line width")
flag.UintVar(&opts.commentWidth, "W", 80, "max comment width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding for child comments")
}

Expand All @@ -43,12 +45,17 @@ func run(args []string, stdout io.Writer) error {
return errors.New("missing URL argument")
}
url := flag.Args()[0]
thread, err := fourchan.Fetch(url, opts.width, opts.leftPadding)

thread, err := fourchan.Fetch(url)
thread.LineWidth = opts.lineWidth
thread.LeftPadding = opts.leftPadding
thread.CommentWidth = opts.commentWidth

if err != nil {
return err
}
if opts.useTui {
tui.RenderLoop(fourchan.NewProgram(*thread))
tui.RenderLoop(fourchan.NewProgram(thread))
} else {
fmt.Println(thread)
}
Expand Down
14 changes: 14 additions & 0 deletions internal/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ func FormatHtml2Text(htmlText string, width, leftPadding int) string {
wrapped, _ := text.WrapLeftPadded(GreenTextIt(plainText), width, leftPadding)
return wrapped
}

func Min(a, b int) int {
if a < b {
return a
}
return b
}

func Max(a, b int) int {
if a > b {
return a
}
return b
}
4 changes: 1 addition & 3 deletions internal/fourchan/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/moshee/go-4chan-api/api"
)

func Fetch(rawUrl string, width, leftPadding uint) (*Thread, error) {
func Fetch(rawUrl string) (*Thread, error) {
threadId, board, err := parseUrl(rawUrl)
if err != nil {
return nil, err
Expand All @@ -15,8 +15,6 @@ func Fetch(rawUrl string, width, leftPadding uint) (*Thread, error) {
}

thread := toThread(apiThread)
thread.width = width
thread.leftPadding = leftPadding
thread.op.thread = thread

return thread, nil
Expand Down
6 changes: 5 additions & 1 deletion internal/fourchan/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
const URL = `https://boards.4channel.org/g/thread/76759434/this-board-is-for-the-discussion-of-technology`

func TestFormatThread(t *testing.T) {
thread, err := Fetch(URL, 100, 3)
thread, err := Fetch(URL)
thread.LineWidth = 100
thread.LeftPadding = 3
thread.CommentWidth = 80

if err != nil {
t.FailNow()
}
Expand Down
14 changes: 10 additions & 4 deletions internal/fourchan/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (op Op) String() (ret string) {
if op.comment != "" {
comment, _ := text.WrapLeftPadded(
format.GreenTextIt(op.comment),
int(op.thread.width),
int(op.thread.leftPadding),
int(op.thread.LineWidth),
int(op.thread.LeftPadding),
)
ret += comment + "\n"
}
Expand All @@ -43,11 +43,17 @@ func (op Op) String() (ret string) {
}

func (post Post) String() (ret string) {
leftPadding := int(post.thread.LeftPadding)*post.depth + 1
rightPadding := 2
lineWidth := format.Min(
int(post.thread.LineWidth),
leftPadding+int(post.thread.CommentWidth),
) - rightPadding
if post.comment != "" {
comment, _ := text.WrapLeftPadded(
format.GreenTextIt(post.comment),
int(post.thread.width),
post.depth*int(post.thread.leftPadding)+1,
lineWidth,
leftPadding,
)
ret += comment + "\n"
}
Expand Down
18 changes: 8 additions & 10 deletions internal/fourchan/tui.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package fourchan

import (
"fmt"

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

const rightPadding = 10

type Model struct {
render tui.Model
Thread
*Thread
}

func NewProgram(thread Thread) *tea.Program {
func NewProgram(thread *Thread) *tea.Program {
return tea.NewProgram(Model{Thread: thread},
tea.WithAltScreen())
}
Expand All @@ -31,14 +28,15 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
// Initialize data to be used for links scrapping
if m.render.RawContent == "" {
m.width = 300
m.render.RawContent = fmt.Sprint(m)
m.LineWidth = 300
m.render.RawContent = m.String()
}
m.render, cmd = m.render.Update(msg)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = uint(msg.Width) - rightPadding
m.render.Viewport.SetContent(fmt.Sprint(m))
m.LineWidth = uint(msg.Width)
m.render.Viewport.SetContent(m.String())
viewport.Sync(m.render.Viewport)
}
return m, cmd
}
11 changes: 6 additions & 5 deletions internal/fourchan/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
)

type Thread struct {
closed bool
leftPadding uint
op Op
posts []Post
width uint
closed bool
op Op
posts []Post
LineWidth uint
LeftPadding uint
CommentWidth uint
}

type Op struct {
Expand Down

0 comments on commit 7cb30bc

Please sign in to comment.