From 908dabb5343cbc45b270ed4ab5ef186cdb82cad3 Mon Sep 17 00:00:00 2001 From: azimut Date: Tue, 26 Jul 2022 23:23:26 -0300 Subject: [PATCH] take width from opts --- cmd/hackerview/main.go | 5 +---- internal/hackernews/printer.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cmd/hackerview/main.go b/cmd/hackerview/main.go index 3f7aeea..6370ed1 100644 --- a/cmd/hackerview/main.go +++ b/cmd/hackerview/main.go @@ -48,10 +48,7 @@ func run(args []string, stdout io.Writer) error { if err != nil { return errors.New("could not fetch url") } - fmt.Println(op) - for _, comment := range *comments { - fmt.Println(&comment) - } + hackernews.Format(opt.width, op, comments) return nil } diff --git a/internal/hackernews/printer.go b/internal/hackernews/printer.go index bd54832..cd7e090 100644 --- a/internal/hackernews/printer.go +++ b/internal/hackernews/printer.go @@ -11,6 +11,16 @@ import ( const SPACES_PER_INDENT = 5 +var max_width int + +func Format(width int, op *Op, comments *[]Comment) { + max_width = width + fmt.Println(op) + for _, comment := range *comments { + fmt.Println(comment) + } +} + func printChilds(c []*Comment) { for _, value := range c { fmt.Println(value) @@ -20,7 +30,9 @@ func printChilds(c []*Comment) { func (o *Op) String() (ret string) { ret += "title: " + o.title + "\n" - ret += "url: " + o.url + "\n" + if o.url != "" { + ret += "url: " + o.url + "\n" + } ret += "self: " + o.selfUrl + "\n" ret += fmt.Sprintf("%s(%d) - %s\n", o.user, o.score, humanize.Time(o.date)) return @@ -35,7 +47,7 @@ func (c *Comment) String() (ret string) { if err != nil { panic(err) } - wrapped, _ := text.WrapLeftPadded(msg, 120, indent+1) + wrapped, _ := text.WrapLeftPadded(msg, max_width, indent+1) ret += "\n" + wrapped + "\n" arrow := ">>> " if c.indent > 0 {