diff options
| author | Jeff Carr <[email protected]> | 2024-01-15 19:24:48 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-01-15 19:24:48 -0600 |
| commit | fdac7e7b8944d51b8207c1797edd0be9450de7b8 (patch) | |
| tree | 9e762c05ea6f716dc9434dae1e852630bd6c0ec5 /old/bash.go | |
| parent | 94aa368cff322e667156571638a45bd3117a2739 (diff) | |
restore the files after garbage collectionv0.5.5
hopefully this actually is a valid git repo
Diffstat (limited to 'old/bash.go')
| -rw-r--r-- | old/bash.go | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/old/bash.go b/old/bash.go new file mode 100644 index 0000000..7143c1f --- /dev/null +++ b/old/bash.go @@ -0,0 +1,61 @@ +package main + +import ( + "io" + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/creack/pty" + "golang.org/x/term" + + "go.wit.com/log" +) + +func test() error { + // Create arbitrary command. + c := exec.Command("bash") + + // Start the command with a pty. + ptmx, err := pty.Start(c) + if err != nil { + return err + } + // Make sure to close the pty at the end. + defer func() { _ = ptmx.Close() }() // Best effort. + + // Handle pty size. + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGWINCH) + go func() { + for range ch { + if err := pty.InheritSize(os.Stdin, ptmx); err != nil { + log.Println("error resizing pty: %s", err) + } + } + }() + ch <- syscall.SIGWINCH // Initial resize. + defer func() { signal.Stop(ch); close(ch) }() // Cleanup signals when done. + + // Set stdin in raw mode. + oldState, err := term.MakeRaw(int(os.Stdin.Fd())) + if err != nil { + panic(err) + } + defer func() { _ = term.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort. + + // Copy stdin to the pty and the pty to stdout. + // NOTE: The goroutine will keep reading until the next keystroke before returning. + go func() { _, _ = io.Copy(ptmx, os.Stdin) }() + _, _ = io.Copy(os.Stdout, ptmx) + + return nil +} + +func mainBash() { + if err := test(); err != nil { + log.Error(err, "exit in mainBash()") + log.Exit(err) + } +} |
