Skip to content

Commit

Permalink
[pager] Improve pager search
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 21, 2023
1 parent 8f0db59 commit 8c5353c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
## Changelog

### 12.88.1

* `[pager]` Improved pager search
* `[fmtutil]` Code refactoring
* `[fmtutil/table]` Code refactoring
* `[pager]` Fixed build tags
* `[terminal/tty]` Fixed build tags

### 12.88.0

* `[pager]` Added new package for pager (`less`/`more`) setup
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.88.0"
const VERSION = "12.88.1"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
54 changes: 47 additions & 7 deletions pager/pager_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// DEFAULT is default pager command
const DEFAULT = "more"

// ////////////////////////////////////////////////////////////////////////////////// //

var pagerCmd *exec.Cmd
var pagerOut *os.File

Expand All @@ -33,7 +28,15 @@ var stderr *os.File

// ////////////////////////////////////////////////////////////////////////////////// //

var ErrAlreadySet = errors.New("Pager already set")
var binLess = "less"
var binMore = "more"

// ////////////////////////////////////////////////////////////////////////////////// //

var (
ErrAlreadySet = errors.New("Pager already set")
ErrNoPager = errors.New("There is no pager on the system")
)

// ////////////////////////////////////////////////////////////////////////////////// //

Expand All @@ -46,6 +49,10 @@ func Setup(pager string) error {

pagerCmd = getPagerCommand(pager)

if pagerCmd == nil {
return ErrNoPager
}

pagerCmd.Stdout, stdout = os.Stdout, os.Stdout
pagerCmd.Stderr, stderr = os.Stderr, os.Stderr

Expand Down Expand Up @@ -86,7 +93,11 @@ func getPagerCommand(pager string) *exec.Cmd {
}

if pager == "" {
pager = DEFAULT
pager = findPager()
}

if pager == "" {
return nil
}

if strings.Contains(pager, " ") {
Expand All @@ -96,3 +107,32 @@ func getPagerCommand(pager string) *exec.Cmd {

return exec.Command(pager)
}

// findPager tries to find pager an it options
func findPager() string {
_, err := exec.LookPath(binMore)

if err == nil {
moreOpts := os.Getenv("MORE")

if moreOpts != "" {
return "more " + moreOpts
}

return "more"
}

_, err = exec.LookPath(binLess)

if err == nil {
lessOpts := os.Getenv("LESS")

if lessOpts != "" {
return "less " + lessOpts
}

return "less -R"
}

return ""
}
32 changes: 29 additions & 3 deletions pager/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,50 @@ func (s *PagerSuite) TearDownSuite(c *C) {

func (s *PagerSuite) TestPager(c *C) {
c.Assert(Setup("cat"), IsNil)
c.Assert(Setup("cat"), NotNil)
c.Assert(Setup("cat"), DeepEquals, ErrAlreadySet)

Complete()

c.Assert(pagerCmd, IsNil)
c.Assert(pagerOut, IsNil)

os.Setenv("PAGER", "")

binMore = "_unknown_"
binLess = "_unknown_"

c.Assert(Setup(""), DeepEquals, ErrNoPager)
}

func (s *PagerSuite) TestPagerSearch(c *C) {
os.Setenv("PAGER", "")
os.Setenv("LESS", "")
os.Setenv("MORE", "")

cmd := getPagerCommand("cat")
c.Assert(cmd.Args, DeepEquals, []string{"cat"})

binMore = "echo"
binLess = "echo"

cmd = getPagerCommand("")
c.Assert(cmd.Args, DeepEquals, []string{"more"})

os.Setenv("PAGER", "less -MQR")
os.Setenv("MORE", "-l -s")

cmd = getPagerCommand("")
c.Assert(cmd.Args, DeepEquals, []string{"less", "-MQR"})
c.Assert(cmd.Args, DeepEquals, []string{"more", "-l", "-s"})

binMore = "_unknown_"

cmd = getPagerCommand("")
c.Assert(cmd.Args, DeepEquals, []string{"less", "-R"})

os.Setenv("LESS", "-MRQ")

cmd = getPagerCommand("")
c.Assert(cmd.Args, DeepEquals, []string{"less", "-MRQ"})

binMore = "more"
binLess = "less"
}

0 comments on commit 8c5353c

Please sign in to comment.