Skip to content

Commit

Permalink
Merge pull request #3770 from BDisp/v2_2798_add-true-color-to-cursesd…
Browse files Browse the repository at this point in the history
…river

Fixes #2798. Add TrueColor support to CursesDriver.
  • Loading branch information
tig authored Oct 11, 2024
2 parents 9cb985d + b98c6fa commit bc1aeaa
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 44 deletions.
253 changes: 210 additions & 43 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal set
}
}

public override bool SupportsTrueColor => false;
public override bool SupportsTrueColor => true;

/// <inheritdoc/>
public override bool EnsureCursorVisibility () { return false; }
Expand Down Expand Up @@ -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 ();
Expand All @@ -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 ();
Expand Down Expand Up @@ -405,7 +567,11 @@ internal override MainLoop Init ()
if (!RunningUnitTests)
{
Curses.CheckWinChange ();
Curses.refresh ();

if (Force16Colors)
{
Curses.refresh ();
}
}

return new MainLoop (_mainLoopDriver);
Expand Down Expand Up @@ -852,7 +1018,8 @@ bool IsButtonClickedOrDoubleClicked (MouseFlags flag)
/// <returns></returns>
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);
Expand All @@ -872,7 +1039,7 @@ private static Attribute MakeColor (short foreground, short background)
/// </remarks>
public override Attribute MakeColor (in Color foreground, in Color background)
{
if (!RunningUnitTests)
if (!RunningUnitTests && Force16Colors)
{
return MakeColor (
ColorNameToCursesColorNumber (foreground.GetClosestNamedColor16 ()),
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/ConsoleDrivers/DriverColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bc1aeaa

Please sign in to comment.