Skip to content

Commit

Permalink
format: add -W for max comment length
Browse files Browse the repository at this point in the history
useful when there are too nested comments
less useful when there aren't many
not applied to the OP for now
  • Loading branch information
azimut committed Apr 29, 2023
1 parent e0d4bb1 commit cceee6f
Show file tree
Hide file tree
Showing 22 changed files with 156 additions and 125 deletions.
25 changes: 14 additions & 11 deletions cmd/discourseview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
)

type options struct {
leftPadding uint
timeout time.Duration
useColors bool
showAuthor bool
showDate bool
showId bool
userAgent string
useTUI bool
width uint
leftPadding uint
timeout time.Duration
useColors bool
showAuthor bool
showDate bool
showId bool
userAgent string
useTUI bool
lineWidth uint
commentWidth uint
}

var opts options
Expand All @@ -35,7 +36,8 @@ func init() {
flag.BoolVar(&opts.useTUI, "x", false, "use TUI")
flag.DurationVar(&opts.timeout, "t", time.Second*5, "timeout in seconds")
flag.StringVar(&opts.userAgent, "A", "DiscourseView/1.0", "user agent to send")
flag.UintVar(&opts.width, "w", 100, "terminal width")
flag.UintVar(&opts.lineWidth, "w", 100, "line width")
flag.UintVar(&opts.commentWidth, "W", 80, "comment width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding on comments")
}

