diff --git a/Terminal.Gui/Views/TableView.cs b/Terminal.Gui/Views/TableView.cs index e8e15fe07..df4072e75 100644 --- a/Terminal.Gui/Views/TableView.cs +++ b/Terminal.Gui/Views/TableView.cs @@ -406,12 +406,13 @@ namespace Terminal.Gui { // Set color scheme based on whether the current cell is the selected one bool isSelectedCell = IsSelected(current.Column.Ordinal,rowToRender); - Driver.SetAttribute (isSelectedCell ? ColorScheme.HotFocus : ColorScheme.Normal); - var val = Table.Rows [rowToRender][current.Column]; // Render the (possibly truncated) cell value var representation = GetRepresentation(val,colStyle); + var scheme = (colStyle?.ColorGetter?.Invoke(val)) ?? ColorScheme; + + Driver.SetAttribute (isSelectedCell ? scheme.HotFocus : scheme.Normal); Driver.AddStr (TruncateOrPad(val,representation, current.Width, colStyle)); @@ -1134,6 +1135,12 @@ namespace Terminal.Gui { /// public Func RepresentationGetter; + /// + /// Defines a delegate for returning a custom color scheme per cell based on cell values. + /// Return null for the default + /// + public Func ColorGetter; + /// /// Defines the format for values e.g. "yyyy-MM-dd" for dates /// diff --git a/UICatalog/Scenarios/TableEditor.cs b/UICatalog/Scenarios/TableEditor.cs index 229cc7c71..7045aac3a 100644 --- a/UICatalog/Scenarios/TableEditor.cs +++ b/UICatalog/Scenarios/TableEditor.cs @@ -24,6 +24,8 @@ namespace UICatalog.Scenarios { private MenuItem miFullRowSelect; private MenuItem miExpandLastColumn; + ColorScheme redColorScheme; + public override void Setup () { Win.Title = this.GetName(); @@ -60,8 +62,6 @@ namespace UICatalog.Scenarios { }); Top.Add (menu); - - var statusBar = new StatusBar (new StatusItem [] { new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)), new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()), @@ -88,6 +88,13 @@ namespace UICatalog.Scenarios { tableView.KeyPress += TableViewKeyPress; SetupScrollBar(); + + redColorScheme = new ColorScheme(){ + Disabled = Colors.Base.Disabled, + HotFocus = Colors.Base.HotFocus, + Focus = Colors.Base.Focus, + Normal = Application.Driver.MakeAttribute(Color.Red,Color.Blue) + }; } private void SetupScrollBar () @@ -268,7 +275,15 @@ namespace UICatalog.Scenarios { // align positive values left TextAlignment.Left: // not a double - TextAlignment.Left + TextAlignment.Left, + + ColorGetter = (v)=>v is double d ? + // color 0 and negative values red + d <= 0.0000001 ? redColorScheme : + // use normal scheme for positive values + null: + // not a double + null }; tableView.Style.ColumnStyles.Add(tableView.Table.Columns["DateCol"],dateFormatStyle);