diff --git a/modules/print/issue.go b/modules/print/issue.go index dc7a908..35a41dd 100644 --- a/modules/print/issue.go +++ b/modules/print/issue.go @@ -8,24 +8,17 @@ import ( "fmt" "code.gitea.io/sdk/gitea" - "github.com/charmbracelet/glamour" ) // IssueDetails print an issue rendered to stdout func IssueDetails(issue *gitea.Issue) { - - in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", issue.Index, + OutputMarkdown(fmt.Sprintf( + "# #%d %s (%s)\n%s created %s\n\n%s\n", + issue.Index, issue.Title, issue.State, issue.Poster.UserName, issue.Created.Format("2006-01-02 15:04:05"), issue.Body, - ) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + )) } diff --git a/modules/print/login.go b/modules/print/login.go index 4801212..e217515 100644 --- a/modules/print/login.go +++ b/modules/print/login.go @@ -10,13 +10,10 @@ import ( "time" "code.gitea.io/tea/modules/config" - - "github.com/charmbracelet/glamour" ) // LoginDetails print login entry to stdout func LoginDetails(login *config.Login) { - in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n", login.Name, login.User, @@ -31,11 +28,5 @@ func LoginDetails(login *config.Login) { } in += fmt.Sprintf("\nCreated: %s", time.Unix(login.Created, 0).Format(time.RFC822)) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + OutputMarkdown(in) } diff --git a/modules/print/markdown.go b/modules/print/markdown.go new file mode 100644 index 0000000..6c5e323 --- /dev/null +++ b/modules/print/markdown.go @@ -0,0 +1,24 @@ +// 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" + + "github.com/charmbracelet/glamour" +) + +// 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) error { + out, err := glamour.Render(markdown, "auto") + if err != nil { + fmt.Printf(markdown) + return err + } + fmt.Print(out) + return nil +} diff --git a/modules/print/print.go b/modules/print/print.go index 21b4b87..49f6046 100644 --- a/modules/print/print.go +++ b/modules/print/print.go @@ -6,17 +6,8 @@ package print import ( "fmt" - - "github.com/muesli/termenv" ) -func getGlamourTheme() string { - if termenv.HasDarkBackground() { - return "dark" - } - return "light" -} - // formatSize get kb in int and return string func formatSize(kb int64) string { if kb < 1024 { diff --git a/modules/print/pull.go b/modules/print/pull.go index 97c4cc8..f92cc11 100644 --- a/modules/print/pull.go +++ b/modules/print/pull.go @@ -8,24 +8,17 @@ import ( "fmt" "code.gitea.io/sdk/gitea" - "github.com/charmbracelet/glamour" ) // PullDetails print an pull rendered to stdout func PullDetails(pr *gitea.PullRequest) { - - in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", pr.Index, + OutputMarkdown(fmt.Sprintf( + "# #%d %s (%s)\n%s created %s\n\n%s\n", + pr.Index, pr.Title, pr.State, pr.Poster.UserName, pr.Created.Format("2006-01-02 15:04:05"), pr.Body, - ) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + )) }