Expand All @@ -58,7 +60,8 @@ func run(args []string, stdout io.Writer) error {
if err != nil {
return errors.New("could not fetch url")
}
thread.Width = int(opts.width)
thread.CommentWidth = int(opts.commentWidth)
thread.LineWidth = int(opts.lineWidth)
thread.LeftPadding = int(opts.leftPadding)
thread.ShowAuthor = opts.showAuthor
thread.ShowDate = opts.showDate
Expand Down
6 changes: 3 additions & 3 deletions cmd/fourchanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func run(args []string, stdout io.Writer) error {
url := flag.Args()[0]

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

if err != nil {
return err
Expand Down
28 changes: 16 additions & 12 deletions cmd/hackerview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
)

type options struct {
leftPadding uint
maxComments uint
nWorkers uint
timeout time.Duration
showColors bool
showDate bool
useTUI bool
width uint
leftPadding uint
maxComments uint
nWorkers uint
timeout time.Duration
showColors bool
showDate bool
useTUI bool
lineWidth uint
commentWidth uint
}

var opts options
Expand All @@ -33,8 +34,9 @@ func init() {
flag.BoolVar(&opts.useTUI, "x", false, "use TUI")
flag.DurationVar(&opts.timeout, "t", time.Second*5, "timeout in seconds")
flag.UintVar(&opts.maxComments, "c", 10, "limits the ammount of comments to fetch")
flag.UintVar(&opts.nWorkers, "W", 3, "number of workers to fetch comments")
flag.UintVar(&opts.width, "w", 100, "fixed with")
flag.UintVar(&opts.nWorkers, "n", 3, "number of workers to fetch comments")
flag.UintVar(&opts.lineWidth, "w", 100, "line width")
flag.UintVar(&opts.commentWidth, "W", 80, "comment width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding")
}

Expand All @@ -57,8 +59,10 @@ func run(args []string, stdout io.Writer) error {
if err != nil {
return errors.New("could not fetch url")
}
thread.Width = opts.width
thread.LeftPadding = opts.leftPadding

thread.CommentWidth = int(opts.commentWidth)
thread.LeftPadding = int(opts.leftPadding)
thread.LineWidth = int(opts.lineWidth)
thread.ShowDate = opts.showDate

if opts.useTUI {
Expand Down
21 changes: 12 additions & 9 deletions cmd/redditview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (
)

type options struct {
leftPadding uint
maxWidth uint
timeout time.Duration
useColors bool
userAgent string
useTUI bool
leftPadding uint
lineWidth uint
commentWidth uint
timeout time.Duration
useColors bool
userAgent string
useTUI bool
}

var opts options
Expand All @@ -30,7 +31,8 @@ func init() {
flag.BoolVar(&opts.useColors, "C", true, "use colors")
flag.DurationVar(&opts.timeout, "t", time.Second*5, "timeout in seconds")
flag.StringVar(&opts.userAgent, "A", "Reddit_Cli/0.1", "user agent to send to reddit")
flag.UintVar(&opts.maxWidth, "w", 110, "max console width")
flag.UintVar(&opts.lineWidth, "w", 100, "line width")
flag.UintVar(&opts.commentWidth, "W", 80, "comment width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding on comments")
}

Expand All @@ -53,8 +55,9 @@ func run(args []string, stdout io.Writer) error {
if err != nil {
return err
}
thread.Width = opts.maxWidth
thread.LeftPadding = opts.leftPadding
thread.CommentWidth = int(opts.commentWidth)
thread.LineWidth = int(opts.lineWidth)
thread.LeftPadding = int(opts.leftPadding)

if opts.useTUI {
tui.RenderLoop(reddit.NewProgram(thread))
Expand Down
27 changes: 15 additions & 12 deletions cmd/vichanview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
)

type options struct {
leftPadding uint
timeout time.Duration
useColors bool
showAuthor bool
showDate bool
showId bool
userAgent string
useTUI bool
width uint
leftPadding uint
timeout time.Duration
useColors bool
showAuthor bool
showDate bool
showId bool
userAgent string
useTUI bool
lineWidth uint
commentWidth uint
}

var opts options
Expand All @@ -36,7 +37,8 @@ func init() {
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.commentWidth, "W", 100, "comment width")
flag.UintVar(&opts.lineWidth, "w", 100, "line width")
flag.UintVar(&opts.leftPadding, "l", 3, "left padding on comments")
}

Expand All @@ -60,11 +62,12 @@ func run(args []string, stdout io.Writer) error {
return errors.New("could not fetch url")
}

thread.LeftPadding = opts.leftPadding
thread.CommentWidth = int(opts.commentWidth)
thread.LeftPadding = int(opts.leftPadding)
thread.LineWidth = int(opts.lineWidth)
thread.ShowAuthor = opts.showAuthor
thread.ShowDate = opts.showDate
thread.ShowId = opts.showId
thread.Width = opts.width

if opts.useTUI {
tui.RenderLoop(vichan.NewProgram(thread))
Expand Down
13 changes: 10 additions & 3 deletions internal/discourse/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (o Op) String() (ret string) {
ret += fmt.Sprintf(" self: %s\n", o.thread.url)
ret += fmt.Sprintf(
"\n%s\n\n",
format.FormatHtml2Text(o.message, o.thread.Width, o.thread.LeftPadding),
format.FormatHtml2Text(o.message, o.thread.LineWidth, o.thread.LeftPadding),
)
ret += fmt.Sprintf(
"%s - %s \n\n\n",
Expand All @@ -33,11 +33,18 @@ func (o Op) String() (ret string) {
}

func (c Comment) String() (ret string) {
leftPadding := c.thread.LeftPadding * c.depth
rightPadding := 2
extraLeft := 1
lineWidth := format.Min(
c.thread.LineWidth,
leftPadding+c.thread.CommentWidth+extraLeft,
) - rightPadding
ret += fmt.Sprintf(
"%s\n",
format.FormatHtml2Text(c.message, c.thread.Width, c.thread.LeftPadding*c.depth+1),
format.FormatHtml2Text(c.message, lineWidth, leftPadding+extraLeft),
)
ret += strings.Repeat(" ", c.depth*c.thread.LeftPadding)
ret += strings.Repeat(" ", leftPadding)
ret += ">>"
if c.thread.ShowAuthor {
ret += " "
Expand Down
10 changes: 4 additions & 6 deletions internal/discourse/tui.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package discourse

import (
"fmt"

"github.com/azimut/cli-view/internal/tui"
tea "github.com/charmbracelet/bubbletea"
)
Expand Down Expand Up @@ -31,14 +29,14 @@ 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 = msg.Width - rightPadding
m.render.Viewport.SetContent(fmt.Sprint(m))
m.LineWidth = msg.Width - rightPadding
m.render.Viewport.SetContent(m.String())
}
return m, cmd
}
17 changes: 9 additions & 8 deletions internal/discourse/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
)

type Thread struct {
comments []Comment
op Op
id int
url string
Width int
LeftPadding int
ShowDate bool
ShowAuthor bool
comments []Comment
op Op
id int
url string
LineWidth int
CommentWidth int
LeftPadding int
ShowDate bool
ShowAuthor bool
}

type Op struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/fourchan/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (op Op) String() (ret string) {
}

func (post Post) String() (ret string) {
leftPadding := int(post.thread.LeftPadding)*post.depth + 1
leftPadding := post.thread.LeftPadding*post.depth + 1
rightPadding := 2
lineWidth := format.Min(
int(post.thread.LineWidth),
leftPadding+int(post.thread.CommentWidth),
post.thread.LineWidth,
leftPadding+post.thread.CommentWidth,
) - rightPadding
if post.comment != "" {
comment, _ := text.WrapLeftPadded(
Expand Down
4 changes: 1 addition & 3 deletions internal/fourchan/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fourchan

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

Expand Down Expand Up @@ -34,9 +33,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.render, cmd = m.render.Update(msg)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.LineWidth = uint(msg.Width)
m.LineWidth = msg.Width
m.render.Viewport.SetContent(m.String())
viewport.Sync(m.render.Viewport)
}
return m, cmd
}
6 changes: 3 additions & 3 deletions internal/fourchan/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type Thread struct {
closed bool
op Op
posts []Post
LineWidth uint
LeftPadding uint
CommentWidth uint
LineWidth int
LeftPadding int
CommentWidth int
}

type Op struct {
Expand Down
20 changes: 12 additions & 8 deletions internal/hackernews/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (o Op) String() (ret string) {
}
ret += fmt.Sprintf(" self: %s\n", o.selfUrl)
if o.text != "" {
ret += fmt.Sprintf("\n%s\n", fixupComment(o.text, 3, o.thread.Width))
ret += fmt.Sprintf("\n%s\n", fixupComment(o.text, 3, o.thread.LineWidth))
}
ret += fmt.Sprintf(
"\n%s(%d) - %s - %d Comments\n",
Expand All @@ -40,10 +40,13 @@ func (o Op) String() (ret string) {
}

func (c Comment) String() (ret string) {
indent := c.indent * int(c.thread.LeftPadding)
ret += "\n" + fixupComment(c.msg, indent+1, c.thread.Width) + "\n"
leftPadding := c.thread.LeftPadding * c.depth
rightPadding := 2
lineWidth := format.Min(c.thread.LineWidth, leftPadding+c.thread.CommentWidth+1) - rightPadding
ret += "\n" + fixupComment(c.msg, leftPadding+1, lineWidth) + "\n"

arrow := ">> "
if c.indent > 0 {
if c.depth > 0 {
arrow = ">> "
}

Expand All @@ -53,23 +56,24 @@ func (c Comment) String() (ret string) {
}

if c.thread.ShowDate {
ret += strings.Repeat(" ", indent) + arrow + author + " - " + humanize.Time(c.date)
ret += strings.Repeat(" ", leftPadding) + arrow + author + " - " + humanize.Time(c.date)
} else {
ret += strings.Repeat(" ", indent) + arrow + author
ret += strings.Repeat(" ", leftPadding) + arrow + author
}

ret += "\n"
return
}

func fixupComment(html string, leftPad int, width uint) string {
func fixupComment(html string, leftPad int, width int) string {
plainText, err := html2text.FromString(
html,
html2text.Options{OmitLinks: false, PrettyTables: true, CitationStyleLinks: true},
)
if err != nil {
panic(err)
}
wrapped, _ := text.WrapLeftPadded(format.GreenTextIt(plainText), int(width), leftPad)
wrapped, _ := text.WrapLeftPadded(format.GreenTextIt(plainText), width, leftPad)
return wrapped
}

Expand Down
Loading

0 comments on commit cceee6f

Please sign in to comment.