markdown: dont emit ansi sequences when not emitting to tty

This commit is contained in:
Norwin 2022-07-30 15:16:59 +02:00
parent f83f579dea
commit 9a084d21ab
3 changed files with 16 additions and 7 deletions

View file

@ -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()))

View file

@ -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 <host>/<owner>/<repo> of an url
var repoURLRegex = regexp.MustCompile("^([[:alnum:]]+://[^/]+(?:/[[:alnum:]]+){2})/.*")

View file

@ -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.WithWordWrap(getWordWrap()),
)