Skip to content

Commit

Permalink
Merge pull request #640 from noborus/subshell-stdin
Browse files Browse the repository at this point in the history
STDIN switching when using Subshell
  • Loading branch information
noborus authored Oct 26, 2024
2 parents 70b2062 + 7109312 commit ab0e275
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions oviewer/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"strings"
"sync/atomic"
"time"

"golang.org/x/term"
)

// toggleWrapMode toggles wrapMode each time it is called.
Expand Down Expand Up @@ -401,14 +403,22 @@ func (root *Root) suspend(context.Context) {

func (root *Root) subShell(shell string) error {
if shell == "" {
if runtime.GOOS == "windows" {
shell = "CMD.EXE"
} else {
shell = "/bin/sh"
shell = getShell()
}

stdin := os.Stdin
if !term.IsTerminal(int(os.Stdin.Fd())) {
// Use TTY as stdin when the current stdin is not a terminal.
tty, err := getTty()
if err != nil {
return fmt.Errorf("failed to open tty: %w", err)
}
defer tty.Close()
stdin = tty
}

c := exec.Command(shell, "-l")
c.Stdin = os.Stdin
c.Stdin = stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
Expand All @@ -418,6 +428,20 @@ func (root *Root) subShell(shell string) error {
return nil
}

func getShell() string {
if runtime.GOOS == "windows" {
return "CMD.EXE"
}
return "/bin/sh"
}

func getTty() (*os.File, error) {
if runtime.GOOS == "windows" {
return os.Open("CONIN$")
}
return os.Open("/dev/tty")
}

// setViewMode switches to the preset display mode.
// Set header lines and columnMode together.
func (root *Root) setViewMode(ctx context.Context, modeName string) {
Expand Down

0 comments on commit ab0e275

Please sign in to comment.