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

Use "github.com/pkg/term" instead of "github.com/kless/term" #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 13 additions & 28 deletions curse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (
"strconv"
"strings"

"github.com/kless/term"
"github.com/pkg/term"
)

type Cursor struct {
Position
StartingPosition Position
Style

terminal *term.Terminal
terminal *term.Term
}

type Position struct {
Expand All @@ -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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an older Go program that I resurrected that uses curse and it gets an error here:
open /bin/stty: operation not permitted
I've tried it as root too just to be sure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, weird, I can take a look after the weekend, if you not figure this out by then that is

return c, err
}

Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *Cursor) SetDefaultStyle() *Cursor {
}

func (c *Cursor) ModeRaw() *Cursor {
_ = c.terminal.RawMode()
_ = c.terminal.SetRaw()

return c
}
Expand Down Expand Up @@ -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))
Expand Down