From a199ed1396ce7cbccae851f28fd575f40d9bd2a1 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Wed, 30 Apr 2025 11:45:45 +0200 Subject: [PATCH] Add separate UsePty flag for CmdObj This decouples StreamOutput from whether a PTY is used. In most cases we just want to see the output in the log window, but don't have to use a PTY, e.g. for the bisect commands. This has the implication that custom commands that are using "stream: true" no longer use a PTY. In most cases that's probably a good thing, but we're going to add a separate pty config for those who really wanted this. --- pkg/commands/oscommands/cmd_obj.go | 17 +++++++++++++++++ pkg/commands/oscommands/cmd_obj_runner.go | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pkg/commands/oscommands/cmd_obj.go b/pkg/commands/oscommands/cmd_obj.go index 776a5fbe7..184afa789 100644 --- a/pkg/commands/oscommands/cmd_obj.go +++ b/pkg/commands/oscommands/cmd_obj.go @@ -22,6 +22,9 @@ type CmdObj struct { // see StreamOutput() streamOutput bool + // see UsePty() + usePty bool + // see IgnoreEmptyError() ignoreEmptyError bool @@ -125,6 +128,19 @@ func (self *CmdObj) ShouldStreamOutput() bool { return self.streamOutput } +// when you call this, then call Run(), we'll use a PTY to run the command. Only +// has an effect if StreamOutput() was also called. Ignored on Windows. +func (self *CmdObj) UsePty() *CmdObj { + self.usePty = true + + return self +} + +// returns true if UsePty() was called +func (self *CmdObj) ShouldUsePty() bool { + return self.usePty +} + // if you call this before ShouldStreamOutput we'll consider an error with no // stderr content as a non-error. Not yet supported for Run or RunWithOutput ( // but adding support is trivial) @@ -172,6 +188,7 @@ func (self *CmdObj) RunAndProcessLines(onLine func(line string) (bool, error)) e func (self *CmdObj) PromptOnCredentialRequest(task gocui.Task) *CmdObj { self.credentialStrategy = PROMPT + self.usePty = true self.task = task return self diff --git a/pkg/commands/oscommands/cmd_obj_runner.go b/pkg/commands/oscommands/cmd_obj_runner.go index 01d07a7ee..6acd8d0fb 100644 --- a/pkg/commands/oscommands/cmd_obj_runner.go +++ b/pkg/commands/oscommands/cmd_obj_runner.go @@ -238,7 +238,13 @@ func (self *cmdObjRunner) runAndStreamAux( var stderr bytes.Buffer cmd.Stderr = io.MultiWriter(cmdWriter, &stderr) - handler, err := self.getCmdHandlerPty(cmd) + var handler *cmdHandler + var err error + if cmdObj.ShouldUsePty() { + handler, err = self.getCmdHandlerPty(cmd) + } else { + handler, err = self.getCmdHandlerNonPty(cmd) + } if err != nil { return err }