Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
abiosoft committed Jun 7, 2018
2 parents 140e0d8 + 2972be2 commit 155bce2
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/*
6 changes: 4 additions & 2 deletions complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ func (o *opCompleter) CompleteRefresh() {
// -1 to avoid reach the end of line
width := o.width - 1
colNum := width / colWidth
colWidth += (width - (colWidth * colNum)) / colNum
if colNum != 0 {
colWidth += (width - (colWidth * colNum)) / colNum
}

o.candidateColNum = colNum
buf := bufio.NewWriter(o.w)
Expand All @@ -219,7 +221,7 @@ func (o *opCompleter) CompleteRefresh() {
}
buf.WriteString(string(same))
buf.WriteString(string(c))
buf.Write(bytes.Repeat([]byte(" "), colWidth-len(c)-len(same)))
buf.Write(bytes.Repeat([]byte(" "), colWidth-runes.WidthAll(c)-runes.WidthAll(same)))

if inSelect {
buf.WriteString("\033[0m")
Expand Down
2 changes: 1 addition & 1 deletion operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewOperation(t *Terminal, cfg *Config) *Operation {
t: t,
buf: NewRuneBuffer(t, cfg.Prompt, cfg, width),
outchan: make(chan []rune),
errchan: make(chan error),
errchan: make(chan error, 1),
}
op.w = op.buf.w
op.SetConfig(cfg)
Expand Down
3 changes: 2 additions & 1 deletion readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Config struct {

FuncGetWidth func() int

Stdin io.Reader
Stdin io.ReadCloser
StdinWriter io.Writer
Stdout io.Writer
Stderr io.Writer
Expand Down Expand Up @@ -274,6 +274,7 @@ func (i *Instance) Close() error {
if err := i.Terminal.Close(); err != nil {
return err
}
i.Config.Stdin.Close()
i.Operation.Close()
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ loop:
}
}

func (r *RemoteSvr) Close() {
func (r *RemoteSvr) Close() error {
if atomic.CompareAndSwapInt32(&r.closed, 0, 1) {
close(r.stopChan)
r.conn.Close()
}
return nil
}

func (r *RemoteSvr) readLoop(buf *bufio.Reader) {
Expand Down
19 changes: 13 additions & 6 deletions std.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,20 @@ func (c *CancelableStdin) Close() error {
type FillableStdin struct {
sync.Mutex
stdin io.Reader
stdinBuffer io.Reader
stdinBuffer io.ReadCloser
buf []byte
bufErr error
}

// NewFillableStdin gives you FillableStdin
func NewFillableStdin(stdin io.Reader) (io.Reader, io.Writer) {

func NewFillableStdin(stdin io.Reader) (io.ReadCloser, io.Writer) {
r, w := io.Pipe()
s := &FillableStdin{
stdinBuffer: r,
stdin: stdin,
}
s.ioloop()
return s, w

}

func (s *FillableStdin) ioloop() {
Expand All @@ -161,6 +159,11 @@ func (s *FillableStdin) ioloop() {
bufR := make([]byte, 100)
var n int
n, s.bufErr = s.stdinBuffer.Read(bufR)
if s.bufErr != nil {
if s.bufErr == io.ErrClosedPipe {
break
}
}
s.Lock()
s.buf = append(s.buf, bufR[:n]...)
s.Unlock()
Expand All @@ -184,7 +187,11 @@ func (s *FillableStdin) Read(p []byte) (n int, err error) {
return n, cerr
}
s.Unlock()
n, err = s.stdin.Read(p)
return n, err
}

return s.stdin.Read(p)

func (s *FillableStdin) Close() error {
s.stdinBuffer.Close()
return nil
}

0 comments on commit 155bce2

Please sign in to comment.