Skip to content

Instantly share code, notes, and snippets.

@mkideal
Last active May 7, 2020 09:04
Show Gist options
  • Save mkideal/19f06e04d612c735cf52ee7c7be25400 to your computer and use it in GitHub Desktop.
Save mkideal/19f06e04d612c735cf52ee7c7be25400 to your computer and use it in GitHub Desktop.
demo terminal app
package xyz
import (
"io"
"os"
"golang.org/x/crypto/ssh/terminal"
)
type readerWriter struct {
io.Reader
io.Writer
}
func run(stdin *os.File, stdout io.Writer) (err error) {
term := terminal.NewTerminal(readerWriter{stdin, stdout}, "$> ")
stdinFD := int(stdin.Fd())
stdinState, err := terminal.MakeRaw(stdinFD)
if err != nil {
return err
}
defer func() {
terminal.Restore(stdinFD, stdinState)
}()
for {
prompt := "$> "
term.SetPrompt(prompt)
line, err := term.ReadLine()
if err != nil {
return err
}
//TODO: handle the line
_ = line
}
return nil
}
@isomorphisms
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment