From 8d163b44bf6546466649d3df656d2dc3ca8668bd Mon Sep 17 00:00:00 2001 From: c-bata Date: Thu, 15 Feb 2018 15:51:02 +0900 Subject: [PATCH] Fix handling of question --- console_interface.go | 4 ++-- posix_output.go | 49 +++++++++++++++++++++----------------------- windows_output.go | 49 +++++++++++++++++++++----------------------- 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/console_interface.go b/console_interface.go index 1215359..3819d34 100644 --- a/console_interface.go +++ b/console_interface.go @@ -53,11 +53,11 @@ type ConsoleWriter interface { // WriteRaw to write raw byte array. WriteRaw(data []byte) - // Write to write byte array without control sequences. + // Write to write safety byte array by removing control sequences. Write(data []byte) // WriteStr to write raw string. WriteRawStr(data string) - // WriteStr to write string without control sequences. + // WriteStr to write safety string by removing control sequences. WriteStr(data string) // Flush to flush buffer. Flush() error diff --git a/posix_output.go b/posix_output.go index 52c69d9..04ce255 100644 --- a/posix_output.go +++ b/posix_output.go @@ -3,6 +3,7 @@ package prompt import ( + "bytes" "strconv" "syscall" ) @@ -22,9 +23,9 @@ func (w *PosixWriter) WriteRaw(data []byte) { return } -// Write to write byte array without control sequences +// Write to write safety byte array by removing control sequences. func (w *PosixWriter) Write(data []byte) { - w.WriteRaw(byteFilter(data, writeFilter)) + w.WriteRaw(bytes.Replace(data, []byte{0x1b}, []byte{'?'}, -1)) return } @@ -34,7 +35,7 @@ func (w *PosixWriter) WriteRawStr(data string) { return } -// WriteStr to write string without control sequences +// WriteStr to write safety string by removing control sequences. func (w *PosixWriter) WriteStr(data string) { w.Write([]byte(data)) return @@ -216,8 +217,26 @@ func (w *PosixWriter) ScrollUp() { // SetTitle sets a title of terminal window. func (w *PosixWriter) SetTitle(title string) { + titleBytes := []byte(title) + patterns := []struct { + from []byte + to []byte + }{ + { + from: []byte{0x13}, + to: []byte{}, + }, + { + from: []byte{0x07}, + to: []byte{}, + }, + } + for i := range patterns { + titleBytes = bytes.Replace(titleBytes, patterns[i].from, patterns[i].to, -1) + } + w.WriteRaw([]byte{0x1b, 0x5d, 0x32, 0x3b}) - w.WriteRaw(byteFilter([]byte(title), setTextFilter)) + w.WriteRaw(titleBytes) w.WriteRaw([]byte{0x07}) return } @@ -303,28 +322,6 @@ var backgroundANSIColors = map[Color][]byte{ White: {0x31, 0x30, 0x37}, // 107 } -func writeFilter(buf byte) bool { - return buf != 0x1b && buf != 0x3f -} - -func setTextFilter(buf byte) bool { - return buf != 0x1b && buf != 0x07 -} - -func byteFilter(buf []byte, fn ...func(b byte) bool) []byte { - if len(fn) == 0 { - return buf - } - ret := make([]byte, 0, len(buf)) - f := fn[0] - for i, n := range buf { - if f(n) { - ret = append(ret, buf[i]) - } - } - return byteFilter(ret, fn[1:]...) -} - var _ ConsoleWriter = &PosixWriter{} // NewStandardOutputWriter returns ConsoleWriter object to write to stdout. diff --git a/windows_output.go b/windows_output.go index 2281ab1..08b2a50 100644 --- a/windows_output.go +++ b/windows_output.go @@ -3,6 +3,7 @@ package prompt import ( + "bytes" "io" "strconv" @@ -24,9 +25,9 @@ func (w *WindowsWriter) WriteRaw(data []byte) { return } -// Write to write byte array without control sequences +// Write to write safety byte array by removing control sequences. func (w *WindowsWriter) Write(data []byte) { - w.WriteRaw(byteFilter(data, writeFilter)) + w.WriteRaw(bytes.Replace(data, []byte{0x1b}, []byte{'?'}, -1)) return } @@ -36,7 +37,7 @@ func (w *WindowsWriter) WriteRawStr(data string) { return } -// WriteStr to write string without control sequences +// WriteStr to write safety string by removing control sequences. func (w *WindowsWriter) WriteStr(data string) { w.Write([]byte(data)) return @@ -214,8 +215,26 @@ func (w *WindowsWriter) ScrollUp() { // SetTitle sets a title of terminal window. func (w *WindowsWriter) SetTitle(title string) { + titleBytes := []byte(title) + patterns := []struct { + from []byte + to []byte + }{ + { + from: []byte{0x13}, + to: []byte{}, + }, + { + from: []byte{0x07}, + to: []byte{}, + }, + } + for i := range patterns { + titleBytes = bytes.Replace(titleBytes, patterns[i].from, patterns[i].to, -1) + } + w.WriteRaw([]byte{0x1b, 0x5d, 0x32, 0x3b}) - w.WriteRaw(byteFilter([]byte(title), setTextFilter)) + w.WriteRaw(titleBytes) w.WriteRaw([]byte{0x07}) return } @@ -301,28 +320,6 @@ var backgroundANSIColors = map[Color][]byte{ White: {0x31, 0x30, 0x37}, // 107 } -func writeFilter(buf byte) bool { - return buf != 0x1b && buf != 0x3f -} - -func setTextFilter(buf byte) bool { - return buf != 0x1b && buf != 0x07 -} - -func byteFilter(buf []byte, fn ...func(b byte) bool) []byte { - if len(fn) == 0 { - return buf - } - ret := make([]byte, 0, len(buf)) - f := fn[0] - for i, n := range buf { - if f(n) { - ret = append(ret, buf[i]) - } - } - return byteFilter(ret, fn[1:]...) -} - var _ ConsoleWriter = &WindowsWriter{} // NewStandardOutputWriter returns ConsoleWriter object to write to stdout.