Allow user to override markdown wrap width via $GH_MDWIDTH from environment

This commit is contained in:
Scott Mcdermott 2024-09-15 22:37:43 -07:00
parent d645fd4f00
commit 4aff4ebe12
2 changed files with 15 additions and 3 deletions

View file

@ -99,6 +99,10 @@ var HelpTopics = []helpTopic{
%[1]sGH_PATH%[1]s: set the path to the gh executable, useful for when gh can not properly determine
its own path such as in the cygwin terminal.
%[1]sGH_MDWIDTH%[1]s: default maximum width for markdown render wrapping. The max width of lines
wrapped on the terminal will be taken as the lesser of the terminal width, this value, or 120 if
not specified. This value is used, for example, with %[1]spr view%[1]s subcommand.
`, "`"),
},
{

View file

@ -1,6 +1,9 @@
package markdown
import (
"os"
"strconv"
"github.com/charmbracelet/glamour"
ghMarkdown "github.com/cli/go-gh/v2/pkg/markdown"
)
@ -10,11 +13,16 @@ func WithoutIndentation() glamour.TermRendererOption {
}
// WithWrap is a rendering option that set the character limit for soft
// wrapping the markdown rendering. There is a max limit of 120 characters.
// wrapping the markdown rendering. There is a max limit of 120 characters,
// unless the user overrides with an environment variable.
// If 0 is passed then wrapping is disabled.
func WithWrap(w int) glamour.TermRendererOption {
if w > 120 {
w = 120
width, err := strconv.Atoi(os.Getenv("GH_MDWIDTH"))
if err != nil {
width = 120
}
if w > width {
w = width
}
return ghMarkdown.WithWrap(w)
}