@@ -19,10 +19,10 @@ internal class ConsoleGui : IDisposable
1919 // Width of Terminal.Gui ListView selection/check UI elements (old == 4, new == 2)
2020 private const int CHECK_WIDTH = 4 ;
2121 private bool _cancelled ;
22- private GridViewDataSource _itemSource ;
2322 private Label _filterLabel ;
2423 private TextField _filterField ;
2524 private ListView _listView ;
25+ private GridViewDataSource _itemSource ;
2626 private ApplicationData _applicationData ;
2727 private GridViewDetails _gridViewDetails ;
2828
@@ -37,22 +37,36 @@ public HashSet<int> Start(ApplicationData applicationData)
3737 ListViewOffset = _applicationData . OutputMode != OutputModeOption . None ? MARGIN_LEFT + CHECK_WIDTH : MARGIN_LEFT
3838 } ;
3939
40- Window win = AddTopLevelWindow ( ) ;
41- AddStatusBar ( ) ;
40+ Window win = CreateTopLevelWindow ( ) ;
4241
43- // GridView header logic
42+ // Create the headers and calculate column widths based on the DataTable
4443 List < string > gridHeaders = _applicationData . DataTable . DataColumns . Select ( ( c ) => c . Label ) . ToList ( ) ;
4544 CalculateColumnWidths ( gridHeaders ) ;
4645
47- AddFilter ( win ) ;
48- AddHeaders ( win , gridHeaders ) ;
46+ // Copy DataTable into the ListView's DataSource
47+ _itemSource = LoadData ( ) ;
48+
49+ // Add Filter UI
50+ if ( ! _applicationData . MinUI )
51+ {
52+ AddFilter ( win ) ;
53+ }
54+
55+ // Add Header UI
56+ if ( ! _applicationData . MinUI )
57+ {
58+ AddHeaders ( win , gridHeaders ) ;
59+ }
60+
61+ // Add ListView
62+ AddListView ( win ) ;
63+
64+ // Status bar is where our key-bindings are handled
65+ AddStatusBar ( ! _applicationData . MinUI ) ;
4966
50- // GridView row logic
51- LoadData ( ) ;
52- AddRows ( win ) ;
67+ // If -Filter parameter is set, apply it.
68+ ApplyFilter ( ) ;
5369
54- _filterField . Text = _applicationData . Filter ?? string . Empty ;
55- _filterField . CursorPosition = _filterField . Text . Length ;
5670 // Run the GUI.
5771 Application . Run ( ) ;
5872 Application . Shutdown ( ) ;
@@ -78,6 +92,40 @@ public HashSet<int> Start(ApplicationData applicationData)
7892 return selectedIndexes ;
7993 }
8094
95+ private GridViewDataSource LoadData ( )
96+ {
97+ var items = new List < GridViewRow > ( ) ;
98+ int newIndex = 0 ;
99+ for ( int i = 0 ; i < _applicationData . DataTable . Data . Count ; i ++ )
100+ {
101+ var dataTableRow = _applicationData . DataTable . Data [ i ] ;
102+ var valueList = new List < string > ( ) ;
103+ foreach ( var dataTableColumn in _applicationData . DataTable . DataColumns )
104+ {
105+ string dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
106+ valueList . Add ( dataValue ) ;
107+ }
108+
109+ string displayString = GridViewHelpers . GetPaddedString ( valueList , 0 , _gridViewDetails . ListViewColumnWidths ) ;
110+
111+ items . Add ( new GridViewRow
112+ {
113+ DisplayString = displayString ,
114+ OriginalIndex = i
115+ } ) ;
116+
117+ newIndex ++ ;
118+ }
119+
120+ return new GridViewDataSource ( items ) ;
121+ }
122+
123+ private void ApplyFilter ( ) {
124+ List < GridViewRow > itemList = GridViewHelpers . FilterData ( _itemSource . GridViewRowList , _applicationData . Filter ?? string . Empty ) ;
125+ // Set the ListView to show only the subset defined by the filter
126+ _listView . Source = new GridViewDataSource ( itemList ) ;
127+ }
128+
81129 private void Accept ( )
82130 {
83131 Application . RequestStop ( ) ;
@@ -89,23 +137,28 @@ private void Close()
89137 Application . RequestStop ( ) ;
90138 }
91139
92- private Window AddTopLevelWindow ( )
140+ private Window CreateTopLevelWindow ( )
93141 {
94142 // Creates the top-level window to show
95143 var win = new Window ( _applicationData . Title )
96144 {
97- X = 0 ,
98- Y = 0 ,
145+ X = _applicationData . MinUI ? - 1 : 0 ,
146+ Y = _applicationData . MinUI ? - 1 : 0 ,
147+
99148 // By using Dim.Fill(), it will automatically resize without manual intervention
100- Width = Dim . Fill ( ) ,
101- Height = Dim . Fill ( 1 )
149+ Width = Dim . Fill ( _applicationData . MinUI ? - 1 : 0 ) ,
150+ Height = Dim . Fill ( _applicationData . MinUI ? - 1 : 1 )
102151 } ;
103-
152+
153+ if ( _applicationData . MinUI ) {
154+ win . Border . BorderStyle = BorderStyle . None ;
155+ }
156+
104157 Application . Top . Add ( win ) ;
105158 return win ;
106159 }
107160
108- private void AddStatusBar ( )
161+ private void AddStatusBar ( bool visible )
109162 {
110163 var statusBar = new StatusBar (
111164 _applicationData . OutputMode != OutputModeOption . None
@@ -140,7 +193,7 @@ private void AddStatusBar()
140193 new StatusItem ( Key . Esc , "~ESC~ Close" , ( ) => Close ( ) )
141194 }
142195 ) ;
143-
196+ statusBar . Visible = visible ;
144197 Application . Top . Add ( statusBar ) ;
145198 }
146199
@@ -197,10 +250,11 @@ private void AddFilter(Window win)
197250 {
198251 _filterLabel = new Label ( FILTER_LABEL )
199252 {
200- X = MARGIN_LEFT
253+ X = MARGIN_LEFT ,
254+ Y = 0
201255 } ;
202256
203- _filterField = new TextField ( string . Empty )
257+ _filterField = new TextField ( _applicationData . Filter ?? string . Empty )
204258 {
205259 X = Pos . Right ( _filterLabel ) + 1 ,
206260 Y = Pos . Top ( _filterLabel ) ,
@@ -225,9 +279,9 @@ private void AddFilter(Window win)
225279 filterErrorLabel . Text = " " ;
226280 filterErrorLabel . ColorScheme = Colors . Base ;
227281 filterErrorLabel . Redraw ( filterErrorLabel . Bounds ) ;
282+ _applicationData . Filter = filterText ;
283+ ApplyFilter ( ) ;
228284
229- List < GridViewRow > itemList = GridViewHelpers . FilterData ( _itemSource . GridViewRowList , filterText ) ;
230- _listView . Source = new GridViewDataSource ( itemList ) ;
231285 }
232286 catch ( Exception ex )
233287 {
@@ -246,12 +300,16 @@ private void AddHeaders(Window win, List<string> gridHeaders)
246300 var header = new Label ( GridViewHelpers . GetPaddedString (
247301 gridHeaders ,
248302 _gridViewDetails . ListViewOffset ,
249- _gridViewDetails . ListViewColumnWidths ) )
303+ _gridViewDetails . ListViewColumnWidths ) ) ;
304+ header . X = 0 ;
305+ if ( _applicationData . MinUI )
250306 {
251- X = 0 ,
252- Y = 2
253- } ;
254-
307+ header . Y = 0 ;
308+ }
309+ else
310+ {
311+ header . Y = 2 ;
312+ }
255313 win . Add ( header ) ;
256314
257315 // This renders dashes under the header to make it more clear what is header and what is data
@@ -269,54 +327,32 @@ private void AddHeaders(Window win, List<string> gridHeaders)
269327 }
270328 }
271329
272- var headerLine = new Label ( headerLineText . ToString ( ) )
273- {
274- X = 0 ,
275- Y = 3
276- } ;
277-
278- win . Add ( headerLine ) ;
279- }
280-
281- private void LoadData ( )
282- {
283- var items = new List < GridViewRow > ( ) ;
284- int newIndex = 0 ;
285- for ( int i = 0 ; i < _applicationData . DataTable . Data . Count ; i ++ )
286- {
287- var dataTableRow = _applicationData . DataTable . Data [ i ] ;
288- var valueList = new List < string > ( ) ;
289- foreach ( var dataTableColumn in _applicationData . DataTable . DataColumns )
290- {
291- string dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
292- valueList . Add ( dataValue ) ;
293- }
294-
295- string displayString = GridViewHelpers . GetPaddedString ( valueList , 0 , _gridViewDetails . ListViewColumnWidths ) ;
296-
297- items . Add ( new GridViewRow
330+ if ( ! _applicationData . MinUI ) {
331+ var headerLine = new Label ( headerLineText . ToString ( ) )
298332 {
299- DisplayString = displayString ,
300- OriginalIndex = i
301- } ) ;
302-
303- newIndex ++ ;
333+ X = 0 ,
334+ Y = Pos . Bottom ( header )
335+ } ;
336+ win . Add ( headerLine ) ;
304337 }
305-
306- _itemSource = new GridViewDataSource ( items ) ;
307338 }
308339
309- private void AddRows ( Window win )
340+ private void AddListView ( Window win )
310341 {
311- _listView = new ListView ( _itemSource )
342+ _listView = new ListView ( _itemSource ) ;
343+ _listView . X = MARGIN_LEFT ;
344+ if ( ! _applicationData . MinUI )
312345 {
313- X = Pos . Left ( _filterLabel ) ,
314- Y = Pos . Bottom ( _filterLabel ) + 3 , // 1 for space, 1 for header, 1 for header underline
315- Width = Dim . Fill ( 2 ) ,
316- Height = Dim . Fill ( ) ,
317- AllowsMarking = _applicationData . OutputMode != OutputModeOption . None ,
318- AllowsMultipleSelection = _applicationData . OutputMode == OutputModeOption . Multiple ,
319- } ;
346+ _listView . Y = Pos . Bottom ( _filterLabel ) + 3 ; // 1 for space, 1 for header, 1 for header underline
347+ }
348+ else
349+ {
350+ _listView . Y = 1 ; // 1 for space, 1 for header, 1 for header underline
351+ }
352+ _listView . Width = Dim . Fill ( 2 ) ;
353+ _listView . Height = Dim . Fill ( ) ;
354+ _listView . AllowsMarking = _applicationData . OutputMode != OutputModeOption . None ;
355+ _listView . AllowsMultipleSelection = _applicationData . OutputMode == OutputModeOption . Multiple ;
320356
321357 win . Add ( _listView ) ;
322358 }
0 commit comments