Skip to content

Instantly share code, notes, and snippets.

@robinmonjo
Created July 27, 2014 15:03
Show Gist options
  • Save robinmonjo/3b100f01fe14dba8959f to your computer and use it in GitHub Desktop.
Save robinmonjo/3b100f01fe14dba8959f to your computer and use it in GitHub Desktop.
Go pty setup for interactive shell
package main
import (
"code.google.com/p/go.crypto/ssh/terminal"
"github.com/kr/pty"
"io"
"os"
"os/exec"
)
func main() {
c := exec.Command("bash")
f, err := pty.Start(c)
if err != nil {
panic(err)
}
oldState, _ := terminal.MakeRaw(int(os.Stdin.Fd()))
defer terminal.Restore(int(os.Stdin.Fd()), oldState)
term := terminal.NewTerminal(os.Stdin, "")
cb := func(s string, i int, r rune) (string, int, bool) {
return keypressed(term, s, i, r)
}
term.AutoCompleteCallback = cb
go func() {
io.Copy(f, os.Stdin)
}()
io.Copy(os.Stdout, f)
}
func keypressed(t *terminal.Terminal, line string, pos int, key rune) (newLine string, newPos int, ok bool) {
c := []byte{byte(key)}
t.Write(c) //write to tty directly
return line, pos, false //print on stdin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment