2015-09-29 12:49:58 +03:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
2015-09-29 16:01:17 +03:00
|
|
|
"bufio"
|
2015-09-29 12:49:58 +03:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"unicode/utf8"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-09-29 18:28:12 +03:00
|
|
|
const (
|
|
|
|
_ = uint16(0)
|
|
|
|
COLOR_FBLUE = 0x0001
|
|
|
|
COLOR_FGREEN = 0x0002
|
|
|
|
COLOR_FRED = 0x0004
|
|
|
|
COLOR_FINTENSITY = 0x0008
|
|
|
|
|
|
|
|
COLOR_BBLUE = 0x0010
|
|
|
|
COLOR_BGREEN = 0x0020
|
|
|
|
COLOR_BRED = 0x0040
|
|
|
|
COLOR_BINTENSITY = 0x0080
|
|
|
|
|
|
|
|
COMMON_LVB_UNDERSCORE = 0x8000
|
2017-10-02 06:59:06 +03:00
|
|
|
COMMON_LVB_BOLD = 0x0007
|
2015-09-29 18:28:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var ColorTableFg = []word{
|
|
|
|
0, // 30: Black
|
|
|
|
COLOR_FRED, // 31: Red
|
|
|
|
COLOR_FGREEN, // 32: Green
|
|
|
|
COLOR_FRED | COLOR_FGREEN, // 33: Yellow
|
|
|
|
COLOR_FBLUE, // 34: Blue
|
|
|
|
COLOR_FRED | COLOR_FBLUE, // 35: Magenta
|
|
|
|
COLOR_FGREEN | COLOR_FBLUE, // 36: Cyan
|
|
|
|
COLOR_FRED | COLOR_FBLUE | COLOR_FGREEN, // 37: White
|
|
|
|
}
|
|
|
|
|
|
|
|
var ColorTableBg = []word{
|
|
|
|
0, // 40: Black
|
|
|
|
COLOR_BRED, // 41: Red
|
|
|
|
COLOR_BGREEN, // 42: Green
|
|
|
|
COLOR_BRED | COLOR_BGREEN, // 43: Yellow
|
|
|
|
COLOR_BBLUE, // 44: Blue
|
|
|
|
COLOR_BRED | COLOR_BBLUE, // 45: Magenta
|
|
|
|
COLOR_BGREEN | COLOR_BBLUE, // 46: Cyan
|
|
|
|
COLOR_BRED | COLOR_BBLUE | COLOR_BGREEN, // 47: White
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type ANSIWriter struct {
|
|
|
|
target io.Writer
|
|
|
|
wg sync.WaitGroup
|
2016-07-23 19:39:44 +03:00
|
|
|
ctx *ANSIWriterCtx
|
2015-09-29 12:49:58 +03:00
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewANSIWriter(w io.Writer) *ANSIWriter {
|
|
|
|
a := &ANSIWriter{
|
|
|
|
target: w,
|
2016-07-23 19:39:44 +03:00
|
|
|
ctx: NewANSIWriterCtx(w),
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ANSIWriter) Close() error {
|
|
|
|
a.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-23 19:39:44 +03:00
|
|
|
type ANSIWriterCtx struct {
|
|
|
|
isEsc bool
|
|
|
|
isEscSeq bool
|
|
|
|
arg []string
|
|
|
|
target *bufio.Writer
|
|
|
|
wantFlush bool
|
|
|
|
}
|
2015-09-29 12:49:58 +03:00
|
|
|
|
2016-07-23 19:39:44 +03:00
|
|
|
func NewANSIWriterCtx(target io.Writer) *ANSIWriterCtx {
|
|
|
|
return &ANSIWriterCtx{
|
|
|
|
target: bufio.NewWriter(target),
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
2016-07-23 19:39:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ANSIWriterCtx) Flush() {
|
|
|
|
a.target.Flush()
|
|
|
|
}
|
2015-09-29 12:49:58 +03:00
|
|
|
|
2016-07-23 19:39:44 +03:00
|
|
|
func (a *ANSIWriterCtx) process(r rune) bool {
|
|
|
|
if a.wantFlush {
|
|
|
|
if r == 0 || r == CharEsc {
|
|
|
|
a.wantFlush = false
|
|
|
|
a.target.Flush()
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-23 19:39:44 +03:00
|
|
|
if a.isEscSeq {
|
|
|
|
a.isEscSeq = a.ioloopEscSeq(a.target, r, &a.arg)
|
|
|
|
return true
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch r {
|
|
|
|
case CharEsc:
|
2016-07-23 19:39:44 +03:00
|
|
|
a.isEsc = true
|
2015-09-29 12:49:58 +03:00
|
|
|
case '[':
|
2016-07-23 19:39:44 +03:00
|
|
|
if a.isEsc {
|
|
|
|
a.arg = nil
|
|
|
|
a.isEscSeq = true
|
|
|
|
a.isEsc = false
|
2015-09-29 12:49:58 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
default:
|
2016-07-23 19:39:44 +03:00
|
|
|
a.target.WriteRune(r)
|
|
|
|
a.wantFlush = true
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
2016-07-23 19:39:44 +03:00
|
|
|
return true
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
|
|
|
|
2016-07-23 19:39:44 +03:00
|
|
|
func (a *ANSIWriterCtx) ioloopEscSeq(w *bufio.Writer, r rune, argptr *[]string) bool {
|
2015-09-29 12:49:58 +03:00
|
|
|
arg := *argptr
|
|
|
|
var err error
|
2015-09-29 16:01:17 +03:00
|
|
|
|
2022-05-16 04:56:06 +03:00
|
|
|
if (r >= 'A' && r <= 'D') || r == 'G' {
|
2015-09-29 16:01:17 +03:00
|
|
|
count := short(GetInt(arg, 1))
|
|
|
|
info, err := GetConsoleScreenBufferInfo()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch r {
|
|
|
|
case 'A': // up
|
|
|
|
info.dwCursorPosition.y -= count
|
|
|
|
case 'B': // down
|
|
|
|
info.dwCursorPosition.y += count
|
|
|
|
case 'C': // right
|
|
|
|
info.dwCursorPosition.x += count
|
|
|
|
case 'D': // left
|
|
|
|
info.dwCursorPosition.x -= count
|
2022-05-16 04:56:06 +03:00
|
|
|
case 'G': // Absolute horizontal position
|
|
|
|
info.dwCursorPosition.x = count - 1 // windows origin is 0, unix is 1
|
2015-09-29 16:01:17 +03:00
|
|
|
}
|
|
|
|
SetConsoleCursorPosition(&info.dwCursorPosition)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-09-29 12:49:58 +03:00
|
|
|
switch r {
|
|
|
|
case 'J':
|
2015-09-29 16:01:17 +03:00
|
|
|
killLines()
|
2015-09-29 12:49:58 +03:00
|
|
|
case 'K':
|
|
|
|
eraseLine()
|
|
|
|
case 'm':
|
|
|
|
color := word(0)
|
|
|
|
for _, item := range arg {
|
|
|
|
var c int
|
|
|
|
c, err = strconv.Atoi(item)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteString("[" + strings.Join(arg, ";") + "m")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if c >= 30 && c < 40 {
|
2015-09-29 18:28:12 +03:00
|
|
|
color ^= COLOR_FINTENSITY
|
2015-09-29 12:49:58 +03:00
|
|
|
color |= ColorTableFg[c-30]
|
2015-09-29 16:01:17 +03:00
|
|
|
} else if c >= 40 && c < 50 {
|
2015-09-29 18:28:12 +03:00
|
|
|
color ^= COLOR_BINTENSITY
|
2015-09-29 16:01:17 +03:00
|
|
|
color |= ColorTableBg[c-40]
|
|
|
|
} else if c == 4 {
|
2015-09-29 18:28:12 +03:00
|
|
|
color |= COMMON_LVB_UNDERSCORE | ColorTableFg[7]
|
2017-10-02 06:59:06 +03:00
|
|
|
} else if c == 1 {
|
|
|
|
color |= COMMON_LVB_BOLD | COLOR_FINTENSITY
|
2015-09-29 16:01:17 +03:00
|
|
|
} else { // unknown code treat as reset
|
2015-09-29 12:49:58 +03:00
|
|
|
color = ColorTableFg[7]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
kernel.SetConsoleTextAttribute(stdout, uintptr(color))
|
2015-09-29 16:01:17 +03:00
|
|
|
case '\007': // set title
|
2015-09-29 12:49:58 +03:00
|
|
|
case ';':
|
|
|
|
if len(arg) == 0 || arg[len(arg)-1] != "" {
|
|
|
|
arg = append(arg, "")
|
2015-09-29 16:01:17 +03:00
|
|
|
*argptr = arg
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
2015-09-29 16:01:17 +03:00
|
|
|
return true
|
2015-09-29 12:49:58 +03:00
|
|
|
default:
|
|
|
|
if len(arg) == 0 {
|
|
|
|
arg = append(arg, "")
|
|
|
|
}
|
|
|
|
arg[len(arg)-1] += string(r)
|
|
|
|
*argptr = arg
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
*argptr = nil
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *ANSIWriter) Write(b []byte) (int, error) {
|
|
|
|
a.Lock()
|
|
|
|
defer a.Unlock()
|
|
|
|
|
|
|
|
off := 0
|
|
|
|
for len(b) > off {
|
|
|
|
r, size := utf8.DecodeRune(b[off:])
|
|
|
|
if size == 0 {
|
|
|
|
return off, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
off += size
|
2016-07-23 19:39:44 +03:00
|
|
|
a.ctx.process(r)
|
2015-09-29 12:49:58 +03:00
|
|
|
}
|
2016-07-23 19:39:44 +03:00
|
|
|
a.ctx.Flush()
|
2015-09-29 12:49:58 +03:00
|
|
|
return off, nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 16:01:17 +03:00
|
|
|
func killLines() error {
|
|
|
|
sbi, err := GetConsoleScreenBufferInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
size := (sbi.dwCursorPosition.y - sbi.dwSize.y) * sbi.dwSize.x
|
|
|
|
size += sbi.dwCursorPosition.x
|
|
|
|
|
|
|
|
var written int
|
|
|
|
kernel.FillConsoleOutputAttribute(stdout, uintptr(ColorTableFg[7]),
|
|
|
|
uintptr(size),
|
|
|
|
sbi.dwCursorPosition.ptr(),
|
|
|
|
uintptr(unsafe.Pointer(&written)),
|
|
|
|
)
|
|
|
|
return kernel.FillConsoleOutputCharacterW(stdout, uintptr(' '),
|
|
|
|
uintptr(size),
|
|
|
|
sbi.dwCursorPosition.ptr(),
|
|
|
|
uintptr(unsafe.Pointer(&written)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-09-29 12:49:58 +03:00
|
|
|
func eraseLine() error {
|
|
|
|
sbi, err := GetConsoleScreenBufferInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 16:01:17 +03:00
|
|
|
size := sbi.dwSize.x
|
|
|
|
sbi.dwCursorPosition.x = 0
|
2015-09-29 12:49:58 +03:00
|
|
|
var written int
|
|
|
|
return kernel.FillConsoleOutputCharacterW(stdout, uintptr(' '),
|
2015-09-29 16:01:17 +03:00
|
|
|
uintptr(size),
|
2015-09-29 12:49:58 +03:00
|
|
|
sbi.dwCursorPosition.ptr(),
|
|
|
|
uintptr(unsafe.Pointer(&written)),
|
|
|
|
)
|
|
|
|
}
|