Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #174. Changing Filter does not undo previously marked items #186

Merged
merged 71 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
6af1903
Fixed #58: Multi-line commands rendering wrong
tig Mar 11, 2020
a69373e
Fixed #58 - Newlines in commands render incorrectly
tig Mar 11, 2020
f155262
Added debug instructions to readme
tig Mar 11, 2020
440833e
Update src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs
tig Mar 11, 2020
dd2e765
simplified stripping of newline/linefeed
tig Mar 12, 2020
e66f614
Merge branch 'master' of tig:tig/GraphicalTools
tig Mar 14, 2020
ebcb92f
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 12, 2020
dce01c8
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 17, 2020
a2c0238
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 19, 2020
2184386
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 19, 2020
668c5c5
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 20, 2020
9d2c85a
Merge branch 'master' of tig:tig/GraphicalTools
tig May 13, 2020
d15fb38
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Aug 24, 2020
49b9559
Merge branch 'master' of tig:tig/GraphicalTools into master
tig Sep 25, 2020
4175dc0
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Sep 27, 2020
53b1203
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Sep 27, 2020
941cef7
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Sep 29, 2020
06c8a1f
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Sep 29, 2020
474826e
made column spacing tigher
tig Sep 29, 2020
3112550
update to new Terminal.gui package; no code changes
tig Sep 29, 2020
67911d3
tweaked widths
tig Sep 29, 2020
86a72b5
removed excess padding on right
tig Sep 29, 2020
c95abc5
removed excess rows at bottom
tig Sep 29, 2020
869b578
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Sep 29, 2020
bed1c3d
Merge branch 'master' into tighter_columns
tig Sep 29, 2020
142ffe5
status bar wsa occluding window
tig Sep 29, 2020
65c3c5a
tweaks
tig Sep 30, 2020
fd934e1
fixed build scripts to only build ocgv
tig Oct 1, 2020
f58d592
Merge branch 'tighter_columns' into minui
tig Oct 1, 2020
8e89181
refactored to make logic more obvious
tig Oct 1, 2020
1137d7d
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Oct 3, 2020
e965afd
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Oct 3, 2020
967b503
merged
tig Oct 3, 2020
e530f37
removed orig files
tig Oct 3, 2020
4188094
Merge branch 'master' of tig:PowerShell/GraphicalTools into master
tig Oct 5, 2020
797de5e
Merge branch 'master' of tig:tig/GraphicalTools into master
tig Oct 11, 2020
be4bea2
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Oct 21, 2020
85d8578
Merge branch 'master' of tig:tig/GraphicalTools
tig Oct 21, 2020
a03fc9b
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Nov 18, 2020
da6e273
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Apr 1, 2021
cec80a4
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Jun 3, 2022
c172968
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Jul 30, 2022
25204bc
Upgrade to Terminal.Gui v1.7
tig Aug 3, 2022
5e0cdcc
Merge branch 'master' into minui
tig Aug 3, 2022
b474f84
removed border when minui is enabled
tig Aug 3, 2022
75ca4d1
re-implemented feature post merge screw up
tig Aug 3, 2022
fd6483e
tweaks
tig Aug 3, 2022
c06641e
improve VS code build and debug support
tig Aug 3, 2022
7c4bec7
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Aug 4, 2022
cf84116
Merge branch 'master' into minui
tig Aug 4, 2022
15ab348
Merge branch 'master' into select_all
tig Aug 4, 2022
6833168
On exit, ensure only visible marked items are output
tig Aug 4, 2022
9aa550b
Merge branch 'minui' into fix_121_filter_input
tig Aug 4, 2022
50289b2
Merge branch 'select_all' into fix_121_filter_input
tig Aug 4, 2022
e544340
Merge branch 'improve_ocgvbuild' into fix_121_filter_input
tig Aug 4, 2022
ac54e61
Fixes 87
tig Aug 4, 2022
241e3d9
merge
tig Aug 9, 2022
d21f8c5
Merge branch 'fix_121_filter_input' into merge_tig
tig Aug 9, 2022
04dde17
Merge branch 'improve_ocgvbuild' into minui
tig Aug 9, 2022
c254965
Merge branch 'minui' into merge_tig
tig Aug 9, 2022
d3c1a82
Merge branch 'master' of tig:tig/GraphicalTools
tig Aug 17, 2022
3efc919
Tweaked build to have a better test at end
tig Aug 17, 2022
59e1cf7
merge
tig Aug 17, 2022
ef2c384
removed merge artifacts
tig Aug 17, 2022
20ce945
exclude .orig files (merge artifacts)
tig Aug 17, 2022
e148c73
Merge branch 'merge_tig'
tig Aug 17, 2022
b709b2c
merge
tig Aug 24, 2022
f93eeae
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Aug 24, 2022
54394e4
Merge branch 'master' of tig:PowerShell/GraphicalTools
tig Sep 15, 2022
1693145
Fixes #174. Changing Filter does not undo previously marked items
tig Apr 28, 2023
265d271
Apply suggestions from code review
andyleejordan May 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ internal class ConsoleGui : IDisposable
private Label _filterLabel;
private TextField _filterField;
private ListView _listView;
private GridViewDataSource _itemSource;
// _inputSource contains the full set of Input data and tracks any items the user
// marks. When the cmdlet exits, any marked items are returned. When a filter is
// active, the list view shows a copy of _inputSource that includes both the items
// matching the filter AND any items previously marked.
private GridViewDataSource _inputSource;

// _listViewSource is a filtered copy of _inputSource that ListView.Source is set to.
// Changes to IsMarked are propogated back to _inputSource.
private GridViewDataSource _listViewSource;
private ApplicationData _applicationData;
private GridViewDetails _gridViewDetails;

