Package list golang-github-sean--pager / db0cfd4
New upstream version 0.0~git20180208.666be9b Thorsten Alteholz 1 year, 4 months ago
4 changed file(s) with 208 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # Binaries for programs and plugins
1 *.exe
2 *.dll
3 *.so
4 *.dylib
5
6 # Test binary, build with `go test -c`
7 *.test
8
9 # Output of the go coverage tool, specifically when used with LiteIDE
10 *.out
11
12 # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
13 .glide/
0 BSD 2-Clause License
1
2 Copyright (c) 2018, Sean Chittenden
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0 # `pager`
1
2 Easy `$PAGER` support for Go (i.e. `less(1)`, `more(1)`) with sane defaults.
3
4 By default `pager` uses `less(1)` with the args: `-X -F -R --buffers=65535` and
5 sets `LESSSECURE=1` before starting `less(1)`.
6
7 If `less(1)` is unavailable, `pager` falls back to `more(1)`.
8
9 The `PAGER` environment variable is honored.
10
11 ## Example Usage
12
13 ```go
14 import (
15 "fmt"
16
17 "github.com/sean-/pager"
18 )
19
20 func main() {
21 p, err := pager.New()
22 if err != nil {
23 panic(fmt.Sprintf("unable to get pager: %v", err))
24 }
25 defer p.Wait()
26
27 foo(p)
28 }
29
30 func foo(w io.Writer) {
31 fmt.Fprintf(w, "header\n")
32 for i := 0; i < 1000; i++ {
33 fmt.Fprintf(w, "line %03d\n", i)
34 }
35 fmt.Fprintf(w, "trailer\n")
36 }
37 ```
38
39 # Credit
40
41 Much of this was pulled from
42 https://gist.github.com/dchapes/1d0c538ce07902b76c75 and reworked slightly.
0 // The Pager package allows the program to easily pipe it's
1 // standard output through a Pager program
2 // (like how the man command does).
3 //
4 // Borrowed from: https://gist.github.com/dchapes/1d0c538ce07902b76c75 and
5 // reworked slightly.
6
7 package pager
8
9 import (
10 "errors"
11 "io"
12 "os"
13 "os/exec"
14 "path"
15 "strings"
16 )
17
18 type Pager struct {
19 cmd *exec.Cmd
20 file io.WriteCloser
21 }
22
23 var pager Pager
24
25 // The environment variables to check for the name of (and arguments to)
26 // the Pager to run.
27 var PagerEnvVariables = []string{"PAGER"}
28
29 // The command names in $PATH to look for if none of the environment
30 // variables are set.
31 // Cannot include arguments.
32 var PagerCommands = []string{"less", "more"}
33
34 func pagerExecPath() (pagerPath string, args []string, err error) {
35 for _, testVar := range PagerEnvVariables {
36 pagerPath = os.Getenv(testVar)
37 if pagerPath != "" {
38 args = strings.Fields(pagerPath)
39 if len(args) > 1 {
40 return args[0], args[1:], nil
41 }
42 }
43 }
44
45 // This default only gets used if PagerCommands is empty.
46 err = exec.ErrNotFound
47 for _, testPath := range PagerCommands {
48 pagerPath, err = exec.LookPath(testPath)
49 if err == nil {
50 switch {
51 case path.Base(pagerPath) == "less":
52 // TODO(seanc@): Make the buffer size conditional
53 args := []string{"-X", "-F", "-R", "--buffers=65535"}
54 return pagerPath, args, nil
55 default:
56 return pagerPath, nil, nil
57 }
58 }
59 }
60 return "", nil, err
61 }
62
63 // New returns a new io.WriteCloser connected to a Pager.
64 // The returned WriteCloser can be used as a replacement to os.Stdout,
65 // everything written to it is piped to a Pager.
66 // To determine what Pager to run, the environment variables listed
67 // in PagerEnvVariables are checked.
68 // If all are empty/unset then the commands listed in PagerCommands
69 // are looked for in $PATH.
70 func New() (*Pager, error) {
71 p := &Pager{}
72 if p.cmd != nil {
73 return nil, errors.New("Pager: already exists")
74 }
75 pagerPath, args, err := pagerExecPath()
76 if err != nil {
77 return nil, err
78 }
79
80 // If the Pager is less(1), set some useful options
81 switch {
82 case path.Base(pagerPath) == "less":
83 os.Setenv("LESSSECURE", "1")
84 }
85
86 p.cmd = exec.Command(pagerPath, args...)
87 p.cmd.Stdout = os.Stdout
88 p.cmd.Stderr = os.Stderr
89 w, err := p.cmd.StdinPipe()
90 if err != nil {
91 return nil, err
92 }
93 f, ok := w.(io.WriteCloser)
94 if !ok {
95 return nil, errors.New("Pager: exec.Command.StdinPipe did not return type io.WriteCloser")
96 }
97 p.file = f
98 err = p.cmd.Start()
99 if err != nil {
100 return nil, err
101 }
102
103 return p, nil
104 }
105
106 // Wait closes the pipe to the Pager setup with New() or Stdout() and waits
107 // for it to exit.
108 //
109 // This should normally be called before the program exists,
110 // typically via a defer call in main().
111 func (p *Pager) Wait() error {
112 if p.cmd == nil {
113 return nil
114 }
115 p.file.Close()
116 return p.cmd.Wait()
117 }
118
119 func (p *Pager) Close() error {
120 return nil
121 }
122
123 func (p *Pager) Write(data []byte) (n int, err error) {
124 return p.file.Write(data)
125 }