From cceee6f919a5806cb2b4b7e10bb5a721cb6e19ba Mon Sep 17 00:00:00 2001 From: azimut Date: Sat, 29 Apr 2023 09:58:23 -0300 Subject: [PATCH] format: add -W for max comment length useful when there are too nested comments less useful when there aren't many not applied to the OP for now --- cmd/discourseview/main.go | 25 ++++++++++++++----------- cmd/fourchanview/main.go | 6 +++--- cmd/hackerview/main.go | 28 ++++++++++++++++------------ cmd/redditview/main.go | 21 ++++++++++++--------- cmd/vichanview/main.go | 27 +++++++++++++++------------ internal/discourse/format.go | 13 ++++++++++--- internal/discourse/tui.go | 10 ++++------ internal/discourse/types.go | 17 +++++++++-------- internal/fourchan/format.go | 6 +++--- internal/fourchan/tui.go | 4 +--- internal/fourchan/type.go | 6 +++--- internal/hackernews/printer.go | 20 ++++++++++++-------- internal/hackernews/tui.go | 4 ++-- internal/hackernews/type.go | 13 +++++++------ internal/reddit/fetch.go | 2 +- internal/reddit/format.go | 19 +++++++++++-------- internal/reddit/tui.go | 10 ++++------ internal/reddit/type.go | 11 ++++++----- internal/vichan/fetch_test.go | 2 +- internal/vichan/format.go | 16 +++++++++++----- internal/vichan/tui.go | 4 ++-- internal/vichan/types.go | 17 +++++++++-------- 22 files changed, 156 insertions(+), 125 deletions(-) diff --git a/cmd/discourseview/main.go b/cmd/discourseview/main.go index b31a6c9..0d109a7 100644 --- a/cmd/discourseview/main.go +++ b/cmd/discourseview/main.go @@ -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 @@ -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") } @@ -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 diff --git a/cmd/fourchanview/main.go b/cmd/fourchanview/main.go index 364b26c..568b73b 100644 --- a/cmd/fourchanview/main.go +++ b/cmd/fourchanview/main.go @@ -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 diff --git a/cmd/hackerview/main.go b/cmd/hackerview/main.go index 0bf4326..b03d846 100644 --- a/cmd/hackerview/main.go +++ b/cmd/hackerview/main.go @@ -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 @@ -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") } @@ -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 { diff --git a/cmd/redditview/main.go b/cmd/redditview/main.go index 48a22f5..8e34634 100644 --- a/cmd/redditview/main.go +++ b/cmd/redditview/main.go @@ -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 @@ -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") } @@ -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)) diff --git a/cmd/vichanview/main.go b/cmd/vichanview/main.go index a4c4d9c..2149208 100644 --- a/cmd/vichanview/main.go +++ b/cmd/vichanview/main.go @@ -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 @@ -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") } @@ -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)) diff --git a/internal/discourse/format.go b/internal/discourse/format.go index e0ba191..3fb6c3c 100644 --- a/internal/discourse/format.go +++ b/internal/discourse/format.go @@ -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", @@ -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 += " " diff --git a/internal/discourse/tui.go b/internal/discourse/tui.go index 4ce0e16..fde855b 100644 --- a/internal/discourse/tui.go +++ b/internal/discourse/tui.go @@ -1,8 +1,6 @@ package discourse import ( - "fmt" - "github.com/azimut/cli-view/internal/tui" tea "github.com/charmbracelet/bubbletea" ) @@ -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 } diff --git a/internal/discourse/types.go b/internal/discourse/types.go index 426872d..540dc21 100644 --- a/internal/discourse/types.go +++ b/internal/discourse/types.go @@ -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 { diff --git a/internal/fourchan/format.go b/internal/fourchan/format.go index bfe8906..c66f1de 100644 --- a/internal/fourchan/format.go +++ b/internal/fourchan/format.go @@ -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( diff --git a/internal/fourchan/tui.go b/internal/fourchan/tui.go index fcf9e6e..eff0216 100644 --- a/internal/fourchan/tui.go +++ b/internal/fourchan/tui.go @@ -2,7 +2,6 @@ package fourchan import ( "github.com/azimut/cli-view/internal/tui" - "github.com/charmbracelet/bubbles/viewport" tea "github.com/charmbracelet/bubbletea" ) @@ -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 } diff --git a/internal/fourchan/type.go b/internal/fourchan/type.go index f58893a..3b2abc3 100644 --- a/internal/fourchan/type.go +++ b/internal/fourchan/type.go @@ -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 { diff --git a/internal/hackernews/printer.go b/internal/hackernews/printer.go index 9004f65..eb87ca6 100644 --- a/internal/hackernews/printer.go +++ b/internal/hackernews/printer.go @@ -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", @@ -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 = ">> " } @@ -53,15 +56,16 @@ 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}, @@ -69,7 +73,7 @@ func fixupComment(html string, leftPad int, width uint) string { if err != nil { panic(err) } - wrapped, _ := text.WrapLeftPadded(format.GreenTextIt(plainText), int(width), leftPad) + wrapped, _ := text.WrapLeftPadded(format.GreenTextIt(plainText), width, leftPad) return wrapped } diff --git a/internal/hackernews/tui.go b/internal/hackernews/tui.go index e21f329..2980812 100644 --- a/internal/hackernews/tui.go +++ b/internal/hackernews/tui.go @@ -29,13 +29,13 @@ 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.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.LineWidth = msg.Width - rightPadding m.render.Viewport.SetContent(m.String()) } return m, cmd diff --git a/internal/hackernews/type.go b/internal/hackernews/type.go index f8c280e..d3e6be9 100644 --- a/internal/hackernews/type.go +++ b/internal/hackernews/type.go @@ -7,11 +7,12 @@ import ( ) type Thread struct { - comments []Comment - op Op - Width uint - LeftPadding uint - ShowDate bool + comments []Comment + op Op + CommentWidth int + LineWidth int + LeftPadding int + ShowDate bool } type Op struct { @@ -31,7 +32,7 @@ type Comment struct { Childs []*Comment date time.Time id int - indent int + depth int kids []int msg string user string diff --git a/internal/reddit/fetch.go b/internal/reddit/fetch.go index d25004f..7a9a7c3 100644 --- a/internal/reddit/fetch.go +++ b/internal/reddit/fetch.go @@ -96,7 +96,7 @@ func toComment(jsonComment gjson.Result) Comment { return Comment{ author: jsonComment.Get("author").String(), createdUtc: jsonComment.Get("created_utc").Int(), - depth: jsonComment.Get("depth").Int(), + depth: int(jsonComment.Get("depth").Int()), id: jsonComment.Get("id").String(), jsonReplies: jsonComment.Get("replies.data.children.#.data").Array(), message: jsonComment.Get("body").String(), diff --git a/internal/reddit/format.go b/internal/reddit/format.go index dbb5bf5..a7075a0 100644 --- a/internal/reddit/format.go +++ b/internal/reddit/format.go @@ -19,8 +19,8 @@ var reMarkdownLink = regexp.MustCompile(`\[([^\]]+)\]\(([^\)]+)\)`) var reHTTPLink = regexp.MustCompile(`[^\[^\(^m]http[s]?://[^\s^\[^\(^\[]+`) func (op Op) String() (ret string) { - leftPadding := int(op.thread.LeftPadding) - maxWidth := int(op.thread.Width) + leftPadding := op.thread.LeftPadding + maxWidth := op.thread.LineWidth ret += "\n" ret += fmt.Sprintf("title: %s\n", op.title) ret += fmt.Sprintf(" self: %s\n", op.self) @@ -43,13 +43,16 @@ func (op Op) String() (ret string) { } func (comment Comment) String() (ret string) { - leftPadding := int(comment.thread.LeftPadding) - maxWidth := int(comment.thread.Width) - commentLeftPadding := int(comment.depth)*leftPadding + 1 + leftPadding := comment.thread.LeftPadding * comment.depth + rightPadding := 2 + lineWidth := format.Min( + comment.thread.LineWidth, + leftPadding+comment.thread.CommentWidth+1, + ) - rightPadding ret += fixupContent( comment.message, - maxWidth, - commentLeftPadding, + lineWidth, + leftPadding+1, ) author := comment.author @@ -57,7 +60,7 @@ func (comment Comment) String() (ret string) { author = format.AuthorStyle.Render(comment.author) } - ret += strings.Repeat(" ", int(comment.depth)*leftPadding) + ret += strings.Repeat(" ", leftPadding) ret += fmt.Sprintf( ">> %s(%d) - %s\n\n", author, diff --git a/internal/reddit/tui.go b/internal/reddit/tui.go index 53c336c..dc28986 100644 --- a/internal/reddit/tui.go +++ b/internal/reddit/tui.go @@ -1,8 +1,6 @@ package reddit import ( - "fmt" - "github.com/azimut/cli-view/internal/tui" tea "github.com/charmbracelet/bubbletea" ) @@ -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) // initializes the structs it needed switch msg := msg.(type) { case tea.WindowSizeMsg: - m.Width = uint(msg.Width) - rightPadding - m.render.Viewport.SetContent(fmt.Sprint(m)) + m.LineWidth = msg.Width - rightPadding + m.render.Viewport.SetContent(m.String()) } return m, cmd } diff --git a/internal/reddit/type.go b/internal/reddit/type.go index 3ffc513..1f1cc14 100644 --- a/internal/reddit/type.go +++ b/internal/reddit/type.go @@ -3,10 +3,11 @@ package reddit import "github.com/tidwall/gjson" type Thread struct { - comments []Comment - op Op - Width uint - LeftPadding uint + comments []Comment + op Op + LineWidth int + CommentWidth int + LeftPadding int } type Op struct { @@ -24,7 +25,7 @@ type Op struct { type Comment struct { author string createdUtc int64 - depth int64 + depth int id string jsonReplies []gjson.Result message string diff --git a/internal/vichan/fetch_test.go b/internal/vichan/fetch_test.go index 85f7415..b47cfad 100644 --- a/internal/vichan/fetch_test.go +++ b/internal/vichan/fetch_test.go @@ -12,7 +12,7 @@ func TestFetch(t *testing.T) { if err != nil { t.Errorf("%v", err) } - thread.Width = 80 + thread.LineWidth = 80 thread.LeftPadding = 3 got := thread.op.id expected := 18084 diff --git a/internal/vichan/format.go b/internal/vichan/format.go index 74e3213..8f9e2bf 100644 --- a/internal/vichan/format.go +++ b/internal/vichan/format.go @@ -35,8 +35,8 @@ func (op Op) String() (ret string) { if op.message != "" { ret += "\n" + format.FormatHtml2Text( op.message, - int(op.thread.Width), - int(op.thread.LeftPadding), + op.thread.LineWidth, + op.thread.LeftPadding, ) ret += "\n\n" } @@ -49,11 +49,17 @@ func (op Op) String() (ret string) { } func (comment Comment) String() (ret string) { + leftPadding := comment.thread.LeftPadding * comment.depth + rightPadding := 2 + lineWidth := format.Min( + comment.thread.LineWidth, + leftPadding+comment.thread.CommentWidth+1, + ) - rightPadding if comment.message != "" { ret += format.FormatHtml2Text( comment.message, - int(comment.thread.Width), - comment.depth*int(comment.thread.LeftPadding)+1, + lineWidth, + leftPadding+1, ) ret += "\n" } @@ -77,7 +83,7 @@ func (comment Comment) String() (ret string) { ret += "\n" for _, attachment := range comment.attachments { - ret += strings.Repeat(" ", comment.depth*3) + ret += strings.Repeat(" ", leftPadding) ret += fmt.Sprintf( ">> %s \"%s\"\n", path.Dir(path.Dir(comment.thread.url))+"/src/"+attachment.newFilename, diff --git a/internal/vichan/tui.go b/internal/vichan/tui.go index 8ed7681..443374e 100644 --- a/internal/vichan/tui.go +++ b/internal/vichan/tui.go @@ -31,13 +31,13 @@ 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.LineWidth = 300 m.render.RawContent = fmt.Sprint(m) } m.render, cmd = m.render.Update(msg) switch msg := msg.(type) { case tea.WindowSizeMsg: - m.Width = uint(msg.Width) - rightPadding + m.LineWidth = msg.Width - rightPadding m.render.Viewport.SetContent(fmt.Sprint(m)) } return m, cmd diff --git a/internal/vichan/types.go b/internal/vichan/types.go index 708e17b..5b515ce 100644 --- a/internal/vichan/types.go +++ b/internal/vichan/types.go @@ -9,14 +9,15 @@ import ( ) type Thread struct { - comments []Comment - op Op - LeftPadding uint - ShowAuthor bool - ShowDate bool - ShowId bool - Width uint - url string + comments []Comment + op Op + LeftPadding int + ShowAuthor bool + ShowDate bool + ShowId bool + LineWidth int + CommentWidth int + url string } type Attachment struct {