mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 00:46:39 +01:00
Merge pull request #1357 from tznind/table-colors
Added support for coloring cells in TableView
This commit is contained in:
@@ -116,6 +116,53 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable xUnit1013 // Public method should be marked as test
|
||||
/// <summary>
|
||||
/// Verifies the console was rendered using the given <paramref name="expectedColors"/> at the given locations.
|
||||
/// Pass a bitmap of indexes into <paramref name="expectedColors"/> as <paramref name="expectedLook"/> and the
|
||||
/// test method will verify those colors were used in the row/col of the console during rendering
|
||||
/// </summary>
|
||||
/// <param name="expectedLook">Numbers between 0 and 9 for each row/col of the console. Must be valid indexes of <paramref name="expectedColors"/></param>
|
||||
/// <param name="expectedColors"></param>
|
||||
public static void AssertDriverColorsAre (string expectedLook, Attribute[] expectedColors)
|
||||
{
|
||||
#pragma warning restore xUnit1013 // Public method should be marked as test
|
||||
|
||||
if(expectedColors.Length > 10) {
|
||||
throw new ArgumentException ("This method only works for UIs that use at most 10 colors");
|
||||
}
|
||||
|
||||
expectedLook = expectedLook.Trim ();
|
||||
var driver = ((FakeDriver)Application.Driver);
|
||||
|
||||
var contents = driver.Contents;
|
||||
|
||||
int r = 0;
|
||||
foreach(var line in expectedLook.Split ('\n').Select(l=>l.Trim())) {
|
||||
|
||||
for (int c = 0; c < line.Length; c++) {
|
||||
|
||||
int val = contents [r, c, 1];
|
||||
|
||||
var match = expectedColors.Where (e => e.Value == val).ToList ();
|
||||
if (match.Count == 0) {
|
||||
throw new Exception ($"Unexpected color {val} was used at row {r} and col {c}. Color value was {val} (expected colors were {string.Join (",", expectedColors.Select (c => c.Value))})");
|
||||
} else if (match.Count > 1) {
|
||||
throw new ArgumentException ($"Bad value for expectedColors, {match.Count} Attributes had the same Value");
|
||||
}
|
||||
|
||||
var colorUsed = Array.IndexOf(expectedColors,match[0]).ToString()[0];
|
||||
var userExpected = line [c];
|
||||
|
||||
if( colorUsed != userExpected) {
|
||||
throw new Exception ($"Colors used did not match expected at row {r} and col {c}. Color index used was {colorUsed} but test expected {userExpected} (these are indexes into the expectedColors array)");
|
||||
}
|
||||
}
|
||||
|
||||
r++;
|
||||
}
|
||||
}
|
||||
|
||||
#region Screen to Graph Tests
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -495,6 +495,54 @@ namespace Terminal.Gui.Views {
|
||||
Application.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TableView_ColorsTest_ColorGetter ()
|
||||
{
|
||||
var tv = SetUpMiniTable ();
|
||||
|
||||
tv.Style.ExpandLastColumn = false;
|
||||
tv.Style.InvertSelectedCellFirstCharacter = true;
|
||||
|
||||
// width exactly matches the max col widths
|
||||
tv.Bounds = new Rect (0, 0, 5, 4);
|
||||
|
||||
// Create a style for column B
|
||||
var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
|
||||
|
||||
// when B is 2 use the custom highlight colour
|
||||
ColorScheme cellHighlight = new ColorScheme () { Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray) };
|
||||
bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
|
||||
|
||||
tv.Redraw (tv.Bounds);
|
||||
|
||||
string expected = @"
|
||||
┌─┬─┐
|
||||
│A│B│
|
||||
├─┼─┤
|
||||
│1│2│
|
||||
";
|
||||
GraphViewTests.AssertDriverContentsAre (expected, output);
|
||||
|
||||
string expectedColors = @"
|
||||
00000
|
||||
00000
|
||||
00000
|
||||
01020
|
||||
";
|
||||
var invertedNormalColor = Application.Driver.MakeAttribute (tv.ColorScheme.Normal.Background, tv.ColorScheme.Normal.Foreground);
|
||||
|
||||
GraphViewTests.AssertDriverColorsAre (expectedColors, new Attribute [] {
|
||||
// 0
|
||||
tv.ColorScheme.Normal,
|
||||
// 1
|
||||
invertedNormalColor,
|
||||
// 2
|
||||
cellHighlight.Normal});
|
||||
|
||||
// Shutdown must be called to safely clean up Application if Init has been called
|
||||
Application.Shutdown ();
|
||||
}
|
||||
|
||||
private TableView SetUpMiniTable ()
|
||||
{
|
||||
|
||||
|
||||
Reference in New Issue
Block a user