diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs index 99e560044d..0b11949a94 100644 --- a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs @@ -39,7 +39,7 @@ internal set } } - public override bool SupportsTrueColor => false; + public override bool SupportsTrueColor => true; /// public override bool EnsureCursorVisibility () { return false; } @@ -200,8 +200,12 @@ public override void Suspend () if (!RunningUnitTests) { Platform.Suspend (); - Curses.Window.Standard.redrawwin (); - Curses.refresh (); + + if (Force16Colors) + { + Curses.Window.Standard.redrawwin (); + Curses.refresh (); + } } StartReportingMouseMoves (); @@ -214,74 +218,232 @@ public override void UpdateCursor () if (!RunningUnitTests && Col >= 0 && Col < Cols && Row >= 0 && Row < Rows) { Curses.move (Row, Col); - Curses.raw (); - Curses.noecho (); - Curses.refresh (); + + if (Force16Colors) + { + Curses.raw (); + Curses.noecho (); + Curses.refresh (); + } } } - public override void UpdateScreen () { - for (var row = 0; row < Rows; row++) + if (Force16Colors) { - if (!_dirtyLines [row]) + for (var row = 0; row < Rows; row++) { - continue; - } - - _dirtyLines [row] = false; - - for (var col = 0; col < Cols; col++) - { - if (Contents [row, col].IsDirty == false) + if (!_dirtyLines [row]) { continue; } - if (RunningUnitTests) + _dirtyLines [row] = false; + + for (var col = 0; col < Cols; col++) { - // In unit tests, we don't want to actually write to the screen. - continue; - } + if (Contents [row, col].IsDirty == false) + { + continue; + } - Curses.attrset (Contents [row, col].Attribute.GetValueOrDefault ().PlatformColor); + if (RunningUnitTests) + { + // In unit tests, we don't want to actually write to the screen. + continue; + } - Rune rune = Contents [row, col].Rune; + Curses.attrset (Contents [row, col].Attribute.GetValueOrDefault ().PlatformColor); - if (rune.IsBmp) - { - // BUGBUG: CursesDriver doesn't render CharMap correctly for wide chars (and other Unicode) - Curses is doing something funky with glyphs that report GetColums() of 1 yet are rendered wide. E.g. 0x2064 (invisible times) is reported as 1 column but is rendered as 2. WindowsDriver & NetDriver correctly render this as 1 column, overlapping the next cell. - if (rune.GetColumns () < 2) + Rune rune = Contents [row, col].Rune; + + if (rune.IsBmp) { - Curses.mvaddch (row, col, rune.Value); + // BUGBUG: CursesDriver doesn't render CharMap correctly for wide chars (and other Unicode) - Curses is doing something funky with glyphs that report GetColums() of 1 yet are rendered wide. E.g. 0x2064 (invisible times) is reported as 1 column but is rendered as 2. WindowsDriver & NetDriver correctly render this as 1 column, overlapping the next cell. + if (rune.GetColumns () < 2) + { + Curses.mvaddch (row, col, rune.Value); + } + else /*if (col + 1 < Cols)*/ + { + Curses.mvaddwstr (row, col, rune.ToString ()); + } } - else /*if (col + 1 < Cols)*/ + else { Curses.mvaddwstr (row, col, rune.ToString ()); + + if (rune.GetColumns () > 1 && col + 1 < Cols) + { + // TODO: This is a hack to deal with non-BMP and wide characters. + //col++; + Curses.mvaddch (row, ++col, '*'); + } } } - else + } + + if (!RunningUnitTests) + { + Curses.move (Row, Col); + _window.wrefresh (); + } + } + else + { + if (RunningUnitTests + || Console.WindowHeight < 1 + || Contents.Length != Rows * Cols + || Rows != Console.WindowHeight) + { + return; + } + + var top = 0; + var left = 0; + int rows = Rows; + int cols = Cols; + var output = new StringBuilder (); + Attribute? redrawAttr = null; + int lastCol = -1; + + CursorVisibility? savedVisibility = _currentCursorVisibility; + SetCursorVisibility (CursorVisibility.Invisible); + + for (int row = top; row < rows; row++) + { + if (Console.WindowHeight < 1) + { + return; + } + + if (!_dirtyLines [row]) + { + continue; + } + + if (!SetCursorPosition (0, row)) { - Curses.mvaddwstr (row, col, rune.ToString ()); + return; + } + + _dirtyLines [row] = false; + output.Clear (); - if (rune.GetColumns () > 1 && col + 1 < Cols) + for (int col = left; col < cols; col++) + { + lastCol = -1; + var outputWidth = 0; + + for (; col < cols; col++) { - // TODO: This is a hack to deal with non-BMP and wide characters. - //col++; - Curses.mvaddch (row, ++col, '*'); + if (!Contents [row, col].IsDirty) + { + if (output.Length > 0) + { + WriteToConsole (output, ref lastCol, row, ref outputWidth); + } + else if (lastCol == -1) + { + lastCol = col; + } + + if (lastCol + 1 < cols) + { + lastCol++; + } + + continue; + } + + if (lastCol == -1) + { + lastCol = col; + } + + Attribute attr = Contents [row, col].Attribute.Value; + + // Performance: Only send the escape sequence if the attribute has changed. + if (attr != redrawAttr) + { + redrawAttr = attr; + + output.Append ( + EscSeqUtils.CSI_SetForegroundColorRGB ( + attr.Foreground.R, + attr.Foreground.G, + attr.Foreground.B + ) + ); + + output.Append ( + EscSeqUtils.CSI_SetBackgroundColorRGB ( + attr.Background.R, + attr.Background.G, + attr.Background.B + ) + ); + } + + outputWidth++; + Rune rune = Contents [row, col].Rune; + output.Append (rune); + + if (Contents [row, col].CombiningMarks.Count > 0) + { + // AtlasEngine does not support NON-NORMALIZED combining marks in a way + // compatible with the driver architecture. Any CMs (except in the first col) + // are correctly combined with the base char, but are ALSO treated as 1 column + // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`. + // + // For now, we just ignore the list of CMs. + //foreach (var combMark in Contents [row, col].CombiningMarks) { + // output.Append (combMark); + //} + // WriteToConsole (output, ref lastCol, row, ref outputWidth); + } + else if (rune.IsSurrogatePair () && rune.GetColumns () < 2) + { + WriteToConsole (output, ref lastCol, row, ref outputWidth); + SetCursorPosition (col - 1, row); + } + + Contents [row, col].IsDirty = false; } } + + if (output.Length > 0) + { + SetCursorPosition (lastCol, row); + Console.Write (output); + } } - } - if (!RunningUnitTests) - { - Curses.move (Row, Col); - _window.wrefresh (); + SetCursorPosition (0, 0); + + _currentCursorVisibility = savedVisibility; + + void WriteToConsole (StringBuilder output, ref int lastCol, int row, ref int outputWidth) + { + SetCursorPosition (lastCol, row); + Console.Write (output); + output.Clear (); + lastCol += outputWidth; + outputWidth = 0; + } } } + private bool SetCursorPosition (int col, int row) + { + // + 1 is needed because non-Windows is based on 1 instead of 0 and + // Console.CursorTop/CursorLeft isn't reliable. + Console.Out.Write (EscSeqUtils.CSI_SetCursorPosition (row + 1, col + 1)); + + return true; + } + internal override void End () { StopReportingMouseMoves (); @@ -405,7 +567,11 @@ internal override MainLoop Init () if (!RunningUnitTests) { Curses.CheckWinChange (); - Curses.refresh (); + + if (Force16Colors) + { + Curses.refresh (); + } } return new MainLoop (_mainLoopDriver); @@ -852,7 +1018,8 @@ bool IsButtonClickedOrDoubleClicked (MouseFlags flag) /// private static Attribute MakeColor (short foreground, short background) { - var v = (short)((ushort)foreground | (background << 4)); + //var v = (short)((ushort)foreground | (background << 4)); + var v = (short)(((ushort)(foreground & 0xffff) << 16) | (background & 0xffff)); // TODO: for TrueColor - Use InitExtendedPair Curses.InitColorPair (v, foreground, background); @@ -872,7 +1039,7 @@ private static Attribute MakeColor (short foreground, short background) /// public override Attribute MakeColor (in Color foreground, in Color background) { - if (!RunningUnitTests) + if (!RunningUnitTests && Force16Colors) { return MakeColor ( ColorNameToCursesColorNumber (foreground.GetClosestNamedColor16 ()), diff --git a/UnitTests/ConsoleDrivers/DriverColorTests.cs b/UnitTests/ConsoleDrivers/DriverColorTests.cs index 090717e97a..8bebace1e8 100644 --- a/UnitTests/ConsoleDrivers/DriverColorTests.cs +++ b/UnitTests/ConsoleDrivers/DriverColorTests.cs @@ -60,7 +60,7 @@ public void SetColors_Changes_Colors (Type driverType) //[InlineData (typeof (ANSIDriver), true)] [InlineData (typeof (WindowsDriver), true)] - [InlineData (typeof (CursesDriver), false)] + [InlineData (typeof (CursesDriver), true)] public void SupportsTrueColor_Defaults (Type driverType, bool expectedSetting) { var driver = (ConsoleDriver)Activator.CreateInstance (driverType);