Skip to content

Commit

Permalink
Change to Do not display screen when redirecting
Browse files Browse the repository at this point in the history
Changes the default to not display the screen when redirecting.
Requires the `--force-screen` option to display the screen.

This is a fix for #634.
  • Loading branch information
noborus committed Oct 15, 2024
1 parent 12bb17f commit c25d495
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -46,6 +47,9 @@ var (
// execCommand targets the output of executing the command.
execCommand bool

// forceScreen is display screen even when redirecting output.
forceScreen bool

// alignF is align column.
alignF bool
// rawF is raw output of escape sequences.
Expand All @@ -57,6 +61,8 @@ var (
ErrCompletion = errors.New("requires one of the arguments bash/zsh/fish/powershell")
// ErrNoArgument indicating that there are no arguments to execute.
ErrNoArgument = errors.New("no arguments to execute")
// ErrMissingFile indicates that the file is missing.
ErrMissingFile = errors.New("missing file")
)

// rootCmd represents the base command when called without any subcommands.
Expand Down Expand Up @@ -117,6 +123,10 @@ It supports various compressed files(gzip, bzip2, zstd, lz4, and xz).
oviewer.MemoryLimit = config.MemoryLimit
oviewer.MemoryLimitFile = config.MemoryLimitFile
SetRedirect()
// Do not display the screen if redirected (unless forceScreen is specified).
if oviewer.STDOUTPIPE != nil && !forceScreen {
return copyOutput(args)
}

if execCommand {
return ExecCommand(args)
Expand Down Expand Up @@ -335,6 +345,35 @@ func flagUsage(f *pflag.FlagSet) string {
return buf.String()
}

func openFile(fileName string) (*os.File, error) {
if fileName == "" {
fd := os.Stdin.Fd()
if term.IsTerminal(int(fd)) {
return nil, ErrMissingFile
}
return os.Stdin, nil
}
return os.Open(fileName)
}

// copyOutput just outputs the input (standard input if there is no first file).
func copyOutput(args []string) error {
files := argsToFiles(args)
fileName := ""
if len(files) > 0 {
fileName = files[0]
}
file, err := openFile(fileName)
if err != nil {
return err
}
defer file.Close()

r := io.Reader(file)
_, err = io.Copy(oviewer.STDOUTPIPE, r)
return err
}

// UsageFunc returns a function that returns the usage.
// This is for overwriting cobra usage.
func UsageFunc() (cmd func(*cobra.Command) error) {
Expand All @@ -360,6 +399,8 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&helpKey, "help-key", "", false, "display key bind information")
rootCmd.PersistentFlags().BoolVarP(&execCommand, "exec", "e", false, "command execution result instead of file")

rootCmd.PersistentFlags().BoolVarP(&forceScreen, "force-screen", "", false, "display screen even when redirecting output")

rootCmd.PersistentFlags().StringVarP(&completion, "completion", "", "", "generate completion script [bash|zsh|fish|powershell]")
_ = rootCmd.RegisterFlagCompletionFunc("completion", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return []string{"bash", "zsh", "fish", "powershell"}, cobra.ShellCompDirectiveNoFileComp
Expand Down

0 comments on commit c25d495

Please sign in to comment.