readline icon indicating copy to clipboard operation
readline copied to clipboard

readline change color on PowerShell

Open knqyf263 opened this issue 7 years ago • 5 comments

Thanks for awesome library. I use readline-demo on Windows PowerShell.

readline

How can I keep the background color?

knqyf263 avatar Mar 19 '17 09:03 knqyf263

I have the same problem. I found an issues that seems related: https://github.com/elm-lang/elm-compiler/issues/1334.

hanshasselberg avatar Apr 19 '17 08:04 hanshasselberg

It's a bug - the code is ignoring the current background colour settings. Here's a first draft patch. I don't use PowerShell, but I use black-on-white in the command prompt and so I was getting a bit annoyed by this. (Command line node.js programs are terrible for doing this too, for some reason.)

I fixed another bug (I think?) in the ESC m logic, too - it was treating colours 30-49 as foreground colours, but in fact only 30-37 are valid.

I'm going to try to turn this into a PR - but this is my first time with go, and so far it's feeling like there's a very good chance I might just give up in frustration before finishing. So I thought I'd post this patch just in case that happens :)

--Tom

diff --git a/vendor/github.com/chzyer/readline/ansi_windows.go b/vendor/github.com/chzyer/readline/ansi_windows.go
index da106b5..f419283 100644
--- a/vendor/github.com/chzyer/readline/ansi_windows.go
+++ b/vendor/github.com/chzyer/readline/ansi_windows.go
@@ -75,11 +75,23 @@ type ANSIWriterCtx struct {
 	arg       []string
 	target    *bufio.Writer
 	wantFlush bool
+	defaultAttributes word
+}
+
+func getTextAttributes() word {
+	sbi,err:=GetConsoleScreenBufferInfo()
+	if err!=nil {
+		// hopefully not a completely dumb default.
+		return ColorTableFg[7]|ColorTableBg[0];
+	}
+
+	return sbi.wAttributes
 }
 
 func NewANSIWriterCtx(target io.Writer) *ANSIWriterCtx {
 	return &ANSIWriterCtx{
 		target: bufio.NewWriter(target),
+		defaultAttributes:getTextAttributes(),
 	}
 }
 
@@ -147,7 +159,8 @@ func (a *ANSIWriterCtx) ioloopEscSeq(w *bufio.Writer, r rune, argptr *[]string)
 	case 'K':
 		eraseLine()
 	case 'm':
-		color := word(0)
+		color := getTextAttributes()
+		
 		for _, item := range arg {
 			var c int
 			c, err = strconv.Atoi(item)
@@ -155,21 +168,25 @@ func (a *ANSIWriterCtx) ioloopEscSeq(w *bufio.Writer, r rune, argptr *[]string)
 				w.WriteString("[" + strings.Join(arg, ";") + "m")
 				break
 			}
-			if c >= 30 && c < 40 {
-				color ^= COLOR_FINTENSITY
+			if c >= 30 && c < 38 {
+				color&=^word(COLOR_FRED|COLOR_FGREEN|COLOR_FBLUE|COLOR_FINTENSITY);
+				color |= COLOR_FINTENSITY
 				color |= ColorTableFg[c-30]
-			} else if c >= 40 && c < 50 {
-				color ^= COLOR_BINTENSITY
+			} else if c >= 40 && c < 48 {
+				color&=^word(COLOR_BRED|COLOR_BGREEN|COLOR_BBLUE|COLOR_BINTENSITY);
+				color |= COLOR_BINTENSITY
 				color |= ColorTableBg[c-40]
 			} else if c == 4 {
+				color&=^word(COLOR_FRED|COLOR_FGREEN|COLOR_FBLUE|COLOR_FINTENSITY);
 				color |= COMMON_LVB_UNDERSCORE | ColorTableFg[7]
 			} else { // unknown code treat as reset
-				color = ColorTableFg[7]
+				color = a.defaultAttributes
 			}
 		}
 		if err != nil {
 			break
 		}
+
 		kernel.SetConsoleTextAttribute(stdout, uintptr(color))
 	case '\007': // set title
 	case ';':
@@ -212,12 +229,11 @@ func killLines() error {
 	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]),
+	kernel.FillConsoleOutputAttribute(stdout, uintptr(sbi.wAttributes),
 		uintptr(size),
 		sbi.dwCursorPosition.ptr(),
 		uintptr(unsafe.Pointer(&written)),

tom-seddon avatar Sep 02 '17 16:09 tom-seddon

@tom-seddon -- what's going on with your patch? Seems like some kind of fix is needed, but (while I'm comfortable with Go now) I don't know readline stuff well enough to say whether your patch is good.

Still, let me know if you need help with this.

jcburley avatar Oct 21 '18 02:10 jcburley

I haven't done any more on this - I was using Go as part of some client work, and haven't used it since. And at some point the project's Go components dropped Windows suppport, so I didn't have any need for a patched version any more.

I don't remember finding any issues with my patched version while I was using it though.

Thanks,

--Tom

tom-seddon avatar Oct 21 '18 11:10 tom-seddon

I just submitted a PR for this as #161 , the changes by @tom-seddon appear to work!

benpye avatar Nov 17 '18 18:11 benpye