mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-22 10:31:37 +01:00
bbb287e29e
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 <git@nroo.de> 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 <dev.lh@web.de>
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package print
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/charmbracelet/glamour"
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
)
|
|
|
|
// outputMarkdown prints markdown to stdout, formatted for terminals.
|
|
// 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(
|
|
styleOption,
|
|
glamour.WithBaseURL(baseURL),
|
|
glamour.WithPreservedNewLines(),
|
|
glamour.WithWordWrap(getWordWrap()),
|
|
)
|
|
if err != nil {
|
|
fmt.Printf(markdown)
|
|
return err
|
|
}
|
|
|
|
out, err := renderer.Render(markdown)
|
|
if err != nil {
|
|
fmt.Printf(markdown)
|
|
return err
|
|
}
|
|
fmt.Print(out)
|
|
return nil
|
|
}
|
|
|
|
// stolen from https://github.com/charmbracelet/glow/blob/e9d728c/main.go#L152-L165
|
|
func getWordWrap() int {
|
|
fd := int(os.Stdout.Fd())
|
|
width := 80
|
|
if terminal.IsTerminal(fd) {
|
|
if w, _, err := terminal.GetSize(fd); err == nil {
|
|
width = w
|
|
}
|
|
}
|
|
if width > 120 {
|
|
width = 120
|
|
}
|
|
return width
|
|
}
|