Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): implement paginated output
Implement a `pager` config option. If configured, `cheat` will automatically pipe output through the configured pager (where appropriate).
- Loading branch information
1 parent
8e602b0
commit 59d5c96
Showing
9 changed files
with
78 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
package display | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/cheat/cheat/internal/config" | ||
) | ||
|
||
// Display writes output either directly to stdout, or through a pager, | ||
// depending upon configuration. | ||
func Display(out string, conf config.Config) { | ||
// if no pager was configured, print the output to stdout and exit | ||
if conf.Pager == "" { | ||
fmt.Print(out) | ||
os.Exit(0) | ||
} | ||
|
||
// otherwise, pipe output through the pager | ||
parts := strings.Split(conf.Pager, " ") | ||
pager := parts[0] | ||
args := parts[1:] | ||
|
||
// run the pager | ||
cmd := exec.Command(pager, args...) | ||
cmd.Stdin = strings.NewReader(out) | ||
cmd.Stdout = os.Stdout | ||
|
||
// handle errors | ||
err := cmd.Run() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to write to pager: %v", err)) | ||
os.Exit(1) | ||
} | ||
} |