Add pager support
All checks were successful
/ build (push) Successful in 31s

This commit is contained in:
Sebastiaan de Schaetzen 2024-10-18 16:14:48 +02:00
parent d71e38f5db
commit d5812297b2
2 changed files with 37 additions and 3 deletions

View File

@ -311,7 +311,11 @@ func ActionViewDescription(_ []string) {
printError("Failed to get issue", err)
return
}
println(issue.Fields.Description)
err = ShowPaged(issue.Fields.Description)
if err != nil {
printError("Failed to show description", err)
return
}
}
func ActionViewComments(_ []string) {
@ -327,12 +331,18 @@ func ActionViewComments(_ []string) {
if len(issue.Fields.Comments.Comments) == 0 {
println("This issue has no comments")
}
content := ""
for i, comment := range issue.Fields.Comments.Comments {
if i != 0 {
println()
}
fmt.Printf("%s at %s:\n", red(comment.Author.DisplayName), blue(comment.Created))
fmt.Printf(" %s\n", comment.Body)
content += fmt.Sprintf("%s at %s:\n", red(comment.Author.DisplayName), blue(comment.Created))
content += fmt.Sprintf(" %s\n", comment.Body)
}
err = ShowPaged(content)
if err != nil {
printError("Failed to show comments", err)
return
}
}

24
pager.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"os"
"os/exec"
"strings"
)
func GetPager() (string, []string) {
pager, hasPager := os.LookupEnv("PAGER")
if hasPager {
return pager, nil
}
return "less", []string{"--RAW-CONTROL-CHARS"}
}
func ShowPaged(content string) error {
pager, args := GetPager()
cmd := exec.Command(pager, args...)
cmd.Stdin = strings.NewReader(content)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}