2020-06-26 00:21:51 +02:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/cheat/cheat/internal/config"
|
|
|
|
)
|
|
|
|
|
2020-11-28 04:35:24 +01:00
|
|
|
// Write writes output either directly to stdout, or through a pager,
|
2020-06-26 00:21:51 +02:00
|
|
|
// depending upon configuration.
|
2020-11-28 04:35:24 +01:00
|
|
|
func Write(out string, conf config.Config) {
|
2020-06-26 00:21:51 +02:00
|
|
|
// if no pager was configured, print the output to stdout and exit
|
|
|
|
if conf.Pager == "" {
|
|
|
|
fmt.Print(out)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, pipe output through the pager
|
|
|
|
parts := strings.Split(conf.Pager, " ")
|
|
|
|
pager := parts[0]
|
|
|
|
args := parts[1:]
|
|
|
|
|
2022-08-05 12:38:08 +02:00
|
|
|
// configure the pager
|
2020-06-26 00:21:51 +02:00
|
|
|
cmd := exec.Command(pager, args...)
|
|
|
|
cmd.Stdin = strings.NewReader(out)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
2022-08-05 12:38:08 +02:00
|
|
|
// run the pager and handle errors
|
|
|
|
if err := cmd.Run(); err != nil {
|
2022-08-05 02:38:49 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "failed to write to pager: %v\n", err)
|
2020-06-26 00:21:51 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|