charmbracelet / wish

Make SSH apps, just like that! 💫
MIT License
3.53k stars 71 forks source link

Capture output for wish.Command #291

Open robinovitch61 opened 3 months ago

robinovitch61 commented 3 months ago

Describe the bug For tea.ExecProcess, I've done something like this to capture output back to the application:

type StdoutProxy struct {
    SavedOutput []byte
}

func (so *StdoutProxy) Write(p []byte) (n int, err error) {
    so.SavedOutput = append(so.SavedOutput, p...)
    return os.Stdout.Write(p)
}

c := exec.Command("bash")
stdoutProxy := &StdoutProxy{}
c.Stdout = stdoutProxy
tea.ExecProcess(c, func(err error) tea.Msg {
    return ExecCompleteMsg{Output: string(stdoutProxy.SavedOutput)}
}

Which has worked nicely. But when I do something similar with a wish.Command and tea.Exec, it doesn't work:

wc := wish.Command(session, "bash")
wc.SetStdout(stdoutProxy)
tea.Exec(wc, func(err error) tea.Msg {
    return ExecCompleteMsg{Output: string(stdoutProxy.SavedOutput)}
}

In this second case, SavedOutput is empty. Any tips here?

Setup Please complete the following information along with version numbers, if applicable.

Note: you might encounter rendering issues if your locale does not use UTF-8 encoding. Please check your locale (locale on POSIX systems) to see what encoding is being used by your system.

❯ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

Expected behavior SavedOutput is not empty in the second case.

caarlos0 commented 3 months ago

because of this: https://github.com/charmbracelet/wish/blob/939d28f53e503d8d6dc92d148cd00c7d90cd230e/cmd.go#L64-L71

in any case, setting the output to stdout would not work, it needs to be the ssh session.

don't quite remember why we don't allow to set the cmd out and etc

caarlos0 commented 3 months ago

ahh, just remembered why.

because it might run on a pty, in which case it'll be overridden by the slave IO anyway