1package display 2 3import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "strings" 8 9 "github.com/cheat/cheat/internal/config" 10) 11 12// Write writes output either directly to stdout, or through a pager, 13// depending upon configuration. 14func Write(out string, conf config.Config) { 15 // if no pager was configured, print the output to stdout and exit 16 if conf.Pager == "" { 17 fmt.Print(out) 18 os.Exit(0) 19 } 20 21 // otherwise, pipe output through the pager 22 parts := strings.Split(conf.Pager, " ") 23 pager := parts[0] 24 args := parts[1:] 25 26 // run the pager 27 cmd := exec.Command(pager, args...) 28 cmd.Stdin = strings.NewReader(out) 29 cmd.Stdout = os.Stdout 30 31 // handle errors 32 err := cmd.Run() 33 if err != nil { 34 fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to write to pager: %v", err)) 35 os.Exit(1) 36 } 37} 38