|
5 | 5 | using System; |
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Linq; |
8 | | -using System.Text; |
9 | 8 |
|
10 | 9 | namespace ConsoleGUI.Controls |
11 | 10 | { |
12 | 11 | public class DataGrid<T> : Control |
13 | 12 | { |
14 | 13 | public class ColumnDefinition |
15 | 14 | { |
16 | | - public readonly string Header; |
| 15 | + private readonly Func<int, Character> _headerSelector; |
| 16 | + private readonly Func<T, int, Character> _valueSelector; |
| 17 | + |
17 | 18 | public readonly int Width; |
18 | | - public readonly Func<T, int, Character> Selector; |
19 | 19 |
|
20 | | - public ColumnDefinition(string header, int width, Func<T, int, Character> selector) |
| 20 | + public ColumnDefinition(string header, int width, Func<T, string> selector) |
21 | 21 | { |
22 | | - Header = header; |
23 | 22 | Width = width; |
24 | | - Selector = selector; |
| 23 | + |
| 24 | + _headerSelector = i => |
| 25 | + { |
| 26 | + var text = header; |
| 27 | + return i < text.Length ? new Character(text[i]) : Character.Empty; |
| 28 | + }; |
| 29 | + |
| 30 | + _valueSelector = (v, i) => |
| 31 | + { |
| 32 | + var text = selector(v); |
| 33 | + return i < text.Length ? new Character(text[i]) : Character.Empty; |
| 34 | + }; |
25 | 35 | } |
26 | 36 |
|
27 | | - public ColumnDefinition(string header, int width, Func<T, string> selector) |
| 37 | + public ColumnDefinition(string header, int width, Func<T, int, Character> selector) |
28 | 38 | { |
29 | | - Header = header; |
30 | 39 | Width = width; |
31 | | - Selector = (v, i) => |
| 40 | + |
| 41 | + _headerSelector = i => |
32 | 42 | { |
33 | | - var text = selector(v); |
| 43 | + var text = header; |
34 | 44 | return i < text.Length ? new Character(text[i]) : Character.Empty; |
35 | 45 | }; |
| 46 | + |
| 47 | + _valueSelector = selector; |
| 48 | + } |
| 49 | + |
| 50 | + public ColumnDefinition(int width, Func<int, Character> headerSelector, Func<T, int, Character> valueSelector) |
| 51 | + { |
| 52 | + Width = width; |
| 53 | + |
| 54 | + _headerSelector = headerSelector; |
| 55 | + _valueSelector = valueSelector; |
36 | 56 | } |
37 | 57 |
|
38 | | - public ColumnDefinition(string header, int width, Func<T, string> selector, Func<T, Color?> foreground = null, Func<T, Color?> background = null) |
| 58 | + public ColumnDefinition( |
| 59 | + string header, |
| 60 | + int width, |
| 61 | + Func<T, string> selector, |
| 62 | + Func<T, Color?> foreground = null, |
| 63 | + Func<T, Color?> background = null, |
| 64 | + TextAlignment textAlignment = TextAlignment.Left) |
39 | 65 | { |
40 | | - Header = header; |
41 | 66 | Width = width; |
42 | | - Selector = (v, i) => |
| 67 | + |
| 68 | + _headerSelector = i => |
| 69 | + { |
| 70 | + var text = header; |
| 71 | + return i < text.Length ? new Character(text[i]) : Character.Empty; |
| 72 | + }; |
| 73 | + |
| 74 | + _valueSelector = (v, i) => |
43 | 75 | { |
44 | 76 | var text = selector(v); |
45 | | - return new Character(i < text.Length ? (char?)text[i] : null, foreground?.Invoke(v), background?.Invoke(v)); |
| 77 | + |
| 78 | + if (textAlignment == TextAlignment.Center) |
| 79 | + i -= (Width - text.Length) / 2; |
| 80 | + if (textAlignment == TextAlignment.Right) |
| 81 | + i -= Width - text.Length; |
| 82 | + |
| 83 | + return new Character( |
| 84 | + i >= 0 && i < text.Length ? (char?)text[i] : null, |
| 85 | + foreground?.Invoke(v), |
| 86 | + background?.Invoke(v)); |
46 | 87 | }; |
47 | 88 | } |
| 89 | + |
| 90 | + internal Character GetHeader(int xOffset) => _headerSelector(xOffset); |
| 91 | + internal Character GetValue(T value, int xOffset) => _valueSelector(value, xOffset); |
48 | 92 | } |
49 | 93 |
|
50 | | - private ColumnDefinition[] columns = new ColumnDefinition[0]; |
| 94 | + private ColumnDefinition[] _columns = new ColumnDefinition[0]; |
51 | 95 | public ColumnDefinition[] Columns |
52 | 96 | { |
53 | | - get => columns; |
| 97 | + get => _columns; |
54 | 98 | set => Setter |
55 | | - .Set(ref columns, value.ToArray()) |
| 99 | + .Set(ref _columns, value.ToArray()) |
56 | 100 | .Then(Initialize); |
57 | 101 | } |
58 | 102 |
|
59 | | - private IReadOnlyCollection<T> data = new T[0]; |
| 103 | + private IReadOnlyCollection<T> _data = new T[0]; |
60 | 104 | public IReadOnlyCollection<T> Data |
61 | 105 | { |
62 | | - get => data; |
| 106 | + get => _data; |
63 | 107 | set => Setter |
64 | | - .Set(ref data, value) |
| 108 | + .Set(ref _data, value) |
| 109 | + .Then(Initialize); |
| 110 | + } |
| 111 | + |
| 112 | + private DataGridStyle _style = DataGridStyle.AllBorders; |
| 113 | + public DataGridStyle Style |
| 114 | + { |
| 115 | + get => _style; |
| 116 | + set => Setter |
| 117 | + .Set(ref _style, value) |
| 118 | + .Then(Initialize); |
| 119 | + } |
| 120 | + |
| 121 | + private bool _showHeader = true; |
| 122 | + public bool ShowHeader |
| 123 | + { |
| 124 | + get => _showHeader; |
| 125 | + set => Setter |
| 126 | + .Set(ref _showHeader, value) |
65 | 127 | .Then(Initialize); |
66 | 128 | } |
67 | 129 |
|
@@ -89,33 +151,80 @@ public override Cell this[Position position] |
89 | 151 | { |
90 | 152 | get |
91 | 153 | { |
| 154 | + if (position.Y < 0) return Character.Empty; |
| 155 | + if (position.X < 0) return Character.Empty; |
| 156 | + if (position.Y >= CalculateDesiredHeight()) return Character.Empty; |
| 157 | + if (position.X >= CalculateDesiredWidth()) return Character.Empty; |
| 158 | + |
92 | 159 | int column = 0; |
93 | | - int xOffset = 0; |
94 | | - if (position.Y > Data.Count * 2) return Character.Empty; |
95 | | - while (column < Columns.Length && position.X > xOffset + Columns[column].Width) xOffset += Columns[column++].Width + 1; |
96 | | - |
97 | | - int x = position.X - xOffset; |
98 | | - if (column >= Columns.Length) return Character.Empty; |
99 | | - if (column == Columns.Length - 1 && x == Columns[column].Width) return Character.Empty; |
100 | | - if (position.Y == 1 && x == Columns[column].Width) return new Character('╪'); |
101 | | - if (position.Y == 1) return new Character('═'); |
102 | | - if (position.Y % 2 == 1 && x == Columns[column].Width) return new Character('┼'); |
103 | | - if (position.Y % 2 == 1) return new Character('─'); |
104 | | - if (x == Columns[column].Width) return new Character('│'); |
105 | | - if (position.Y == 0) return x < Columns[column].Header.Length ? new Character(Columns[column].Header[x]) : Character.Empty; |
106 | | - return Columns[column].Selector(Data.ElementAt(position.Y / 2 - 1), x); |
| 160 | + int xOffset = position.X; |
| 161 | + bool isVerticalBorder = false; |
| 162 | + |
| 163 | + while (xOffset >= Columns[column].Width) |
| 164 | + { |
| 165 | + if (Style.HasVertivalBorders && xOffset == Columns[column].Width) |
| 166 | + { |
| 167 | + isVerticalBorder = true; |
| 168 | + break; |
| 169 | + } |
| 170 | + |
| 171 | + xOffset -= Columns[column].Width; |
| 172 | + xOffset -= Style.HasVertivalBorders ? 1 : 0; |
| 173 | + column += 1; |
| 174 | + } |
| 175 | + |
| 176 | + if (ShowHeader && position.Y == 0 && isVerticalBorder) return Style.HeaderVerticalBorder.Value; |
| 177 | + if (ShowHeader && position.Y == 1 && isVerticalBorder && Style.HeaderIntersectionBorder.HasValue) return Style.HeaderIntersectionBorder.Value; |
| 178 | + if (ShowHeader && position.Y == 1 && Style.HeaderHorizontalBorder.HasValue) return Style.HeaderHorizontalBorder.Value; |
| 179 | + if (ShowHeader && position.Y == 0) return Columns[column].GetHeader(xOffset); |
| 180 | + |
| 181 | + int yOffset = position.Y; |
| 182 | + |
| 183 | + if (ShowHeader) yOffset--; |
| 184 | + if (ShowHeader && Style.HeaderHorizontalBorder.HasValue) yOffset--; |
| 185 | + |
| 186 | + int row = Style.CellHorizontalBorder.HasValue |
| 187 | + ? yOffset / 2 |
| 188 | + : yOffset; |
| 189 | + bool isHorizontalBorder = Style.CellHorizontalBorder.HasValue |
| 190 | + ? yOffset % 2 == 1 |
| 191 | + : false; |
| 192 | + |
| 193 | + if (isHorizontalBorder && isVerticalBorder) return Style.CellIntersectionBorder.Value; |
| 194 | + if (isHorizontalBorder) return Style.CellHorizontalBorder.Value; |
| 195 | + if (isVerticalBorder) return Style.CellVerticalBorder.Value; |
| 196 | + |
| 197 | + return Columns[column].GetValue(Data.ElementAt(row), xOffset); |
107 | 198 | } |
108 | 199 | } |
109 | 200 |
|
110 | 201 | protected override void Initialize() |
111 | 202 | { |
112 | | - using (Freeze()) |
113 | | - { |
114 | | - int width = Columns.Sum(c => c.Width) + Math.Max(0, Columns.Length - 1); |
115 | | - int height = Data.Count * 2 + 1; |
| 203 | + Resize(new Size(CalculateDesiredWidth(), CalculateDesiredHeight())); |
| 204 | + } |
116 | 205 |
|
117 | | - Resize(new Size(width, height)); |
118 | | - } |
| 206 | + private int CalculateDesiredHeight() |
| 207 | + { |
| 208 | + int height = Data.Count; |
| 209 | + |
| 210 | + if (ShowHeader) |
| 211 | + height += 1; |
| 212 | + if (ShowHeader && Style.HeaderHorizontalBorder.HasValue) |
| 213 | + height += 1; |
| 214 | + if (Data.Count > 0 && Style.CellHorizontalBorder.HasValue) |
| 215 | + height += Data.Count - 1; |
| 216 | + |
| 217 | + return height; |
| 218 | + } |
| 219 | + |
| 220 | + private int CalculateDesiredWidth() |
| 221 | + { |
| 222 | + int width = Columns.Sum(c => c.Width); |
| 223 | + |
| 224 | + if (Columns.Length > 0 && Style.HasVertivalBorders) |
| 225 | + width += Columns.Length - 1; |
| 226 | + |
| 227 | + return width; |
119 | 228 | } |
120 | 229 | } |
121 | 230 | } |
0 commit comments