Fix cmd background on Windows

This commit is contained in:
Ben Pye 2018-11-17 18:14:32 +00:00
parent 2972be24d4
commit 5ff4ccac79
No known key found for this signature in database
GPG Key ID: D3033D0EAE5351B1
1 changed files with 25 additions and 10 deletions

View File

@ -76,11 +76,23 @@ type ANSIWriterCtx struct {
arg []string arg []string
target *bufio.Writer target *bufio.Writer
wantFlush bool wantFlush bool
defaultAttributes word
}
func getTextAttributes() word {
sbi, err := GetConsoleScreenBufferInfo()
if err != nil {
// Hopefully sane default
return ColorTableFg[7] | ColorTableBg[0]
}
return sbi.wAttributes
} }
func NewANSIWriterCtx(target io.Writer) *ANSIWriterCtx { func NewANSIWriterCtx(target io.Writer) *ANSIWriterCtx {
return &ANSIWriterCtx{ return &ANSIWriterCtx{
target: bufio.NewWriter(target), target: bufio.NewWriter(target),
defaultAttributes: getTextAttributes(),
} }
} }
@ -148,7 +160,7 @@ func (a *ANSIWriterCtx) ioloopEscSeq(w *bufio.Writer, r rune, argptr *[]string)
case 'K': case 'K':
eraseLine() eraseLine()
case 'm': case 'm':
color := word(0) color := getTextAttributes()
for _, item := range arg { for _, item := range arg {
var c int var c int
c, err = strconv.Atoi(item) c, err = strconv.Atoi(item)
@ -156,13 +168,16 @@ func (a *ANSIWriterCtx) ioloopEscSeq(w *bufio.Writer, r rune, argptr *[]string)
w.WriteString("[" + strings.Join(arg, ";") + "m") w.WriteString("[" + strings.Join(arg, ";") + "m")
break break
} }
if c >= 30 && c < 40 { if c >= 30 && c < 38 {
color &= ^word(COLOR_FRED | COLOR_FGREEN | COLOR_FBLUE | COLOR_FINTENSITY)
color ^= COLOR_FINTENSITY color ^= COLOR_FINTENSITY
color |= ColorTableFg[c-30] color |= ColorTableFg[c-30]
} else if c >= 40 && c < 50 { } else if c >= 40 && c < 48 {
color &= ^word(COLOR_BRED | COLOR_BGREEN | COLOR_BBLUE | COLOR_BINTENSITY)
color ^= COLOR_BINTENSITY color ^= COLOR_BINTENSITY
color |= ColorTableBg[c-40] color |= ColorTableBg[c-40]
} else if c == 4 { } else if c == 4 {
color &= ^word(COLOR_FRED | COLOR_FGREEN | COLOR_FBLUE | COLOR_FINTENSITY)
color |= COMMON_LVB_UNDERSCORE | ColorTableFg[7] color |= COMMON_LVB_UNDERSCORE | ColorTableFg[7]
} else if c == 1 { } else if c == 1 {
color |= COMMON_LVB_BOLD | COLOR_FINTENSITY color |= COMMON_LVB_BOLD | COLOR_FINTENSITY
@ -220,7 +235,7 @@ func killLines() error {
size += sbi.dwCursorPosition.x size += sbi.dwCursorPosition.x
var written int var written int
kernel.FillConsoleOutputAttribute(stdout, uintptr(ColorTableFg[7]), kernel.FillConsoleOutputAttribute(stdout, uintptr(sbi.wAttributes),
uintptr(size), uintptr(size),
sbi.dwCursorPosition.ptr(), sbi.dwCursorPosition.ptr(),
uintptr(unsafe.Pointer(&written)), uintptr(unsafe.Pointer(&written)),