summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorChristian Muehlhaeuser <muesli@gmail.com>2019-12-31 11:13:06 +0100
committerChristian Muehlhaeuser <muesli@gmail.com>2019-12-31 11:13:06 +0100
commite7c92ddfa77bc2fdd1a7143ee07c65b898c22b82 (patch)
tree66d78f468486ccb2c8cea927b9a1637ebba29d5a /main.go
parent1d4f5c84808b9e02fae85b58b0743f065d7fbca1 (diff)
Add -p flag to render output in default pager
Starts the user's default $PAGER or defaults to 'less -r'.
Diffstat (limited to 'main.go')
-rw-r--r--main.go25
1 files changed, 23 insertions, 2 deletions
diff --git a/main.go b/main.go
index 1470d11..1108428 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"os"
+ "os/exec"
"path/filepath"
"strings"
@@ -22,6 +23,7 @@ var (
CommitSHA = ""
readmeNames = []string{"README.md", "README"}
+ pager bool
style string
width uint
@@ -122,6 +124,7 @@ func execute(cmd *cobra.Command, args []string) error {
style = "notty"
}
+ // render
var baseURL string
u, err := url.ParseRequestURI(src.URL)
if err == nil {
@@ -143,16 +146,33 @@ func execute(cmd *cobra.Command, args []string) error {
return err
}
+ // trim lines
lines := strings.Split(string(out), "\n")
+ var content string
for i, s := range lines {
- fmt.Print(strings.TrimSpace(s))
+ content += strings.TrimSpace(s)
// don't add an artificial newline after the last split
if i+1 < len(lines) {
- fmt.Println()
+ content += "\n"
}
}
+ // display
+ if cmd.Flags().Changed("pager") {
+ pager := os.Getenv("PAGER")
+ if pager == "" {
+ pager = "less -r"
+ }
+
+ pa := strings.Split(pager, " ")
+ c := exec.Command(pa[0], pa[1:]...)
+ c.Stdin = strings.NewReader(content)
+ c.Stdout = os.Stdout
+ return c.Run()
+ }
+
+ fmt.Print(content)
return nil
}
@@ -172,6 +192,7 @@ func init() {
}
rootCmd.Version = Version
+ rootCmd.Flags().BoolVarP(&pager, "pager", "p", false, "display with pager")
rootCmd.Flags().StringVarP(&style, "style", "s", "dark", "style name or JSON path")
rootCmd.Flags().UintVarP(&width, "width", "w", 100, "word-wrap at width")
}