Skip to content

Commit

Permalink
Leverage 'StatusBar' instead of the 'Menu'. (#85)
Browse files Browse the repository at this point in the history
* Fixed #58: Multi-line commands rendering wrong

* Fixed #58 - Newlines in commands render incorrectly

* Added debug instructions to readme

* Update src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs

Co-Authored-By: Tyler James Leonhardt <[email protected]>

* simplified stripping of newline/linefeed

* fixed exit UI via StatusBar instead of Menu

* fixes Filter TextView and Apply button don't honor horizontal resizing

* removed commented out code

* Add StatusBar label explainging that SPACE Marks Items

* fixed spelling of delegate

* removed Apply button

Co-authored-by: Tyler James Leonhardt <[email protected]>
  • Loading branch information
tig and TylerLeonhardt authored Apr 19, 2020
1 parent d3ebace commit 72803b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
73 changes: 37 additions & 36 deletions src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace OutGridView.Cmdlet
internal class ConsoleGui : IDisposable
{
private const string FILTER_LABEL = "Filter";
private const string APPLY_LABEL = "Apply";
private bool _cancelled;
private GridViewDataSource _itemSource;
private ListView _listView;
Expand All @@ -27,12 +26,13 @@ public HashSet<int> Start(ApplicationData applicationData)
_applicationData = applicationData;
_gridViewDetails = new GridViewDetails
{
// Have a 8 character addition of a checkbox (" [ ]") that we have to factor in.
ListViewOffset = 8
// If we have an OutputMode, then we want to make them selectable. If we make them selectable,
// they have a 8 character addition of a checkbox (" [ ]") that we have to factor in.
ListViewOffset = _applicationData.OutputMode != OutputModeOption.None ? 8 : 4
};

AddMenu();
Window win = AddTopLevelWindow();
AddStatusBar();

// GridView header logic
List<string> gridHeaders = _applicationData.DataTable.DataColumns.Select((c) => c.Label).ToList();
Expand All @@ -44,7 +44,7 @@ public HashSet<int> Start(ApplicationData applicationData)
// GridView row logic
LoadData();
AddRows(win);

// Run the GUI.
Application.Run();

Expand All @@ -66,13 +66,22 @@ public HashSet<int> Start(ApplicationData applicationData)
return selectedIndexes;
}

private void Accept(){
Application.RequestStop();
}

private void Close(){
_cancelled = true;
Application.RequestStop();
}

private Window AddTopLevelWindow()
{
// Creates the top-level window to show
var win = new Window(_applicationData.Title)
{
X = 0,
Y = 1, // Leave one row for the toplevel menu
Y = 0,
// By using Dim.Fill(), it will automatically resize without manual intervention
Width = Dim.Fill(),
Height = Dim.Fill()
Expand All @@ -82,19 +91,25 @@ private Window AddTopLevelWindow()
return win;
}

private void AddMenu()
private void AddStatusBar()
{
var menu = new MenuBar(new MenuBarItem []
{
new MenuBarItem("_Actions (F9)",
new MenuItem []
var statusBar = new StatusBar(
_applicationData.OutputMode != OutputModeOption.None
? new StatusItem []
{
// Use Key.Unknown for SPACE with no delegate because ListView already
// handles SPACE
new StatusItem(Key.Unknown, "~SPACE~ Mark Item", null),
new StatusItem(Key.Enter, "~ENTER~ Accept", () => Accept()),
new StatusItem(Key.Esc, "~ESC~ Close", () => Close())
}
: new StatusItem []
{
new MenuItem("_Accept", string.Empty, () => { Application.RequestStop(); }),
new MenuItem("_Cancel", string.Empty, () =>{ _cancelled = true; Application.RequestStop(); })
})
});
new StatusItem(Key.Esc, "~ESC~ Close", () => Close())
}
);

Application.Top.Add(menu);
Application.Top.Add(statusBar);
}

private void CalculateColumnWidths(List<string> gridHeaders)
Expand All @@ -120,7 +135,7 @@ private void CalculateColumnWidths(List<string> gridHeaders)
{
listViewColumnWidths[index] = len;
}

index++;
}
}
Expand Down Expand Up @@ -154,28 +169,25 @@ private void AddFilter(Window win)
X = 2
};

// 1 is for space between filterField and applyButton
// 2 is for the square brackets added to buttons
var filterLabelAndApplyButtonWidth = filterLabel.Text.Length + 1 + APPLY_LABEL.Length;
var filterLabelWidth = filterLabel.Text.Length + 1;
var filterField = new TextField(string.Empty)
{
X = Pos.Right(filterLabel) + 1,
Y = Pos.Top(filterLabel),
CanFocus = true,
Width = Dim.Fill() - filterLabelAndApplyButtonWidth
Width = Dim.Fill() - filterLabelWidth
};

var filterErrorLabel = new Label(string.Empty)
{
X = Pos.Right(filterLabel) + 1,
Y = Pos.Top(filterLabel) + 1,
ColorScheme = Colors.Base,
Width = Dim.Fill() - filterLabelAndApplyButtonWidth
Width = Dim.Fill() - filterLabelWidth
};

EventHandler<ustring> filterChanged = (object sender, ustring e) =>
{
// TODO: remove Apply button and code when this starts working
try
{
filterErrorLabel.Text = " ";
Expand All @@ -196,18 +208,7 @@ private void AddFilter(Window win)

filterField.Changed += filterChanged;

var filterApplyButton = new Button(APPLY_LABEL)
{
// Pos.Right(filterField) returns 0
X = Pos.Right(filterField) + 1,
Y = Pos.Top(filterLabel),
Clicked = () =>
{
filterChanged.Invoke(null, filterField.Text);
}
};

win.Add(filterLabel, filterField, filterErrorLabel, filterApplyButton);
win.Add(filterLabel, filterField, filterErrorLabel);
}

private void AddHeaders(Window win, List<string> gridHeaders)
Expand Down Expand Up @@ -283,7 +284,7 @@ private void AddRows(Window win)
Y = 4,
Width = Dim.Fill(2),
Height = Dim.Fill(2),
AllowsMarking = true
AllowsMarking = _applicationData.OutputMode != OutputModeOption.None,
};

win.Add(_listView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class OutConsoleGridViewCmdletCommand : PSCmdlet, IDisposable
/// and if it should be possible to select multiple or single list items.
/// </summary>
[Parameter()]
public OutputModeOption OutputMode { set; get; }
public OutputModeOption OutputMode { set; get; } = OutputModeOption.Multiple;

#endregion Input Parameters

Expand Down

0 comments on commit 72803b9

Please sign in to comment.