-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #405 from essentialkaos/develop
Version 12.88.0
- Loading branch information
Showing
17 changed files
with
407 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pager | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func ExampleSetup() { | ||
// Use pager from PAGER env var or default (more) | ||
Setup("") | ||
|
||
// Or provide specific command. | ||
Setup("less -MQR") | ||
|
||
// Complete must be called at the end of the program work. You can call it with defer | ||
// in your main function. | ||
defer Complete() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Package pager provides methods for pager setup (more/less) | ||
package pager | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// DEFAULT is default pager command | ||
const DEFAULT = "more" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var pagerCmd *exec.Cmd | ||
var pagerOut *os.File | ||
|
||
var stdout *os.File | ||
var stderr *os.File | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var ErrAlreadySet = errors.New("Pager already set") | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// Setup set up pager for work. After calling this method, any data sent to Stdout and | ||
// Stderr (using fmt, fmtc, or terminal packages) will go to the pager. | ||
func Setup(pager string) error { | ||
if pagerCmd != nil { | ||
return ErrAlreadySet | ||
} | ||
|
||
pagerCmd = getPagerCommand(pager) | ||
|
||
pagerCmd.Stdout, stdout = os.Stdout, os.Stdout | ||
pagerCmd.Stderr, stderr = os.Stderr, os.Stderr | ||
|
||
w, err := pagerCmd.StdinPipe() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
pagerOut = w.(*os.File) | ||
os.Stdout = pagerOut | ||
|
||
return pagerCmd.Start() | ||
} | ||
|
||
// Complete finishes pager work | ||
func Complete() { | ||
if pagerOut != nil { | ||
pagerOut.Close() | ||
pagerOut = nil | ||
} | ||
|
||
if pagerCmd != nil { | ||
pagerCmd.Wait() | ||
pagerCmd = nil | ||
} | ||
|
||
os.Stdout = stdout | ||
os.Stderr = stderr | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// getPagerCommand creates command for pager | ||
func getPagerCommand(pager string) *exec.Cmd { | ||
if pager == "" { | ||
pager = os.Getenv("PAGER") | ||
} | ||
|
||
if pager == "" { | ||
pager = DEFAULT | ||
} | ||
|
||
if strings.Contains(pager, " ") { | ||
cmdSlice := strings.Fields(pager) | ||
return exec.Command(cmdSlice[0], cmdSlice[1:]...) | ||
} | ||
|
||
return exec.Command(pager) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package pager | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/essentialkaos/check" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
type PagerSuite struct{} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var _ = Suite(&PagerSuite{}) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func (s *PagerSuite) TearDownSuite(c *C) { | ||
Complete() | ||
} | ||
|
||
func (s *PagerSuite) TestPager(c *C) { | ||
c.Assert(Setup("cat"), IsNil) | ||
c.Assert(Setup("cat"), NotNil) | ||
|
||
Complete() | ||
|
||
c.Assert(pagerCmd, IsNil) | ||
c.Assert(pagerOut, IsNil) | ||
} | ||
|
||
func (s *PagerSuite) TestPagerSearch(c *C) { | ||
os.Setenv("PAGER", "") | ||
|
||
cmd := getPagerCommand("cat") | ||
c.Assert(cmd.Args, DeepEquals, []string{"cat"}) | ||
|
||
cmd = getPagerCommand("") | ||
c.Assert(cmd.Args, DeepEquals, []string{"more"}) | ||
|
||
os.Setenv("PAGER", "less -MQR") | ||
cmd = getPagerCommand("") | ||
c.Assert(cmd.Args, DeepEquals, []string{"less", "-MQR"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Package pager provides methods for pager setup (more/less) | ||
package pager | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import "errors" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// DEFAULT is default pager command | ||
const DEFAULT = "more" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
var ErrAlreadySet = errors.New("Pager already set") | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// ❗ Setup set up pager for work. After calling this method, any data sent to Stdout and | ||
// Stderr (using fmt, fmtc, or terminal packages) will go to the pager. | ||
func Setup(pager string) error { | ||
return nil | ||
} | ||
|
||
// ❗ Complete finishes pager work | ||
func Complete() { | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package tty | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
type winsize struct { | ||
rows uint16 | ||
cols uint16 | ||
xpixels uint16 | ||
ypixels uint16 | ||
} | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// tty is a path to TTY device file | ||
var tty = "/dev/tty" | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
// GetSize returns window width and height | ||
func GetSize() (int, int) { | ||
t, err := os.OpenFile(tty, syscall.O_RDONLY, 0) | ||
|
||
if err != nil { | ||
return -1, -1 | ||
} | ||
|
||
var sz winsize | ||
|
||
_, _, _ = syscall.Syscall( | ||
syscall.SYS_IOCTL, t.Fd(), | ||
uintptr(syscall.TIOCGWINSZ), | ||
uintptr(unsafe.Pointer(&sz)), | ||
) | ||
|
||
return int(sz.cols), int(sz.rows) | ||
} | ||
|
||
// GetWidth returns window width | ||
func GetWidth() int { | ||
w, _ := GetSize() | ||
return w | ||
} | ||
|
||
// GetHeight returns window height | ||
func GetHeight() int { | ||
_, h := GetSize() | ||
return h | ||
} |
Oops, something went wrong.