From bbb287e29e34a0d5d73084913f8e96bd0613cd3e Mon Sep 17 00:00:00 2001 From: Norwin Date: Wed, 14 Sep 2022 02:35:15 +0800 Subject: [PATCH] markdown: dont emit ansi sequences when not emitting to tty (#491) Allows generating a plain text version of an issue (i.e. without colors and other terminal formatting) when storing stdout in a file. ``` tea issue --comments 1 > issue1.txt ``` `IsInteractive()` had to be moved to avoid a recursive import chain. --- In the future, it would be nice to also respect the `--output` flag. This flag is currently designed for tabular output, but we could add more supported values like `markdown` `ansi`, `plain` to it Co-authored-by: Norwin Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/491 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: delvh --- modules/interact/comments.go | 7 +------ modules/print/formatters.go | 7 +++++++ modules/print/markdown.go | 9 ++++++++- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/interact/comments.go b/modules/interact/comments.go index a1adcef..194335d 100644 --- a/modules/interact/comments.go +++ b/modules/interact/comments.go @@ -27,7 +27,7 @@ func ShowCommentsMaybeInteractive(ctx *context.TeaContext, idx int64, totalComme return err } print.Comments(comments) - } else if IsInteractive() && !ctx.IsSet("comments") { + } else if print.IsInteractive() && !ctx.IsSet("comments") { // if we're interactive, but --comments hasn't been explicitly set to false if err := ShowCommentsPaginated(ctx, idx, totalComments); err != nil { fmt.Printf("error while loading comments: %v\n", err) @@ -70,11 +70,6 @@ func ShowCommentsPaginated(ctx *context.TeaContext, idx int64, totalComments int return nil } -// IsInteractive checks if the output is piped, but NOT if the session is run interactively.. -func IsInteractive() bool { - return terminal.IsTerminal(int(os.Stdout.Fd())) -} - // IsStdinPiped checks if stdin is piped func IsStdinPiped() bool { return !terminal.IsTerminal(int(os.Stdin.Fd())) diff --git a/modules/print/formatters.go b/modules/print/formatters.go index d9f9797..ec53fa0 100644 --- a/modules/print/formatters.go +++ b/modules/print/formatters.go @@ -6,13 +6,20 @@ package print import ( "fmt" + "os" "regexp" "time" "code.gitea.io/sdk/gitea" "github.com/muesli/termenv" + "golang.org/x/crypto/ssh/terminal" ) +// IsInteractive checks if the output is piped, but NOT if the session is run interactively.. +func IsInteractive() bool { + return terminal.IsTerminal(int(os.Stdout.Fd())) +} + // captures the repo URL part // of an url var repoURLRegex = regexp.MustCompile("^([[:alnum:]]+://[^/]+(?:/[[:alnum:]]+){2})/.*") diff --git a/modules/print/markdown.go b/modules/print/markdown.go index 44ee701..99318ab 100644 --- a/modules/print/markdown.go +++ b/modules/print/markdown.go @@ -16,8 +16,15 @@ import ( // If the input could not be parsed, it is printed unformatted, the error // is returned anyway. func outputMarkdown(markdown string, baseURL string) error { + var styleOption glamour.TermRendererOption + if IsInteractive() { + styleOption = glamour.WithAutoStyle() + } else { + styleOption = glamour.WithStandardStyle("notty") + } + renderer, err := glamour.NewTermRenderer( - glamour.WithAutoStyle(), + styleOption, glamour.WithBaseURL(baseURL), glamour.WithPreservedNewLines(), glamour.WithWordWrap(getWordWrap()),