Fix windows writer

This commit is contained in:
c-bata 2018-06-28 22:11:35 +09:00
parent 0f9b504036
commit 7f685d1a1d
4 changed files with 12 additions and 16 deletions

@ -138,9 +138,7 @@ func (ip *InputProcessor) Run(ctx context.Context) (err error) {
log.Printf("[ERROR] cannot read %s", err) log.Printf("[ERROR] cannot read %s", err)
return err return err
} }
if !(len(b) == 1 && b[0] == 0) { ip.UserInput <- b
ip.UserInput <- b
}
} }
} }
} }

@ -84,13 +84,11 @@ func (ip *InputProcessor) Run(ctx context.Context) (err error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return
case <-ip.Pause:
// Ignore on windows.
default: default:
b, err := ip.in.Read() b, err := ip.in.Read()
if err != nil { if err == nil && !(len(b) == 1 && b[0] == 0) {
log.Printf("[ERROR] cannot read %s", err)
return err
}
if !(len(b) == 1 && b[0] == 0) {
ip.UserInput <- b ip.UserInput <- b
} }
continue continue

@ -3,9 +3,8 @@
package prompt package prompt
import ( import (
"io"
"context" "context"
"io"
"github.com/mattn/go-colorable" "github.com/mattn/go-colorable"
"github.com/mattn/go-tty" "github.com/mattn/go-tty"
@ -21,8 +20,7 @@ type WindowsWriter struct {
// Flush to flush buffer // Flush to flush buffer
func (w *WindowsWriter) Flush() error { func (w *WindowsWriter) Flush() error {
t := w.tty.Output() _, err := w.writer.Write(w.buffer)
_, err := t.Write(w.buffer)
if err != nil { if err != nil {
return err return err
} }
@ -32,7 +30,7 @@ func (w *WindowsWriter) Flush() error {
// GetWinSize returns WinSize object to represent width and height of terminal. // GetWinSize returns WinSize object to represent width and height of terminal.
func (w *WindowsWriter) GetWinSize() WinSize { func (w *WindowsWriter) GetWinSize() WinSize {
row, col, err := w.tty.Size() col, row, err := w.tty.Size()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -61,12 +59,13 @@ var _ ConsoleWriter = &WindowsWriter{}
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout. // NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
// This generates win32 control sequences. // This generates win32 control sequences.
func NewStandardOutputWriter() (ConsoleWriter, error) { func NewStandardOutputWriter() (ConsoleWriter, error) {
w := &WindowsWriter{}
t, err := tty.Open() t, err := tty.Open()
if err != nil { if err != nil {
return nil, err return nil, err
} }
w.tty = t w := &WindowsWriter{
w.writer = colorable.NewColorable(t.Output()) tty: t,
writer: colorable.NewColorable(t.Output()),
}
return w, nil return w, nil
} }

@ -322,6 +322,7 @@ func (r *Renderer) breakLine(buffer *Buffer) {
r.out.WriteStr(buffer.Document().Text + "\n") r.out.WriteStr(buffer.Document().Text + "\n")
r.out.SetColor(DefaultColor, DefaultColor, false) r.out.SetColor(DefaultColor, DefaultColor, false)
r.out.Flush() r.out.Flush()
r.previousCursor = 0
r.previousRequest.buffer = buffer r.previousRequest.buffer = buffer
} }