Skip to content

Commit

Permalink
mr_show: fix line wrapping
Browse files Browse the repository at this point in the history
The glamour library has, by default, line wrapping activated at the mark of
80 columns, causing a differente rendering from what is seen in the WebUI.
Setting this option to `0` turns it off and prints messages as they were
exactly sent by the user, even if it was sent with `--force-linebreak`.

What is a bit confusing is that setting it to `0` make the lib to honor the
line breaks as they were sent. While setting it to a big number, e.g. 150,
won't honor the linebreaks.

This patch adds a common function for getting the terminal renderer with
line wrapping turned off and a variable style that can be passed as an
argument.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Mar 15, 2021
1 parent 8c8e502 commit f058ea9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
8 changes: 4 additions & 4 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ func printIssue(issue *gitlab.Issue, project string, renderMarkdown bool) {
}

if renderMarkdown {
r, _ := glamour.NewTermRenderer(
glamour.WithStandardStyle("auto"),
)

r, err := getTermRenderer(glamour.WithAutoStyle())
if err != nil {
log.Fatal(err)
}
issue.Description, _ = r.Render(issue.Description)
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func printMR(mr *gitlab.MergeRequest, project string, renderMarkdown bool) {
}

if renderMarkdown {
r, _ := glamour.NewTermRenderer(
glamour.WithStandardStyle("auto"),
)

r, err := getTermRenderer(glamour.WithAutoStyle())
if err != nil {
log.Fatal(err)
}
mr.Description, _ = r.Render(mr.Description)
}

Expand Down
14 changes: 10 additions & 4 deletions cmd/show_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ func getBoldStyle() ansi.StyleConfig {
return style
}

func getTermRenderer(style glamour.TermRendererOption) (*glamour.TermRenderer, error) {
r, err := glamour.NewTermRenderer(
glamour.WithWordWrap(0),
style,
)
return r, err
}

func printDiscussions(discussions []*gitlab.Discussion, since string, idstr string, idNum int, renderMarkdown bool) {
newAccessTime := time.Now().UTC()

Expand All @@ -179,10 +187,8 @@ func printDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
sinceIsSet = false
}

mdRendererNormal, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle())
mdRendererBold, _ := glamour.NewTermRenderer(
glamour.WithStyles(getBoldStyle()))
mdRendererNormal, _ := getTermRenderer(glamour.WithAutoStyle())
mdRendererBold, _ := getTermRenderer(glamour.WithStyles(getBoldStyle()))

// for available fields, see
// https://godoc.org/github.com/xanzy/go-gitlab#Note
Expand Down

0 comments on commit f058ea9

Please sign in to comment.