From 30251c8bafbf31c64ddd58655c438053f2cb0e6f Mon Sep 17 00:00:00 2001 From: tznind Date: Mon, 14 Dec 2020 14:01:29 +0000 Subject: [PATCH] Fixed alignment padding and made better use of Bounds.Width --- Terminal.Gui/Views/TableView.cs | 45 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/Terminal.Gui/Views/TableView.cs b/Terminal.Gui/Views/TableView.cs index be09d4abe..eccc83605 100644 --- a/Terminal.Gui/Views/TableView.cs +++ b/Terminal.Gui/Views/TableView.cs @@ -230,7 +230,7 @@ namespace Terminal.Gui.Views { line++; } - RenderHeaderMidline(line,bounds.Width,columnsToRender); + RenderHeaderMidline(line,columnsToRender); line++; if(Style.ShowHorizontalHeaderUnderline){ @@ -251,7 +251,7 @@ namespace Terminal.Gui.Views { if ( Table == null || rowToRender >= Table.Rows.Count || rowToRender < 0) continue; - RenderRow(line,bounds.Width,rowToRender,columnsToRender); + RenderRow(line,rowToRender,columnsToRender); } } @@ -311,12 +311,12 @@ namespace Terminal.Gui.Views { } } - private void RenderHeaderMidline(int row,int availableWidth, ColumnToRender[] columnsToRender) + private void RenderHeaderMidline(int row, ColumnToRender[] columnsToRender) { // Renders something like: // │ArithmeticComparator│chi │Healthboard│Interpretation│Labnumber│ - ClearLine(row,availableWidth); + ClearLine(row,Bounds.Width); //render start of line if(style.ShowVerticalHeaderLines) @@ -325,7 +325,7 @@ namespace Terminal.Gui.Views { for(int i =0 ; i @@ -348,15 +348,14 @@ namespace Terminal.Gui.Views { /// /// /// - /// - private int GetCellWidth (ColumnToRender [] columnsToRender, int i,int availableWidth) + private int GetCellWidth (ColumnToRender [] columnsToRender, int i) { var current = columnsToRender[i]; var next = i+1 < columnsToRender.Length ? columnsToRender[i+1] : null; if(next == null) { // cell can fill to end of the line - return availableWidth - current.X; + return Bounds.Width - current.X; } else { // cell can fill up to next cell start @@ -393,7 +392,7 @@ namespace Terminal.Gui.Views { } } - private void RenderRow(int row, int availableWidth, int rowToRender, ColumnToRender[] columnsToRender) + private void RenderRow(int row, int rowToRender, ColumnToRender[] columnsToRender) { //render start of line if(style.ShowVerticalCellLines) @@ -403,7 +402,7 @@ namespace Terminal.Gui.Views { for(int i=0;i< columnsToRender.Length ;i++) { var current = columnsToRender[i]; - var availableWidthForCell = GetCellWidth(columnsToRender,i,availableWidth); + var availableWidthForCell = GetCellWidth(columnsToRender,i); var colStyle = Style.GetColumnStyleIfAny(current.Column); @@ -428,7 +427,7 @@ namespace Terminal.Gui.Views { //render end of line if(style.ShowVerticalCellLines) - AddRune(availableWidth-1,row,Driver.VLine); + AddRune(Bounds.Width-1,row,Driver.VLine); } private void RenderSeparator(int col, int row,bool isHeader) @@ -456,7 +455,7 @@ namespace Terminal.Gui.Views { /// /// Optional style indicating custom alignment for the cell /// - private ustring TruncateOrPad (object originalCellValue,string representation, int availableHorizontalSpace, ColumnStyle colStyle) + private string TruncateOrPad (object originalCellValue,string representation, int availableHorizontalSpace, ColumnStyle colStyle) { if (string.IsNullOrEmpty (representation)) return representation; @@ -465,29 +464,23 @@ namespace Terminal.Gui.Views { if(representation.Length < availableHorizontalSpace) { // pad it out with spaces to the given alignment - int toPad = availableHorizontalSpace - representation.Length; + int toPad = availableHorizontalSpace - (representation.Length+1 /*leave 1 space for cell boundary*/); switch(colStyle?.GetAlignment(originalCellValue) ?? TextAlignment.Left) { case TextAlignment.Left : - representation = representation.PadRight(toPad); - break; + return representation + new string(' ',toPad); case TextAlignment.Right : - representation = representation.PadLeft(toPad); - break; + return new string(' ',toPad) + representation; // TODO: With single line cells, centered and justified are the same right? case TextAlignment.Centered : case TextAlignment.Justified : - //round down - representation = representation.PadRight((int)Math.Floor(toPad/2.0)); - //round up - representation = representation.PadLeft((int)Math.Ceiling(toPad/2.0)); - break; - + return + new string(' ',(int)Math.Floor(toPad/2.0)) + // round down + representation + + new string(' ',(int)Math.Ceiling(toPad/2.0)) ; // round up } - - return representation; } // value is too wide