Expand All @@ -43,8 +51,9 @@ public HashSet<int> Start(ApplicationData applicationData)
List<string> gridHeaders = _applicationData.DataTable.DataColumns.Select((c) => c.Label).ToList();
CalculateColumnWidths(gridHeaders);

// Copy DataTable into the ListView's DataSource
_itemSource = LoadData();
// Copy the input DataTable into our master ListView source list; upon exit any items
// that are IsMarked are returned (if Outputmode is set)
_inputSource = LoadData();

if (!_applicationData.MinUI)
{
Expand All @@ -60,7 +69,9 @@ public HashSet<int> Start(ApplicationData applicationData)
// Status bar is where our key-bindings are handled
AddStatusBar(!_applicationData.MinUI);

// If -Filter parameter is set, apply it.
// We *always* apply a filter, even if the -Filter parameter is not set or Filtering is not
// available. The ListView always shows a fitlered version of _inputSource even if there is no
// actual fitler.
ApplyFilter();

_listView.SetFocus();
Expand All @@ -76,10 +87,8 @@ public HashSet<int> Start(ApplicationData applicationData)
return selectedIndexes;
}

// Ensure that only items that are marked AND not filtered out
// get returned (See Issue #121)
List<GridViewRow> itemList = GridViewHelpers.FilterData(_itemSource.GridViewRowList, _applicationData.Filter);
foreach (GridViewRow gvr in itemList)
// Return any items that were selected.
foreach (GridViewRow gvr in _inputSource.GridViewRowList)
{
if (gvr.IsMarked)
{
Expand Down Expand Up @@ -109,6 +118,7 @@ private GridViewDataSource LoadData()
items.Add(new GridViewRow
{
DisplayString = displayString,
// We use this to keep _inputSource up to date when a filter is applied
OriginalIndex = i
});

Expand All @@ -120,9 +130,22 @@ private GridViewDataSource LoadData()

private void ApplyFilter()
{
List<GridViewRow> itemList = GridViewHelpers.FilterData(_itemSource.GridViewRowList, _applicationData.Filter ?? string.Empty);
// Set the ListView to show only the subset defined by the filter
_listView.Source = new GridViewDataSource(itemList);
// The ListView is always filled with a (filtered) copy of _inputSource.
// We listen for `MarkChanged` events on this filtered list and apply those changes up to _inputSource.

if (_listViewSource != null) {
_listViewSource.MarkChanged -= ListViewSource_MarkChanged;
_listViewSource = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that I think we're relying on the garbage collector to dispose of the previously created _listViewSource here. I think that's fine? If _listViewSource is disposable, we should do that. I'm not sure if it is though...will find out soon I guess.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok we're good! It is not Disposable, it's an implementation of IListDataSource (which is a simple interface with just primitives and methods) and it holds a List<GridViewRow> which is managed only and thus not disposable.

}

_listViewSource = new GridViewDataSource(GridViewHelpers.FilterData(_inputSource.GridViewRowList, _applicationData.Filter ?? string.Empty));
_listViewSource.MarkChanged += ListViewSource_MarkChanged;
_listView.Source = _listViewSource;
}

private void ListViewSource_MarkChanged (object s, GridViewDataSource.RowMarkedEventArgs a)
{
_inputSource.GridViewRowList[a.Row.OriginalIndex].IsMarked = a.Row.IsMarked;
}

private void Accept()
Expand Down Expand Up @@ -198,7 +221,7 @@ private void AddStatusBar(bool visible)
// when ENTER is pressed in Single mode. If something was previously selected
// (using SPACE) then honor that as the single item to return
if (_applicationData.OutputMode == OutputModeOption.Single &&
_itemSource.GridViewRowList.Find(i => i.IsMarked) == null)
_inputSource.GridViewRowList.Find(i => i.IsMarked) == null)
{
_listView.MarkUnmarkRow();
}
Expand Down Expand Up @@ -315,7 +338,7 @@ private void AddFilter(Window win)
filterErrorLabel.Text = ex.Message;
filterErrorLabel.ColorScheme = Colors.Error;
filterErrorLabel.Redraw(filterErrorLabel.Bounds);
_listView.Source = _itemSource;
_listView.Source = _inputSource;
}
};

Expand Down Expand Up @@ -370,7 +393,7 @@ private void AddHeaders(Window win, List<string> gridHeaders)

private void AddListView(Window win)
{
_listView = new ListView(_itemSource);
_listView = new ListView(_inputSource);
_listView.X = MARGIN_LEFT;
if (!_applicationData.MinUI)
{
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.PowerShell.ConsoleGuiTools/GridViewDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ public void Render(ListView container, ConsoleDriver driver, bool selected, int

public void SetMark(int item, bool value)
{
var oldValue = GridViewRowList[item].IsMarked;
GridViewRowList[item].IsMarked = value;
var args = new RowMarkedEventArgs() {
Row = GridViewRowList[item],
OldValue = oldValue
};
MarkChanged?.Invoke(this, args);
}

public class RowMarkedEventArgs : EventArgs {
public GridViewRow Row { get; set;}
public bool OldValue { get ; set;}

}

public event EventHandler<RowMarkedEventArgs> MarkChanged;

public IList ToList()
{
return GridViewRowList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static List<GridViewRow> FilterData(List<GridViewRow> list, string filter

foreach (GridViewRow gvr in list)
{
if(Regex.IsMatch(gvr.DisplayString, filter, RegexOptions.IgnoreCase))
if (gvr.IsMarked || Regex.IsMatch(gvr.DisplayString, filter, RegexOptions.IgnoreCase))
{
items.Add(gvr);
}
Expand Down