Skip to content

Use x/term on Unix systems, deduplicate tscreen_*.go #2

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

Open
wants to merge 4 commits into
base: legacy
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ require (
github.com/gdamore/encoding v1.0.0
github.com/lucasb-eyer/go-colorful v1.0.3
github.com/mattn/go-runewidth v0.0.7
github.com/xo/terminfo v0.0.0-20200218205459-454e5b68f9e8
github.com/zyedidia/poller v1.0.1
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf
golang.org/x/text v0.3.0
)

Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tW
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/xo/terminfo v0.0.0-20200218205459-454e5b68f9e8 h1:woqigIZtZUZxws1zZA99nAvuz2mQrxtWsuZSR9c8I/A=
github.com/xo/terminfo v0.0.0-20200218205459-454e5b68f9e8/go.mod h1:6Yhx5ZJl5942QrNRWLwITArVT9okUXc5c3brgWJMoDc=
github.com/zyedidia/poller v1.0.1 h1:Tt9S3AxAjXwWGNiC2TUdRJkQDZSzCBNVQ4xXiQ7440s=
github.com/zyedidia/poller v1.0.1/go.mod h1:vZXJOHGDcuK08GXhF6IAY0ZFd2WcgOR5DOTp84Uk5eE=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3 changes: 2 additions & 1 deletion tscreen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"
"unicode/utf8"

"golang.org/x/term"
"golang.org/x/text/transform"

"github.com/micro-editor/tcell/v2/terminfo"
Expand Down Expand Up @@ -126,7 +127,6 @@ type tScreen struct {
clear bool
cursorx int
cursory int
tiosp *termiosPrivate
wasbtn bool
acs map[rune]string
charset string
Expand All @@ -140,6 +140,7 @@ type tScreen struct {
buttondn bool
rawseq []string
finiOnce sync.Once
saved *term.State

sync.Mutex
}
Expand Down
121 changes: 0 additions & 121 deletions tscreen_bsd.go

This file was deleted.

77 changes: 35 additions & 42 deletions tscreen_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,19 @@ package tcell
// close it, it actually closes

import (
"os"
"os/signal"
"strconv"
"syscall"
"unsafe"

"github.com/zyedidia/poller"
"golang.org/x/sys/unix"
"golang.org/x/term"
)

type termiosPrivate syscall.Termios

func (t *tScreen) termioInit() error {
var e error
var newtios termiosPrivate
var fd uintptr
var tios uintptr
var ioc uintptr
t.tiosp = &termiosPrivate{}
var state *term.State

if t.in, e = poller.Open("/dev/tty", poller.O_RO); e != nil {
goto failed
Expand All @@ -60,31 +57,12 @@ func (t *tScreen) termioInit() error {
goto failed
}

tios = uintptr(unsafe.Pointer(t.tiosp))
ioc = uintptr(syscall.TIOCGETA)
fd = uintptr(t.out.(*poller.FD).Sysfd())
if _, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0); e1 != 0 {
e = e1
state, e = term.MakeRaw(t.out.(*poller.FD).Sysfd())
if e != nil {
goto failed
}

newtios = *t.tiosp
newtios.Iflag &^= syscall.IGNBRK | syscall.BRKINT | syscall.PARMRK |
syscall.ISTRIP | syscall.INLCR | syscall.IGNCR |
syscall.ICRNL | syscall.IXON
newtios.Oflag &^= syscall.OPOST
newtios.Lflag &^= syscall.ECHO | syscall.ECHONL | syscall.ICANON |
syscall.ISIG | syscall.IEXTEN
newtios.Cflag &^= syscall.CSIZE | syscall.PARENB
newtios.Cflag |= syscall.CS8

tios = uintptr(unsafe.Pointer(&newtios))

ioc = uintptr(syscall.TIOCSETA)
if _, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0); e1 != 0 {
e = e1
goto failed
}
t.saved = state

signal.Notify(t.sigwinch, syscall.SIGWINCH)

Expand All @@ -110,11 +88,8 @@ func (t *tScreen) termioFini() {

<-t.indoneq

if t.out != nil {
fd := uintptr(t.out.(*poller.FD).Sysfd())
ioc := uintptr(syscall.TIOCSETAF)
tios := uintptr(unsafe.Pointer(t.tiosp))
syscall.Syscall6(syscall.SYS_IOCTL, fd, ioc, tios, 0, 0, 0)
if t.out != nil && t.saved != nil {
term.Restore(t.out.(*poller.FD).Sysfd(), t.saved)
t.out.(*poller.FD).Close()
}

Expand All @@ -125,15 +100,33 @@ func (t *tScreen) termioFini() {

func (t *tScreen) getWinSize() (int, int, error) {

fd := uintptr(t.out.(*poller.FD).Sysfd())
dim := [4]uint16{}
dimp := uintptr(unsafe.Pointer(&dim))
ioc := uintptr(syscall.TIOCGWINSZ)
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL,
fd, ioc, dimp, 0, 0, 0); err != 0 {
wsz, err := unix.IoctlGetWinsize(int(t.out.(*poller.FD).Sysfd()), unix.TIOCGWINSZ)
if err != nil {
return -1, -1, err
}
return int(dim[1]), int(dim[0]), nil
cols := int(wsz.Col)
rows := int(wsz.Row)
if cols == 0 {
colsEnv := os.Getenv("COLUMNS")
if colsEnv != "" {
if cols, err = strconv.Atoi(colsEnv); err != nil {
return -1, -1, err
}
} else {
cols = t.ti.Columns
}
}
if rows == 0 {
rowsEnv := os.Getenv("LINES")
if rowsEnv != "" {
if rows, err = strconv.Atoi(rowsEnv); err != nil {
return -1, -1, err
}
} else {
rows = t.ti.Lines
}
}
return cols, rows, nil
}

func (t *tScreen) Beep() error {
Expand Down
Loading