From beb12e5a914897000a8d4d347fe4a644ba3fa18a Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Tue, 6 Sep 2016 22:31:28 +0300 Subject: [PATCH] Use "github.com/pkg/term" instead of "github.com/kless/term" --- curse.go | 41 +++++++++++++---------------------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/curse.go b/curse.go index 8dd11d5..613b1a1 100644 --- a/curse.go +++ b/curse.go @@ -12,7 +12,7 @@ import ( "strconv" "strings" - "github.com/kless/term" + "github.com/pkg/term" ) type Cursor struct { @@ -20,7 +20,7 @@ type Cursor struct { StartingPosition Position Style - terminal *term.Terminal + terminal *term.Term } type Position struct { @@ -40,7 +40,7 @@ func New() (*Cursor, error) { c := &Cursor{} c.Position.X, c.StartingPosition.X = col, col c.Position.Y, c.StartingPosition.Y = line, line - c.terminal, err = term.New() + c.terminal, err = term.Open("/bin/stty") return c, err } @@ -130,7 +130,7 @@ func (c *Cursor) SetDefaultStyle() *Cursor { } func (c *Cursor) ModeRaw() *Cursor { - _ = c.terminal.RawMode() + _ = c.terminal.SetRaw() return c } @@ -170,35 +170,20 @@ func GetScreenDimensions() (cols int, lines int, err error) { return cols, lines, nil } -func fallback_SetRawMode() { - rawMode := exec.Command("/bin/stty", "raw") - rawMode.Stdin = os.Stdin - _ = rawMode.Run() - rawMode.Wait() -} - -func fallback_SetCookedMode() { - // I've noticed that this does not always work when called from - // inside the program. From command line, you can run the following - // '$ go run calling_app.go; stty -raw' - // if you lose the ability to visably enter new text - cookedMode := exec.Command("/bin/stty", "-raw") - cookedMode.Stdin = os.Stdin - _ = cookedMode.Run() - cookedMode.Wait() -} - func GetCursorPosition() (col int, line int, err error) { // set terminal to raw mode and back - t, err := term.New() + t, err := term.Open("/bin/stty") if err != nil { - fallback_SetRawMode() - defer fallback_SetCookedMode() - } else { - t.RawMode() - defer t.Restore() + return } + err = t.SetRaw() + if err != nil { + return + } + + defer t.Restore() + // same as $ echo -e "\033[6n" // by printing the output, we are triggering input fmt.Printf(fmt.Sprintf("\r%c[6n", ESC))