11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- using OutGridView . Models ;
54using System ;
65using System . Collections . Generic ;
76using System . Diagnostics ;
87using System . Linq ;
98using System . Reflection ;
10- using System . Text ;
119using System . Text . RegularExpressions ;
10+ using OutGridView . Models ;
1211using Terminal . Gui . App ;
13- using Terminal . Gui . Configuration ;
1412using Terminal . Gui . Drawing ;
1513using Terminal . Gui . Input ;
1614using Terminal . Gui . ViewBase ;
@@ -21,16 +19,20 @@ namespace OutGridView.Cmdlet;
2119internal sealed class ConsoleGui : IDisposable
2220{
2321 private const string FILTER_LABEL = "Filter" ;
22+
2423 // This adjusts the left margin of all controls
2524 private const int MARGIN_LEFT = 1 ;
25+
2626 // Width of Terminal.Gui ListView selection/check UI elements (old == 4, new == 2)
2727 private const int CHECK_WIDTH = 2 ;
2828 private bool _cancelled ;
2929 private Label ? _filterLabel ;
3030 private TextField ? _filterField ;
3131 private View ? _filterErrorView ;
3232 private Label ? _header ;
33+
3334 private ListView ? _listView ;
35+
3436 // _inputSource contains the full set of Input data and tracks any items the user
3537 // marks. When the cmdlet exits, any marked items are returned. When a filter is
3638 // active, the list view shows a copy of _inputSource that includes both the items
@@ -48,22 +50,21 @@ public HashSet<int> Start(ApplicationData applicationData)
4850 _applicationData = applicationData ;
4951 // In Terminal.Gui v2, Application.Init() no longer accepts a driver parameter.
5052 // Instead, use Application.ForceDriver to specify the driver.
51- if ( _applicationData . UseNetDriver )
52- {
53- Application . ForceDriver = "NetDriver" ;
54- }
53+ if ( _applicationData . UseNetDriver ) Application . ForceDriver = "NetDriver" ;
5554 Application . Init ( ) ;
5655 _gridViewDetails = new GridViewDetails
5756 {
5857 // If OutputMode is Single or Multiple, then we make items selectable. If we make them selectable,
5958 // 2 columns are required for the check/selection indicator and space.
60- ListViewOffset = _applicationData . OutputMode != OutputModeOption . None ? MARGIN_LEFT + CHECK_WIDTH : MARGIN_LEFT
59+ ListViewOffset = _applicationData . OutputMode != OutputModeOption . None
60+ ? MARGIN_LEFT + CHECK_WIDTH
61+ : MARGIN_LEFT
6162 } ;
6263
63- Window win = CreateTopLevelWindow ( ) ;
64+ var win = CreateTopLevelWindow ( ) ;
6465
6566 // Create the headers and calculate column widths based on the DataTable
66- List < string > gridHeaders = _applicationData . DataTable . DataColumns . Select ( ( c ) => c . Label ) . ToList ( ) ;
67+ var gridHeaders = _applicationData . DataTable . DataColumns . Select ( c => c . Label ) . ToList ( ) ;
6768
6869 // Copy the input DataTable into our master ListView source list; upon exit any items
6970 // that are IsMarked are returned (if Outputmode is set)
@@ -98,30 +99,23 @@ public HashSet<int> Start(ApplicationData applicationData)
9899 Application . Shutdown ( ) ;
99100
100101 // Return results of selection if required.
101- HashSet < int > selectedIndexes = new HashSet < int > ( ) ;
102- if ( _cancelled )
103- {
104- return selectedIndexes ;
105- }
102+ var selectedIndexes = new HashSet < int > ( ) ;
103+ if ( _cancelled ) return selectedIndexes ;
106104
107105 // Return any items that were selected.
108- foreach ( GridViewRow gvr in _inputSource . GridViewRowList )
109- {
106+ foreach ( var gvr in _inputSource . GridViewRowList )
110107 if ( gvr . IsMarked )
111- {
112108 selectedIndexes . Add ( gvr . OriginalIndex ) ;
113- }
114- }
115109
116110 return selectedIndexes ;
117111
118112 void OnWinSubViewLayout ( object ? sender , EventArgs e )
119113 {
120114 CalculateColumnWidths ( gridHeaders ) ;
121115
122- if ( _header is not null )
116+ if ( _header is { } )
123117 _header . Text = GridViewHelpers . GetPaddedString ( gridHeaders , _gridViewDetails ! . ListViewOffset ,
124- _gridViewDetails . ListViewColumnWidths ) ;
118+ _gridViewDetails . ListViewColumnWidths ) ;
125119 UpdateDisplayStrings ( _listViewSource ) ;
126120 ApplyFilter ( ) ;
127121 }
@@ -133,17 +127,17 @@ private GridViewDataSource LoadData()
133127 if ( _applicationData == null )
134128 return new GridViewDataSource ( items ) ;
135129
136- for ( int i = 0 ; i < _applicationData . DataTable . Data . Count ; i ++ )
130+ for ( var i = 0 ; i < _applicationData . DataTable . Data . Count ; i ++ )
137131 {
138132 var dataTableRow = _applicationData . DataTable . Data [ i ] ;
139133 var valueList = new List < string > ( ) ;
140134 foreach ( var dataTableColumn in _applicationData . DataTable . DataColumns )
141135 {
142- string dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
136+ var dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
143137 valueList . Add ( dataValue ) ;
144138 }
145139
146- string displayString = GridViewHelpers . GetPaddedString ( valueList , 0 , _gridViewDetails ? . ListViewColumnWidths ) ;
140+ var displayString = GridViewHelpers . GetPaddedString ( valueList , 0 , _gridViewDetails ? . ListViewColumnWidths ) ;
147141
148142 items . Add ( new GridViewRow
149143 {
@@ -165,12 +159,14 @@ private void UpdateDisplayStrings(GridViewDataSource? source)
165159 var dataTableRow = _applicationData ! . DataTable . Data [ gvr . OriginalIndex ] ;
166160 foreach ( var dataTableColumn in _applicationData . DataTable . DataColumns )
167161 {
168- string dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
162+ var dataValue = dataTableRow . Values [ dataTableColumn . ToString ( ) ] . DisplayValue ;
169163 valueList . Add ( dataValue ) ;
170164 }
165+
171166 gvr . DisplayString = GridViewHelpers . GetPaddedString ( valueList , 0 , _gridViewDetails ? . ListViewColumnWidths ) ;
172167 }
173168 }
169+
174170 private void ApplyFilter ( )
175171 {
176172 // The ListView is always filled with a (filtered) copy of _inputSource.
@@ -186,14 +182,10 @@ private void ApplyFilter()
186182 _listViewSource = null ;
187183 }
188184
189- if ( _inputSource is null )
190- {
191- _inputSource = LoadData ( ) ;
192- }
185+ _inputSource ??= LoadData ( ) ;
193186
194187
195188 if ( _applicationData != null )
196- {
197189 try
198190 {
199191 _listViewSource = new GridViewDataSource ( GridViewHelpers . FilterData ( _inputSource . GridViewRowList ,
@@ -203,25 +195,19 @@ private void ApplyFilter()
203195 {
204196 _filterErrorView ! . Text = ex . Message ;
205197 }
206- }
207198
208199 _listViewSource ? . MarkChanged + = ListViewSource_MarkChanged ;
209200 _listView ? . Source = _listViewSource ;
210201
211202 // Restore selection - find the previously selected item in the new filtered list
212- if ( selectedItem is not null && _listViewSource != null )
203+ if ( selectedItem is { } && _listViewSource != null )
213204 {
214- int newIndex =
205+ var newIndex =
215206 _listViewSource . GridViewRowList . FindIndex ( i => i . OriginalIndex == selectedItem . OriginalIndex ) ;
216- if ( newIndex >= 0 )
217- {
218- _listView ! . SelectedItem = newIndex ;
219- }
220- }
221- if ( _listView ? . SelectedItem == - 1 )
222- {
223- _listView ! . SelectedItem = 0 ;
207+ if ( newIndex >= 0 ) _listView ! . SelectedItem = newIndex ;
224208 }
209+
210+ if ( _listView ? . SelectedItem == - 1 ) _listView ! . SelectedItem = 0 ;
225211 }
226212
227213 private void ListViewSource_MarkChanged ( object ? s , GridViewDataSource . RowMarkedEventArgs a )
@@ -245,13 +231,10 @@ private Window CreateTopLevelWindow()
245231 // Creates the top-level window to show
246232 var win = new Window
247233 {
248- Title = _applicationData ! . Title ?? "Out-ConsoleGridView" ,
234+ Title = _applicationData ! . Title ?? "Out-ConsoleGridView"
249235 } ;
250236
251- if ( _applicationData . MinUI )
252- {
253- win . BorderStyle = LineStyle . None ;
254- }
237+ if ( _applicationData . MinUI ) win . BorderStyle = LineStyle . None ;
255238
256239 return win ;
257240 }
@@ -260,11 +243,9 @@ private void AddStatusBar(Window win, bool visible)
260243 {
261244 var shortcuts = new List < Shortcut > ( ) ;
262245 if ( _applicationData ! . OutputMode != OutputModeOption . None )
263- {
264246 // Use Key.Empty for SPACE with no delegate because ListView already
265247 // handles SPACE
266248 shortcuts . Add ( new Shortcut ( Key . Space , "Select Item" , null ) ) ;
267- }
268249
269250 if ( _applicationData . OutputMode == OutputModeOption . Multiple )
270251 {
@@ -283,7 +264,6 @@ private void AddStatusBar(Window win, bool visible)
283264 }
284265
285266 if ( _applicationData . OutputMode != OutputModeOption . None )
286- {
287267 shortcuts . Add ( new Shortcut ( Key . Enter , "Accept" , ( ) =>
288268 {
289269 if ( Application . Top ? . MostFocused == _listView )
@@ -293,29 +273,28 @@ private void AddStatusBar(Window win, bool visible)
293273 // (using SPACE) then honor that as the single item to return
294274 if ( _applicationData . OutputMode == OutputModeOption . Single &&
295275 _inputSource ! . GridViewRowList . Find ( i => i . IsMarked ) == null )
296- {
297276 // Toggle the mark on the currently selected item
298277 if ( _listView ! . SelectedItem >= 0 && _listView . SelectedItem < _listViewSource ! . Count )
299278 {
300279 var item = _listViewSource . GridViewRowList [ _listView . SelectedItem ] ;
301280 item . IsMarked = ! item . IsMarked ;
302281 }
303- }
282+
304283 Accept ( ) ;
305284 }
306285 else if ( Application . Top ? . MostFocused == _filterField )
307286 {
308287 _listView ! . SetFocus ( ) ;
309288 }
310289 } ) ) ;
311- }
312290
313291 shortcuts . Add ( new Shortcut ( Key . Esc , "Close" , Close ) ) ;
314292 if ( _applicationData . Verbose || _applicationData . Debug )
315293 {
316294 shortcuts . Add ( new Shortcut ( Key . Empty , $ " v{ _applicationData . ModuleVersion } ", null ) ) ;
317295 shortcuts . Add ( new Shortcut ( Key . Empty ,
318- $ "{ Application . Driver } v{ FileVersionInfo . GetVersionInfo ( Assembly . GetAssembly ( typeof ( Application ) ) ! . Location ) . ProductVersion } ", null ) ) ;
296+ $ "{ Application . Driver } v{ FileVersionInfo . GetVersionInfo ( Assembly . GetAssembly ( typeof ( Application ) ) ! . Location ) . ProductVersion } ",
297+ null ) ) ;
319298 }
320299
321300 win . Add ( new StatusBar ( shortcuts ) ) ;
@@ -326,43 +305,36 @@ private void CalculateColumnWidths(List<string> gridHeaders)
326305 _gridViewDetails ! . ListViewColumnWidths = new int [ gridHeaders . Count ] ;
327306 var listViewColumnWidths = _gridViewDetails . ListViewColumnWidths ;
328307
329- for ( int i = 0 ; i < gridHeaders . Count ; i ++ )
330- {
331- listViewColumnWidths [ i ] = gridHeaders [ i ] . Length ;
332- }
308+ for ( var i = 0 ; i < gridHeaders . Count ; i ++ ) listViewColumnWidths [ i ] = gridHeaders [ i ] . Length ;
333309
334310 // calculate the width of each column based on longest string in each column for each row
335- foreach ( var row in _applicationData . DataTable . Data )
311+ foreach ( var row in _applicationData ! . DataTable . Data )
336312 {
337- int index = 0 ;
313+ var index = 0 ;
338314
339315 // use half of the visible buffer height for the number of objects to inspect to calculate widths
340- foreach ( var col in row . Values . Take ( Application . Top . Frame . Height / 2 ) )
316+ foreach ( var col in row . Values . Take ( Application . Top ! . Frame . Height / 2 ) )
341317 {
342318 var len = col . Value . DisplayValue . Length ;
343- if ( len > listViewColumnWidths [ index ] )
344- {
345- listViewColumnWidths [ index ] = len ;
346- }
319+ if ( len > listViewColumnWidths [ index ] ) listViewColumnWidths [ index ] = len ;
347320 index ++ ;
348321 }
349322 }
350323
351324 // if the total width is wider than the usable width, remove 1 from widest column until it fits
352- _gridViewDetails . UsableWidth = Application . Top . Frame . Width - MARGIN_LEFT - listViewColumnWidths . Length - _gridViewDetails . ListViewOffset ;
353- int columnWidthsSum = listViewColumnWidths . Sum ( ) ;
325+ _gridViewDetails . UsableWidth = Application . Top ! . Frame . Width - MARGIN_LEFT - listViewColumnWidths . Length -
326+ _gridViewDetails . ListViewOffset ;
327+ var columnWidthsSum = listViewColumnWidths . Sum ( ) ;
354328 while ( columnWidthsSum >= _gridViewDetails . UsableWidth )
355329 {
356- int maxWidth = 0 ;
357- int maxIndex = 0 ;
358- for ( int i = 0 ; i < listViewColumnWidths . Length ; i ++ )
359- {
330+ var maxWidth = 0 ;
331+ var maxIndex = 0 ;
332+ for ( var i = 0 ; i < listViewColumnWidths . Length ; i ++ )
360333 if ( listViewColumnWidths [ i ] > maxWidth )
361334 {
362335 maxWidth = listViewColumnWidths [ i ] ;
363336 maxIndex = i ;
364337 }
365- }
366338
367339 listViewColumnWidths [ maxIndex ] -- ;
368340 columnWidthsSum -- ;
@@ -407,14 +379,13 @@ private void AddFilter(Window win)
407379
408380 _filterField . TextChanged += ( sender , e ) =>
409381 {
410- string ? filterText = _filterField . Text ? . ToString ( ) ;
382+ var filterText = _filterField . Text ;
411383 try
412384 {
413385 _filterErrorView . Text = string . Empty ;
414386 _filterErrorView . SetNeedsDraw ( ) ;
415387 _applicationData ! . Filter = filterText ! ;
416388 ApplyFilter ( ) ;
417-
418389 }
419390 catch ( Exception ex )
420391 {
@@ -435,22 +406,18 @@ private void AddHeaders(Window win, List<string> gridHeaders)
435406 //Text = GridViewHelpers.GetPaddedString(gridHeaders, _gridViewDetails!.ListViewOffset, _gridViewDetails.ListViewColumnWidths),
436407 } ;
437408 if ( _applicationData ! . MinUI )
438- {
439409 _header . Y = 0 ;
440- }
441410 else
442- {
443411 _header . Y = Pos . Bottom ( _filterErrorView ! ) ;
444- }
445412 win . Add ( _header ) ;
446413
447414 if ( ! _applicationData . MinUI )
448415 {
449- var headerLine = new Line ( )
416+ var headerLine = new Line
450417 {
451418 X = MARGIN_LEFT ,
452419 Y = Pos . Bottom ( _header ) ,
453- Width = Dim . Fill ( MARGIN_LEFT ) ,
420+ Width = Dim . Fill ( MARGIN_LEFT )
454421 } ;
455422 win . Add ( headerLine ) ;
456423 }
@@ -464,13 +431,9 @@ private void AddListView(Window win)
464431 X = MARGIN_LEFT
465432 } ;
466433 if ( ! _applicationData ! . MinUI )
467- {
468434 _listView . Y = Pos . Bottom ( _filterLabel ! ) + 2 ; // 1 for space, 1 for header, 1 for header underline
469- }
470435 else
471- {
472436 _listView . Y = 1 ; // 1 for space, 1 for header, 1 for header underline
473- }
474437 _listView . Width = Dim . Fill ( 1 ) ;
475438 _listView . Height = Dim . Fill ( ) ;
476439 _listView . AllowsMarking = _applicationData . OutputMode != OutputModeOption . None ;
@@ -487,7 +450,6 @@ private void AddListView(Window win)
487450 public void Dispose ( )
488451 {
489452 if ( ! Console . IsInputRedirected )
490- {
491453 // By emitting this, we fix two issues:
492454 // 1. An issue where arrow keys don't work in the console because .NET
493455 // requires application mode to support Arrow key escape sequences.
@@ -497,6 +459,5 @@ public void Dispose()
497459 // mouse tracking is still on. Esc[?1003l turns it off.
498460 // See https://www.xfree86.org/current/ctlseqs.html#Mouse%20Tracking
499461 Console . Write ( "\u001b [?1h\u001b [?1003l" ) ;
500- }
501462 }
502- }
463+ }
0 commit comments