Skip to content

Commit

Permalink
Re-implemented in pure golang.
Browse files Browse the repository at this point in the history
Signed-off-by: xchenan <xchenan@gmail.com>
  • Loading branch information
xchenan committed May 2, 2017
1 parent 5f1c81f commit 1665a79
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 124 deletions.
10 changes: 0 additions & 10 deletions edit/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,11 @@ func setupTerminal(file *os.File) (*sys.Termios, error) {
}

savedTermios := term.Copy()
fmt.Printf("%v\n", savedTermios)

term.SetICanon(false)
fmt.Printf("%v\n", term)
term.SetEcho(false)
fmt.Printf("%v\n", term)
term.SetVMin(1)
fmt.Printf("%v\n", term)
term.SetVTime(0)
fmt.Printf("%v\n", term)

err = term.ApplyToFd(fd)
if err != nil {
Expand Down Expand Up @@ -293,7 +288,6 @@ func (ed *Editor) startReadLine() error {

// Enable bracketed paste.
ed.file.WriteString("\033[?2004h")
fmt.Printf("after write: %v:", ed)

return nil
}
Expand Down Expand Up @@ -342,9 +336,7 @@ func (ed *Editor) finishReadLine(addError func(error)) {

// ReadLine reads a line interactively.
func (ed *Editor) ReadLine() (line string, err error) {
fmt.Printf("ed start: %v\n", ed)
e := ed.startReadLine()
fmt.Printf("ed after start: %v", e)
if e != nil {
return "", e
}
Expand All @@ -371,10 +363,8 @@ MainLoop:
for {
ed.promptContent = callPrompt(ed, ed.prompt.Get().(eval.Callable))
ed.rpromptContent = callPrompt(ed, ed.rprompt.Get().(eval.Callable))
fmt.Printf("ed before refresh: %v", ed)

err := ed.refresh(fullRefresh, true)
fmt.Printf("ed after refresh: %v", ed)
fullRefresh = false
if err != nil {
return "", err
Expand Down
5 changes: 0 additions & 5 deletions sys/isatty.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package sys

/*
#include <unistd.h>
*/
// import "C"
import (
"github.com/mattn/go-isatty"
)

func IsATTY(fd int) bool {
return isatty.IsTerminal(uintptr(fd)) ||
isatty.IsCygwinTerminal(uintptr(fd))
// return C.isatty(C.int(fd)) != 0
}
48 changes: 12 additions & 36 deletions sys/select.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
package sys

/*
#include <sys/select.h>
void fdclr(int fd, fd_set *set) {
FD_CLR(fd, set);
}
int fdisset(int fd, fd_set *set) {
return FD_ISSET(fd, set);
}
void fdset(int fd, fd_set *set) {
FD_SET(fd, set);
}
void fdzero(fd_set *set) {
FD_ZERO(set);
}
*/
// import "C"

import (
"syscall"
// "unsafe"
)

type FdSet syscall.FdSet

const NFDBits = 64

// func (fs *FdSet) c() *C.fd_set {
// return (*C.fd_set)(unsafe.Pointer(fs))
// }
func index(fd int) (idx uint, bit int64) {
u := uint(fd)
return u / NFDBits, 1 << (u % NFDBits)
}

func (fs *FdSet) s() *syscall.FdSet {
return (*syscall.FdSet)(fs)
Expand All @@ -45,24 +24,21 @@ func NewFdSet(fds ...int) *FdSet {
}

func (fs *FdSet) Clear(fds ...int) {
for _, v := range fds {
fd := uint(v)
fs.Bits[fd/NFDBits] &= ^(1 << (fd % NFDBits))
// C.fdclr(C.int(fd), fs.c())
for _, fd := range fds {
idx, bit := index(fd)
fs.Bits[idx] &= ^bit
}
}

func (fs *FdSet) IsSet(fd int) bool {
f := uint(fd)
return fs.Bits[f/NFDBits]&(1<<(f%NFDBits)) != 0
// return C.fdisset(C.int(fd), fs.c()) != 0
idx, bit := index(fd)
return fs.Bits[idx]&bit != 0
}

func (fs *FdSet) Set(fds ...int) {
for _, v := range fds {
fd := uint(v)
fs.Bits[fd/NFDBits] |= (1 << (fd % NFDBits))
// C.fdset(C.int(fd), fs.c())
for _, fd := range fds {
idx, bit := index(fd)
fs.Bits[idx] |= bit
}
}

Expand Down
27 changes: 0 additions & 27 deletions sys/tc.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
package sys

/*
#include <unistd.h>
#include <errno.h>
pid_t get(int fd) {
return tcgetpgrp(fd);
}
int set(int fd, pid_t pid) {
return tcsetpgrp(fd, pid);
}
int e() {
return errno;
}
*/
// import "C"
import (
"syscall"
"unsafe"
Expand All @@ -30,11 +13,6 @@ func Tcgetpgrp(fd int) (int, error) {
return pid, nil
}
return -1, errno
// i := C.get(C.int(fd))
// if i == -1 {
// return -1, syscall.Errno(C.e())
// }
// return int(i), nil
}

func Tcsetpgrp(fd int, pid int) error {
Expand All @@ -44,9 +22,4 @@ func Tcsetpgrp(fd int, pid int) error {
return nil
}
return errno
// i := C.set(C.int(fd), C.pid_t(pid))
// if i != 0 {
// return syscall.Errno(C.e())
// }
// return nil
}
35 changes: 5 additions & 30 deletions sys/termios.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2015 go-termios Author. All Rights Reserved.
// https://github.com/go-termios/termios
// Author: John Lenton <chipaca@github.com>

package sys

/*
#include <termios.h>
*/
// import "C"
import (
"golang.org/x/sys/unix"
// "syscall"
"unsafe"
)

Expand All @@ -33,29 +32,11 @@ func NewTermiosFromFd(fd int) (*Termios, error) {
return nil, err
}
return &term, nil
// term := new(Termios)
// err := term.FromFd(fd)
// if err != nil {
// return nil, err
// }
// return term, nil
}

// func (term *Termios) c() *C.struct_termios {
// return (*C.struct_termios)(term)
// }

// FromFd fills term with the terminal attribute of the given file descriptor.
// func (term *Termios) FromFd(fd int) error {
// _, err := C.tcgetattr((C.int)(fd), term.c())
// return err
// }

// ApplyToFd applies term to the given file descriptor.
func (term *Termios) ApplyToFd(fd int) error {
return ioctl(uintptr(fd), setAttrNowIOCTL, unsafe.Pointer(&term))
// _, err := C.tcsetattr((C.int)(fd), 0, term.c())
// return err
return ioctl(uintptr(fd), setAttrNowIOCTL, unsafe.Pointer(term))
}

// Copy returns a copy of term.
Expand All @@ -67,13 +48,11 @@ func (term *Termios) Copy() *Termios {
// SetVTime sets the timeout in deciseconds for noncanonical read.
func (term *Termios) SetVTime(v uint8) {
term.Cc[unix.VTIME] = v
// term.c_cc[C.VTIME] = C.cc_t(v)
}

// SetVMin sets the minimal number of characters for noncanonical read.
func (term *Termios) SetVMin(v uint8) {
term.Cc[unix.VMIN] = v
// term.c_cc[C.VMIN] = C.cc_t(v)
}

func setFlag(flag *uint32, mask uint32, v bool) {
Expand All @@ -87,18 +66,14 @@ func setFlag(flag *uint32, mask uint32, v bool) {
// SetICanon sets the canonical flag.
func (term *Termios) SetICanon(v bool) {
setFlag(&term.Lflag, unix.ICANON, v)
// setFlag(&term.c_lflag, C.ICANON, v)
}

// SetEcho sets the echo flag.
func (term *Termios) SetEcho(v bool) {
setFlag(&term.Lflag, unix.ECHO, v)
// setFlag(&term.c_lflag, C.ECHO, v)
}

// FlushInput discards data written to a file descriptor but not read.
func FlushInput(fd int) error {
return ioctlu(uintptr(fd), flushIOCTL, uintptr(unix.TCIFLUSH))
// _, err := C.tcflush((C.int)(fd), syscall.TCIFLUSH)
// return err
}
4 changes: 4 additions & 0 deletions sys/termios_freebsd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2015 go-termios Author. All Rights Reserved.
// https://github.com/go-termios/termios
// Author: John Lenton <chipaca@github.com>

package sys

import (
Expand Down
4 changes: 4 additions & 0 deletions sys/termios_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2015 go-termios Author. All Rights Reserved.
// https://github.com/go-termios/termios
// Author: John Lenton <chipaca@github.com>

package sys

import (
Expand Down
20 changes: 4 additions & 16 deletions sys/winsize.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package sys

/*
#include <termios.h>
#include <sys/ioctl.h>
// Copyright 2015 go-termios Author. All Rights Reserved.
// https://github.com/go-termios/termios
// Author: John Lenton <chipaca@github.com>

void getwinsize(int fd, int *row, int *col) {
struct winsize wsz;
ioctl(fd, TIOCGWINSZ, &wsz);
*row = wsz.ws_row;
*col = wsz.ws_col;
}
*/
// import "C"
package sys

import (
"fmt"
Expand All @@ -36,7 +27,4 @@ func GetWinsize(fd int) (row, col int) {
return -1, -1
}
return int(ws.row), int(ws.col)
// var r, c C.int
// C.getwinsize(C.int(fd), &r, &c)
// return int(r), int(c)
}

0 comments on commit 1665a79

Please sign in to comment.