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

How to connect stderr to a terminal? #102

Closed
nagylzs opened this issue Jan 18, 2025 · 2 comments
Closed

How to connect stderr to a terminal? #102

nagylzs opened this issue Jan 18, 2025 · 2 comments

Comments

@nagylzs
Copy link

nagylzs commented Jan 18, 2025

RunWithConnection takes two parameters: stdin and stdout. I can see that the Terminal struct only contains these:

type Terminal struct {
        // more fields here...
	pty io.Closer
	in  io.WriteCloser
	out io.Reader
        // more fields here...
}

I wonder why stderr is not given there? What happens if the remote side prints something to stderr? Is it simply ignored by fyne.Terminal? Isn't it a problem?

@nagylzs
Copy link
Author

nagylzs commented Jan 18, 2025

I figured out that connecting the pty (as a pipe) directly to t.RunWithConnection() does the trick. So it seems.

@nagylzs
Copy link
Author

nagylzs commented Jan 18, 2025

It seems that connecting the pty itself instead of stdin and stdout works, here is a minimal working example:

package main

import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"github.com/creack/pty"
	"github.com/fyne-io/terminal"
	"log/slog"
	"os"
	"os/exec"
	"sync"
)

func main() {
	err := runMain()
	if err != nil {
		slog.Error(err.Error())
		os.Exit(1)
	}
}

func runMain() error {
	wg := sync.WaitGroup{}
	wg.Add(1)

	term := terminal.New()
	go func() {
		var err error

		cmd := exec.Command("/usr/bin/bash")
		env := os.Environ()
		env = append(env, "TERM=xterm-256color")
		cmd.Env = env
		// connect tty instead of stdin and stdout
		tty, err := pty.Start(cmd)

		if err != nil {
			slog.Error(err.Error())
			os.Exit(1)
		}
		slog.Info("started", "pid", cmd.Process.Pid)
		err = term.RunWithConnection(tty, tty)

		if err != nil {
			slog.Error(err.Error())
		}

		wg.Done()
	}()

	a := app.New()
	go func() {
		wg.Wait()
		a.Quit()
	}()
	w := a.NewWindow("Test")
	w.SetContent(container.New(layout.NewStackLayout(), term))
	w.ShowAndRun()
	return nil
}

However, it is still not clear how to forward events like terminal resize.

See #103

@nagylzs nagylzs closed this as completed Jan 18, 2025
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

1 participant