Skip to content

Commit

Permalink
pipe to less
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Nov 24, 2024
1 parent ea46057 commit 54867b6
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions jqx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/fs"
"os"
"os/exec"

"github.com/myaaaaaaaaa/go-jqx"
)
Expand All @@ -22,6 +23,12 @@ func try(err error) {
os.Exit(5)
}
}
func must[T any](val T, err error) T {
if err != nil {
panic(err)
}
return val
}

func main() {
prog := jqx.Program{
Expand All @@ -36,6 +43,18 @@ func main() {
StdoutIsTerminal: isTerminal(os.Stdout),
}

if prog.StdoutIsTerminal {
cmd := exec.Command("less", "-S")
pipe := must(cmd.StdinPipe())
cmd.Stdout = os.Stdout
prog.Stdout = pipe

try(cmd.Start())

defer cmd.Wait()
defer pipe.Close()
}

try(prog.Main())
try(os.CopyFS(".", prog.FS))
}

0 comments on commit 54867b6

Please sign in to comment.