private string FmtHdr(int colIndex, int w) { ConsoleListBoxColumn <RowT> col = columns[colIndex]; if (colIndex == sortColIndex) { return(FormatExactWidth( col.Header + " " + (sortDir == ListSortDirection.Ascending ? sortUp : sortDown), w )); } else { return(FormatExactWidth(col.Header, w)); } }
private Comparison <RowT> getComparer(ConsoleListBoxColumn <RowT> col, bool ascending) { if (ascending) { return(col.Comparer ?? ((a, b) => col.Renderer(a).Trim().CompareTo(col.Renderer(b).Trim()))); } else if (col.Comparer != null) { return((a, b) => col.Comparer(b, a)); } else { return((a, b) => col.Renderer(b).Trim().CompareTo(col.Renderer(a).Trim())); } }
/// <summary> /// Draw the list box /// </summary> /// <param name="focused">Framework parameter not relevant to this object</param> public override void Draw(bool focused) { int l = GetLeft(), r = GetRight(), t = GetTop(), b = GetBottom(), h = b - t + 1; bool needScrollbar = (sortedFilteredData.Count >= h); int contentR = needScrollbar ? r - 1 : r; // Prevent selection from running off the end of the list if (selectedRow > sortedFilteredData.Count - 1) { selectedRow = sortedFilteredData.Count - 1; } // Ensure selection is not before the top of the list if (selectedRow < 0) { selectedRow = 0; } // Don't scroll past the bottom of the list if (topRow > sortedFilteredData.Count - h + 1) { topRow = sortedFilteredData.Count - h + 1; } // Don't scroll before the top of the list if (topRow < 0) { topRow = 0; } if (topRow > selectedRow) { // Scroll up to reveal selected row topRow = selectedRow; } else if (topRow < selectedRow - h + 2) { // Scroll down to reveal selected row topRow = selectedRow - h + 2; } for (int y = 0, index = topRow - 1; y < h; ++y, ++index) { Console.SetCursorPosition(l, t + y); if (y == 0) { Console.BackgroundColor = ConsoleTheme.Current.ListBoxHeaderBg; Console.ForegroundColor = ConsoleTheme.Current.ListBoxHeaderFg; Console.Write(" "); for (int i = 0; i < columns.Count; ++i) { ConsoleListBoxColumn <RowT> col = columns[i]; if (i > 0) { Console.Write(" "); } // Truncate to designated size of the ListBox int maxW = r - Console.CursorLeft + 1; if (maxW > 0) { Console.Write(FmtHdr( i, col.Width < maxW ? col.Width : maxW )); } } } else if (index >= 0 && index < sortedFilteredData.Count) { if (topRow + y - 1 == selectedRow) { Console.BackgroundColor = ConsoleTheme.Current.ListBoxSelectedBg; Console.ForegroundColor = ConsoleTheme.Current.ListBoxSelectedFg; } else { Console.BackgroundColor = ConsoleTheme.Current.ListBoxUnselectedBg; Console.ForegroundColor = ConsoleTheme.Current.ListBoxUnselectedFg; } Console.Write(" "); for (int i = 0; i < columns.Count; ++i) { ConsoleListBoxColumn <RowT> col = columns[i]; if (i > 0) { Console.Write(" "); } // Truncate to designated size of the ListBox int maxW = contentR - Console.CursorLeft + 1; if (maxW > 0) { Console.Write(FormatExactWidth( col.Renderer(sortedFilteredData[index]).Trim(), col.Width < maxW ? col.Width : maxW )); } } } else { Console.BackgroundColor = ConsoleTheme.Current.ListBoxUnselectedBg; Console.ForegroundColor = ConsoleTheme.Current.ListBoxUnselectedFg; } try { if (y == 0) { Console.Write("".PadRight(r - Console.CursorLeft + 1)); } else { // Make space for scrollbar for anti-flicker Console.Write("".PadRight(contentR - Console.CursorLeft + 1)); } } catch { } } // Now draw the scrollbar if (needScrollbar) { DrawScrollbar( r, t + scrollTop, b, sortedFilteredData.Count > 0 ? t + 1 + scrollTop + (h - 2 - scrollTop) * selectedRow / sortedFilteredData.Count : -1 ); } }