Merge pull request #45 from c-bata/fix-question-handling
Fix handling of question mark
This commit is contained in:
commit
7e7f5ec007
|
@ -53,11 +53,11 @@ type ConsoleWriter interface {
|
||||||
|
|
||||||
// WriteRaw to write raw byte array.
|
// WriteRaw to write raw byte array.
|
||||||
WriteRaw(data []byte)
|
WriteRaw(data []byte)
|
||||||
// Write to write byte array without control sequences.
|
// Write to write safety byte array by removing control sequences.
|
||||||
Write(data []byte)
|
Write(data []byte)
|
||||||
// WriteStr to write raw string.
|
// WriteStr to write raw string.
|
||||||
WriteRawStr(data string)
|
WriteRawStr(data string)
|
||||||
// WriteStr to write string without control sequences.
|
// WriteStr to write safety string by removing control sequences.
|
||||||
WriteStr(data string)
|
WriteStr(data string)
|
||||||
// Flush to flush buffer.
|
// Flush to flush buffer.
|
||||||
Flush() error
|
Flush() error
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package prompt
|
package prompt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
@ -22,9 +23,9 @@ func (w *PosixWriter) WriteRaw(data []byte) {
|
||||||
return
|
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) {
|
func (w *PosixWriter) Write(data []byte) {
|
||||||
w.WriteRaw(byteFilter(data, writeFilter))
|
w.WriteRaw(bytes.Replace(data, []byte{0x1b}, []byte{'?'}, -1))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ func (w *PosixWriter) WriteRawStr(data string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteStr to write string without control sequences
|
// WriteStr to write safety string by removing control sequences.
|
||||||
func (w *PosixWriter) WriteStr(data string) {
|
func (w *PosixWriter) WriteStr(data string) {
|
||||||
w.Write([]byte(data))
|
w.Write([]byte(data))
|
||||||
return
|
return
|
||||||
|
@ -216,8 +217,26 @@ func (w *PosixWriter) ScrollUp() {
|
||||||
|
|
||||||
// SetTitle sets a title of terminal window.
|
// SetTitle sets a title of terminal window.
|
||||||
func (w *PosixWriter) SetTitle(title string) {
|
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([]byte{0x1b, 0x5d, 0x32, 0x3b})
|
||||||
w.WriteRaw(byteFilter([]byte(title), setTextFilter))
|
w.WriteRaw(titleBytes)
|
||||||
w.WriteRaw([]byte{0x07})
|
w.WriteRaw([]byte{0x07})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -303,28 +322,6 @@ var backgroundANSIColors = map[Color][]byte{
|
||||||
White: {0x31, 0x30, 0x37}, // 107
|
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{}
|
var _ ConsoleWriter = &PosixWriter{}
|
||||||
|
|
||||||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
package prompt
|
package prompt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -24,9 +25,9 @@ func (w *WindowsWriter) WriteRaw(data []byte) {
|
||||||
return
|
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) {
|
func (w *WindowsWriter) Write(data []byte) {
|
||||||
w.WriteRaw(byteFilter(data, writeFilter))
|
w.WriteRaw(bytes.Replace(data, []byte{0x1b}, []byte{'?'}, -1))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ func (w *WindowsWriter) WriteRawStr(data string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteStr to write string without control sequences
|
// WriteStr to write safety string by removing control sequences.
|
||||||
func (w *WindowsWriter) WriteStr(data string) {
|
func (w *WindowsWriter) WriteStr(data string) {
|
||||||
w.Write([]byte(data))
|
w.Write([]byte(data))
|
||||||
return
|
return
|
||||||
|
@ -214,8 +215,26 @@ func (w *WindowsWriter) ScrollUp() {
|
||||||
|
|
||||||
// SetTitle sets a title of terminal window.
|
// SetTitle sets a title of terminal window.
|
||||||
func (w *WindowsWriter) SetTitle(title string) {
|
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([]byte{0x1b, 0x5d, 0x32, 0x3b})
|
||||||
w.WriteRaw(byteFilter([]byte(title), setTextFilter))
|
w.WriteRaw(titleBytes)
|
||||||
w.WriteRaw([]byte{0x07})
|
w.WriteRaw([]byte{0x07})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -301,28 +320,6 @@ var backgroundANSIColors = map[Color][]byte{
|
||||||
White: {0x31, 0x30, 0x37}, // 107
|
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{}
|
var _ ConsoleWriter = &WindowsWriter{}
|
||||||
|
|
||||||
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
// NewStandardOutputWriter returns ConsoleWriter object to write to stdout.
|
||||||
|
|
Loading…
Reference in New Issue