Use pager to display results

This commit is contained in:
Jan Philipp Timme 2018-12-30 14:51:08 +01:00
parent 6619bd86ce
commit f48e4c9f60
1 changed files with 23 additions and 1 deletions

24
main.go
View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"strconv"
@ -22,6 +23,7 @@ func main() {
if settings["interactive"] == "true" {
interactiveShell()
}
fmt.Printf("I am done.\n")
}
func parseArguments() {
@ -50,8 +52,28 @@ func showQueue() {
fmt.Printf("Error reading: %s\n", err.Error())
os.Exit(1)
}
reader, writer := io.Pipe()
pagerDone := make(chan bool)
go launchPager(reader, pagerDone)
for _, entry := range entries {
fmt.Printf("%s\n", entry.String())
fmt.Fprintf(writer, "%s\n", entry.String())
}
writer.Close()
// Wait for pager to be closed before continuing the main program
<-pagerDone
}
func launchPager(reader *io.PipeReader, pagerDone chan<- bool) {
cmd := exec.Command("less")
cmd.Stdin = reader
cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
fmt.Printf("Error launching pager: %s\n", err.Error())
}
if err := cmd.Wait(); err != nil {
fmt.Printf("Error terminating pager: %s\n", err.Error())
} else {
pagerDone <- true
}
}