detect default fg/bg colors on Windows

This commit is contained in:
Adam D. Ruppe 2018-11-10 15:02:20 -05:00
parent 93a7aa324a
commit 5854599551
1 changed files with 14 additions and 4 deletions

View File

@ -794,6 +794,14 @@ http://msdn.microsoft.com/en-us/library/windows/desktop/ms683193%28v=vs.85%29.as
if(GetConsoleScreenBufferInfo(hConsole, &originalSbi) == 0) if(GetConsoleScreenBufferInfo(hConsole, &originalSbi) == 0)
throw new Exception("not a user-interactive terminal"); throw new Exception("not a user-interactive terminal");
defaultForegroundColor = cast(Color) (originalSbi.wAttributes & 0x0f);
defaultBackgroundColor = cast(Color) ((originalSbi.wAttributes >> 4) & 0x0f);
}
version(Windows) {
private Color defaultBackgroundColor = Color.black;
private Color defaultForegroundColor = Color.white;
} }
// only use this if you are sure you know what you want, since the terminal is a shared resource you generally really want to reset it to normal when you leave... // only use this if you are sure you know what you want, since the terminal is a shared resource you generally really want to reset it to normal when you leave...
@ -930,20 +938,20 @@ http://msdn.microsoft.com/en-us/library/windows/desktop/ms683193%28v=vs.85%29.as
// this isn't necessarily right but meh // this isn't necessarily right but meh
if(background == Color.DEFAULT) if(background == Color.DEFAULT)
setTob = Color.black; setTob = defaultBackgroundColor;
if(foreground == Color.DEFAULT) if(foreground == Color.DEFAULT)
setTof = Color.white; setTof = defaultForegroundColor;
if(force == ForceOption.alwaysSend || reverseVideo != this.reverseVideo || foreground != _currentForeground || background != _currentBackground) { if(force == ForceOption.alwaysSend || reverseVideo != this.reverseVideo || foreground != _currentForeground || background != _currentBackground) {
flush(); // if we don't do this now, the buffering can screw up the colors... flush(); // if we don't do this now, the buffering can screw up the colors...
if(reverseVideo) { if(reverseVideo) {
if(background == Color.DEFAULT) if(background == Color.DEFAULT)
setTof = Color.black; setTof = defaultBackgroundColor;
else else
setTof = cast(ushort) background | (foreground & Bright); setTof = cast(ushort) background | (foreground & Bright);
if(background == Color.DEFAULT) if(background == Color.DEFAULT)
setTob = Color.white; setTob = defaultForegroundColor;
else else
setTob = cast(ushort) (foreground & ~Bright); setTob = cast(ushort) (foreground & ~Bright);
} }
@ -2704,6 +2712,8 @@ void main() {
terminal.write("test some long string to see if it wraps or what because i dont really know what it is going to do so i just want to test i think it will wrap but gotta be sure lolololololololol"); terminal.write("test some long string to see if it wraps or what because i dont really know what it is going to do so i just want to test i think it will wrap but gotta be sure lolololololololol");
terminal.writefln("%d %d", terminal.cursorX, terminal.cursorY); terminal.writefln("%d %d", terminal.cursorX, terminal.cursorY);
terminal.color(Color.DEFAULT, Color.DEFAULT);
int centerX = terminal.width / 2; int centerX = terminal.width / 2;
int centerY = terminal.height / 2; int centerY = terminal.height / 2;