public List <PropertyInfo> GetColumns()
        {
            if (_columns == null)
            {
                var allColumns = typeof(T).GetProperties()
                                 .Where(p => !ColumnsConfig.ColumnsToIgnore.Contains(p.Name)).ToList();

                if (VisibleColumns.Any())
                {
                    var invalidColumns = VisibleColumns.Where(v => !allColumns.Select(c => c.Name).Contains(v));
                    if (invalidColumns.Any())
                    {
                        throw new InvalidColumnNameException(invalidColumns.ToDelimitedString());
                    }
                }

                var orderedColumns = VisibleColumns.Select(visibleColumn => allColumns.FirstOrDefault(c => c.Name == visibleColumn)).ToList();
                orderedColumns.AddRange(allColumns.Where(c => !c.IsVirtual() && !orderedColumns.Contains(c)));

                _columns = new List <PropertyInfo>();
                _columns.AddRange(orderedColumns);
            }
            return(_columns);
        }
Exemple #2
0
        private void PaintInternal(ConsoleBitmap context)
        {
            if (this.Height < 5)
            {
                context.DrawString("Grid can't render in a space this small", 0, 0);
                return;
            }

            if (VisibleColumns.Count == 0)
            {
                context.DrawString(NoVisibleColumnsMessage.ToConsoleString(DefaultColors.H1Color), 0, 0);
                return;
            }

            List <ConsoleString>          headers           = new List <ConsoleString>();
            List <List <ConsoleString> >  rows              = new List <List <ConsoleString> >();
            List <ColumnOverflowBehavior> overflowBehaviors = new List <ColumnOverflowBehavior>();


            if (VisibleColumns.Where(c => c.WidthPercentage != 0).Count() == 0)
            {
                foreach (var col in VisibleColumns)
                {
                    col.WidthPercentage = 1.0 / VisibleColumns.Count;
                }
            }

            foreach (var header in VisibleColumns)
            {
                headers.Add(header.ColumnDisplayName);
                var colWidth = (int)(header.WidthPercentage * this.Width);

                if (header.OverflowBehavior is SmartWrapOverflowBehavior)
                {
                    (header.OverflowBehavior as SmartWrapOverflowBehavior).MaxWidthBeforeWrapping = colWidth;
                }
                else if (header.OverflowBehavior is TruncateOverflowBehavior)
                {
                    (header.OverflowBehavior as TruncateOverflowBehavior).ColumnWidth = (header.OverflowBehavior as TruncateOverflowBehavior).ColumnWidth == 0 ? colWidth : (header.OverflowBehavior as TruncateOverflowBehavior).ColumnWidth;
                }

                overflowBehaviors.Add(header.OverflowBehavior);
            }

            int viewIndex = visibleRowOffset;

            foreach (var item in DataView.Items)
            {
                List <ConsoleString> row = new List <ConsoleString>();
                int columnIndex          = 0;
                foreach (var col in VisibleColumns)
                {
                    var value        = PropertyResolver(item, col.ColumnName.ToString());
                    var displayValue = value == null ? "<null>".ToConsoleString() : (value is ConsoleString ? (ConsoleString)value : value.ToString().ToConsoleString());

                    if (viewIndex == SelectedIndex && this.CanFocus)
                    {
                        if (this.SelectionMode == GridSelectionMode.Row || (this.SelectionMode == GridSelectionMode.Cell && columnIndex == selectedColumnIndex))
                        {
                            displayValue = new ConsoleString(displayValue.ToString(), this.Background, HasFocus ? DefaultColors.FocusColor : DefaultColors.SelectedUnfocusedColor);
                        }
                    }

                    row.Add(displayValue);
                    columnIndex++;
                }
                viewIndex++;
                rows.Add(row);
            }

            ConsoleTableBuilder builder = new ConsoleTableBuilder();
            ConsoleString       table;

            table = builder.FormatAsTable(headers, rows, RowPrefix.ToString(), overflowBehaviors, Gutter);


            if (FilterText != null)
            {
                table = table.Highlight(FilterText, DefaultColors.HighlightContrastColor, DefaultColors.HighlightColor, StringComparison.InvariantCultureIgnoreCase);
            }

            if (DataView.IsViewComplete == false)
            {
                table += "Loading more rows...".ToConsoleString(DefaultColors.H1Color);
            }
            else if (DataView.IsViewEndOfData && DataView.Items.Count == 0)
            {
                table += NoDataMessage.ToConsoleString(DefaultColors.H1Color);
            }
            else if (DataView.IsViewEndOfData)
            {
                if (ShowEndIfComplete)
                {
                    table += EndOfDataMessage.ToConsoleString(DefaultColors.H1Color);
                }
            }
            else
            {
                table += MoreDataMessage;
            }
            context.DrawString(table, 0, 0);


            if (FilteringEnabled)
            {
            }
        }
 public List <int> GetFilterColumns()
 {
     return(VisibleColumns.Where(x => !x.Value).
            Select(x => VisibleColumns.IndexOf(x)).ToList());
 }