-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpager.go
60 lines (54 loc) · 1.04 KB
/
pager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package herd
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
)
type pager struct {
process *exec.Cmd
stdin io.WriteCloser
}
func (p *pager) start() error {
if p == nil || p.process != nil {
return nil
}
pager, ok := os.LookupEnv("PAGER")
if !ok {
pager = "less"
}
args := []string{}
if strings.HasSuffix(pager, "less") {
args = append(args, "-R")
}
cmd := exec.Command(pager, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fd, err := cmd.StdinPipe()
if err != nil {
return err
}
p.process = cmd
p.stdin = fd
return cmd.Start()
}
func (p *pager) WriteString(msg string) (int, error) {
if p.stdin == nil {
return 0, fmt.Errorf("trying to write to a process that hasn't started")
}
return p.stdin.Write([]byte(msg))
}
func (p *pager) Write(msg []byte) (int, error) {
if p.stdin == nil {
return 0, fmt.Errorf("trying to write to a process that hasn't started")
}
return p.stdin.Write(msg)
}
func (p *pager) Wait() error {
if p == nil || p.process == nil {
return nil
}
p.stdin.Close()
return p.process.Wait()
}