Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate stderr and stdout #194

Closed
gitargowithit opened this issue May 1, 2024 · 3 comments
Closed

Separate stderr and stdout #194

gitargowithit opened this issue May 1, 2024 · 3 comments

Comments

@gitargowithit
Copy link

Hey all, I am using pty in my project to get some CLI tools to print with color.

Is it possible to get back 3 files here, one as it is now where stdout & stderr are combined, and one where it is just stdout and the other stderr? This would really help with some string/file manipulation I want to do afterwards. Currently this is the only way I found to separate out stdout and stderr:

func RunCommand(command string) (stdout, stderr string, err error) {
	cmd := exec.Command("bash", "-c", command)

	var stderrBuf bytes.Buffer
	var stdoutBuf bytes.Buffer

	cmd.Stderr = &stderrBuf // this line redirects the stderr to the buffer

	ptmx, err := pty.StartWithSize(cmd, &pty.Winsize{Cols: 80, Rows: 24})
	if err != nil {
		return "", "", err
	}
	defer func() { _ = ptmx.Close() }()

	go func() {
		_, _ = stdcopy(&stdoutBuf, ptmx)
	}()

	if err := cmd.Wait(); err != nil {
		return stdoutBuf.String(), stderrBuf.String(), err
	}

	return stdoutBuf.String(), stderrBuf.String(), nil
}

However this means that I can't keep the original output ordering, if for example a CLI tool prints to stdout then stderr then back to stdout this ordering is lost.

Thanks for the project, would appreciate some insight here!

@creack
Copy link
Owner

creack commented May 23, 2024

Trying to split stdout and stderr would result in ioctl error as it doesn't make sense for the driver.
Think of the terminal as your computer screen. You have only one of those, regardless if it is stdout or stderr, it gets printed on your one screen. Same thing for the terminal.

I believe you may not need a TTY though, displaying colors is something up to the terminal emulator, the tty only passes through the ascii escape sequences which are in turn interpreted by your terminal emulator to display a color.

Some color libraries use the presence of a TTY to automatically toggle colors on/off, but that can be manually changed.

@creack creack closed this as completed May 23, 2024
@gitargowithit
Copy link
Author

Thanks @creack. How do you suggest passing in "mock tty" for the libraries to print with color? For some reason the tool I'm using won't allow me to force color output even with the --colors flag set.

@creack
Copy link
Owner

creack commented May 31, 2024

Well, you can use this library, but if you need a valid tty, you can't split stdout/stderr, otherwise, you will face 'invalid ioctl for device' errors.
I would recommend looking for a different tool that doesn't require a tty to display colors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants