Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chzyer committed Sep 27, 2015
1 parent 092d0fe commit 593678b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
4 changes: 2 additions & 2 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (o *opCompleter) OnComplete() {
buf := o.op.buf
rs := buf.Runes()

if o.IsInCompleteMode() && EqualRunes(rs, o.candidateSource) {
if o.IsInCompleteMode() && RunesEqual(rs, o.candidateSource) {
o.EnterCompleteSelectMode()
o.doSelect()
return
Expand All @@ -87,7 +87,7 @@ func (o *opCompleter) OnComplete() {
return
}

same, size := AggRunes(newLines)
same, size := RunesAggregate(newLines)
if size > 0 {
buf.WriteRunes(same)
o.ExitCompleteMode(false)
Expand Down
29 changes: 29 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package readline

import (
"container/list"
"fmt"
"os"
"time"
)

func sleep(n int) {
Debug(n)
time.Sleep(2000 * time.Millisecond)
}

// print a linked list to Debug()
func debugList(l *list.List) {
idx := 0
for e := l.Front(); e != nil; e = e.Next() {
Debug(idx, fmt.Sprintf("%+v", e.Value))
idx++
}
}

// append log info to another file
func Debug(o ...interface{}) {
f, _ := os.OpenFile("debug.tmp", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
fmt.Fprintln(f, o...)
f.Close()
}
2 changes: 1 addition & 1 deletion history.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (o *opHistory) NewHistory(current []rune) {
prev := back.Prev()
if prev != nil {
use := o.showItem(o.current.Value.(*hisItem))
if equalRunes(use, prev.Value.(*hisItem).Source) {
if RunesEqual(use, prev.Value.(*hisItem).Source) {
o.current = o.history.Back()
o.current.Value.(*hisItem).Clean()
o.historyVer++
Expand Down
55 changes: 14 additions & 41 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package readline

import (
"container/list"
"fmt"
"os"
"syscall"
"time"
"unicode"
"unsafe"

Expand All @@ -24,6 +20,7 @@ func MakeRaw(fd int) (*terminal.State, error) {
func Restore(fd int, state *terminal.State) error {
err := terminal.Restore(fd, state)
if err != nil {
// errno 0 means everything is ok :)
if err.Error() == "errno 0" {
err = nil
}
Expand All @@ -36,6 +33,7 @@ func IsPrintable(key rune) bool {
return key >= 32 && !isInSurrogateArea
}

// translate Esc[X
func escapeExKey(r rune) rune {
switch r {
case 'D':
Expand All @@ -50,6 +48,7 @@ func escapeExKey(r rune) rune {
return r
}

// translate EscX to Meta+X
func escapeKey(r rune) rune {
switch r {
case 'b':
Expand All @@ -68,19 +67,15 @@ func escapeKey(r rune) rune {
return r
}

func Debug(o ...interface{}) {
f, _ := os.OpenFile("debug.tmp", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
fmt.Fprintln(f, o...)
f.Close()
}

type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}

// get width of the terminal
// we can cache it here and refresh after received signal
func getWidth() int {
ws := &winsize{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
Expand All @@ -94,15 +89,7 @@ func getWidth() int {
return int(ws.Col)
}

func debugList(l *list.List) {
idx := 0
for e := l.Front(); e != nil; e = e.Next() {
Debug(idx, fmt.Sprintf("%+v", e.Value))
idx++
}
}

func equalRunes(a, b []rune) bool {
func RunesEqual(a, b []rune) bool {
if len(a) != len(b) {
return false
}
Expand All @@ -114,11 +101,7 @@ func equalRunes(a, b []rune) bool {
return true
}

func sleep(n int) {
Debug(n)
time.Sleep(2000 * time.Millisecond)
}

// calculate how many lines for N character
func LineCount(w int) int {
screenWidth := getWidth()
r := w / screenWidth
Expand All @@ -128,6 +111,7 @@ func LineCount(w int) int {
return r
}

// Search in runes from end to front
func RunesIndexBck(r, sub []rune) int {
for i := len(r) - len(sub); i >= 0; i-- {
found := true
Expand All @@ -144,6 +128,7 @@ func RunesIndexBck(r, sub []rune) int {
return -1
}

// Search in runes from front to end
func RunesIndex(r, sub []rune) int {
for i := 0; i < len(r); i++ {
found := true
Expand Down Expand Up @@ -229,7 +214,7 @@ func RunesWidth(r []rune) (length int) {
return
}

func AggRunes(candicate [][]rune) (same []rune, size int) {
func RunesAggregate(candicate [][]rune) (same []rune, size int) {
for i := 0; i < len(candicate[0]); i++ {
for j := 0; j < len(candicate)-1; j++ {
if i >= len(candicate[j]) || i >= len(candicate[j+1]) {
Expand All @@ -243,37 +228,25 @@ func AggRunes(candicate [][]rune) (same []rune, size int) {
}
aggregate:
if size > 0 {
same = CopyRunes(candicate[0][:size])
same = RunesCopy(candicate[0][:size])
for i := 0; i < len(candicate); i++ {
n := CopyRunes(candicate[i])
n := RunesCopy(candicate[i])
copy(n, n[size:])
candicate[i] = n[:len(n)-size]
}
}
return
}

func CopyRunes(r []rune) []rune {
func RunesCopy(r []rune) []rune {
n := make([]rune, len(r))
copy(n, r)
return n
}

func EqualRunes(r, r2 []rune) bool {
if len(r) != len(r2) {
return false
}
for idx := range r {
if r[idx] != r2[idx] {
return false
}
}
return true
}

func RunesHasPrefix(r, prefix []rune) bool {
if len(r) < len(prefix) {
return false
}
return EqualRunes(r[:len(prefix)], prefix)
return RunesEqual(r[:len(prefix)], prefix)
}
2 changes: 1 addition & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAggRunes(t *testing.T) {
},
}
for _, r := range runes {
same, off := AggRunes(r.r)
same, off := RunesAggregate(r.r)
if off != r.length {
t.Fatal("result not expect", off)
}
Expand Down

0 comments on commit 593678b

Please sign in to comment.