Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline change color on PowerShell #112

Open
knqyf263 opened this issue Mar 19, 2017 · 5 comments
Open

readline change color on PowerShell #112

knqyf263 opened this issue Mar 19, 2017 · 5 comments

Comments

@knqyf263
Copy link

knqyf263 commented Mar 19, 2017

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

readline

How can I keep the background color?

@hanshasselberg
Copy link

I have the same problem. I found an issues that seems related: elm/compiler#1334.

@tom-seddon
Copy link

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)),

@jcburley
Copy link

@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.

@tom-seddon
Copy link

tom-seddon commented Oct 21, 2018

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

@benpye
Copy link

benpye commented Nov 17, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants