Skip to content

Commit 1c8c9cb

Browse files
Added back braces for namespace, dropping braces makes PR hard to read.
1 parent b321382 commit 1c8c9cb

7 files changed

+617
-604
lines changed

src/Microsoft.PowerShell.ConsoleGuiTools/ConsoleGui.cs

+270-268
Large diffs are not rendered by default.

src/Microsoft.PowerShell.ConsoleGuiTools/GridViewDataSource.cs

+50-48
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,71 @@
1212

1313
using Terminal.Gui;
1414

15-
namespace Microsoft.PowerShell.ConsoleGuiTools;
16-
17-
internal sealed class GridViewDataSource(IEnumerable<GridViewRow> gridViewRowList) : IListDataSource
15+
namespace Microsoft.PowerShell.ConsoleGuiTools
1816
{
19-
internal List<GridViewRow> GridViewRowList { get; init; } = gridViewRowList.ToList();
20-
21-
public int Count => GridViewRowList.Count;
22-
23-
public int Length { get; }
2417

25-
public void Render(ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start)
18+
internal sealed class GridViewDataSource(IEnumerable<GridViewRow> gridViewRowList) : IListDataSource
2619
{
27-
container.Move(col, line);
28-
RenderUstr(driver, GridViewRowList[item].DisplayString, width);
29-
}
20+
internal List<GridViewRow> GridViewRowList { get; init; } = gridViewRowList.ToList();
3021

31-
public bool IsMarked(int item) => GridViewRowList[item].IsMarked;
22+
public int Count => GridViewRowList.Count;
3223

33-
public void SetMark(int item, bool value)
34-
{
35-
var oldValue = GridViewRowList[item].IsMarked;
36-
GridViewRowList[item].IsMarked = value;
37-
var args = new RowMarkedEventArgs()
24+
public int Length { get; }
25+
26+
public void Render(ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start)
3827
{
39-
Row = GridViewRowList[item],
40-
OldValue = oldValue
41-
};
42-
MarkChanged?.Invoke(this, args);
43-
}
28+
container.Move(col, line);
29+
RenderUstr(driver, GridViewRowList[item].DisplayString, width);
30+
}
4431

45-
public sealed class RowMarkedEventArgs : EventArgs
46-
{
47-
public GridViewRow Row { get; set; }
48-
public bool OldValue { get; set; }
32+
public bool IsMarked(int item) => GridViewRowList[item].IsMarked;
4933

50-
}
34+
public void SetMark(int item, bool value)
35+
{
36+
var oldValue = GridViewRowList[item].IsMarked;
37+
GridViewRowList[item].IsMarked = value;
38+
var args = new RowMarkedEventArgs()
39+
{
40+
Row = GridViewRowList[item],
41+
OldValue = oldValue
42+
};
43+
MarkChanged?.Invoke(this, args);
44+
}
5145

52-
public event EventHandler<RowMarkedEventArgs> MarkChanged;
46+
public sealed class RowMarkedEventArgs : EventArgs
47+
{
48+
public GridViewRow Row { get; set; }
49+
public bool OldValue { get; set; }
5350

54-
public IList ToList()
55-
{
56-
return GridViewRowList;
57-
}
51+
}
5852

59-
// A slightly adapted method from gui.cs: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
60-
private static void RenderUstr(ConsoleDriver driver, ustring ustr, int width)
61-
{
62-
int used = 0;
63-
int index = 0;
64-
while (index < ustr.Length)
53+
public event EventHandler<RowMarkedEventArgs> MarkChanged;
54+
55+
public IList ToList()
6556
{
66-
(var rune, var size) = Utf8.DecodeRune(ustr, index, index - ustr.Length);
67-
var count = Rune.ColumnWidth(rune);
68-
if (used + count > width) break;
69-
driver.AddRune(rune);
70-
used += count;
71-
index += size;
57+
return GridViewRowList;
7258
}
7359

74-
while (used < width)
60+
// A slightly adapted method from gui.cs: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
61+
private static void RenderUstr(ConsoleDriver driver, ustring ustr, int width)
7562
{
76-
driver.AddRune(' ');
77-
used++;
63+
int used = 0;
64+
int index = 0;
65+
while (index < ustr.Length)
66+
{
67+
(var rune, var size) = Utf8.DecodeRune(ustr, index, index - ustr.Length);
68+
var count = Rune.ColumnWidth(rune);
69+
if (used + count > width) break;
70+
driver.AddRune(rune);
71+
used += count;
72+
index += size;
73+
}
74+
75+
while (used < width)
76+
{
77+
driver.AddRune(' ');
78+
used++;
79+
}
7880
}
7981
}
8082
}

src/Microsoft.PowerShell.ConsoleGuiTools/GridViewHelpers.cs

+43-41
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,55 @@
88

99
using Microsoft.PowerShell.ConsoleGuiTools.Models;
1010

11-
namespace Microsoft.PowerShell.ConsoleGuiTools;
12-
13-
internal static class GridViewHelpers
11+
namespace Microsoft.PowerShell.ConsoleGuiTools
1412
{
15-
// Add all items already selected plus any that match the filter
16-
// The selected items should be at the top of the list, in their original order
17-
internal static List<GridViewRow> FilterData(List<GridViewRow> listToFilter, string filter)
13+
14+
internal static class GridViewHelpers
1815
{
19-
var filteredList = new List<GridViewRow>();
20-
if (string.IsNullOrEmpty(filter))
16+
// Add all items already selected plus any that match the filter
17+
// The selected items should be at the top of the list, in their original order
18+
internal static List<GridViewRow> FilterData(List<GridViewRow> listToFilter, string filter)
2119
{
22-
return listToFilter;
23-
}
20+
var filteredList = new List<GridViewRow>();
21+
if (string.IsNullOrEmpty(filter))
22+
{
23+
return listToFilter;
24+
}
2425

25-
filteredList.AddRange(listToFilter.Where(gvr => gvr.IsMarked));
26-
filteredList.AddRange(listToFilter.Where(gvr => !gvr.IsMarked && Regex.IsMatch(gvr.DisplayString, filter, RegexOptions.IgnoreCase)));
26+
filteredList.AddRange(listToFilter.Where(gvr => gvr.IsMarked));
27+
filteredList.AddRange(listToFilter.Where(gvr => !gvr.IsMarked && Regex.IsMatch(gvr.DisplayString, filter, RegexOptions.IgnoreCase)));
2728

28-
return filteredList;
29-
}
30-
31-
/// <summary>
32-
/// Creates the header and data source for the GridView.
33-
/// </summary>
34-
/// <param name="listViewOffset"> Dictates where the header should actually start considering
35-
/// some offset is needed to factor in the checkboxes
36-
/// </param>
37-
/// <param name="applicationData"></param>
38-
/// <param name="leftMargin">Dictates where the header should actually start considering some offset is needed to factor in the checkboxes</param>
39-
/// <returns><see cref="GridViewHeader"/> and <see cref="GridViewDataSource"/> from commandlet inputs.</returns>
40-
internal static (GridViewHeader Header, GridViewDataSource DataSource) CreateGridViewInputs(int listViewOffset, int leftMargin, ApplicationData applicationData, object[] properties)
41-
{
42-
var table = FormatHelper.FormatTable(applicationData.Input, applicationData.Force, properties);
43-
44-
var gridViewHeader = new GridViewHeader
45-
{
46-
HeaderText = string.Concat(new string(' ', listViewOffset), table.Header),
47-
HeaderUnderLine = string.Concat(new string(' ', listViewOffset), table.HeaderLine)
48-
};
29+
return filteredList;
30+
}
4931

50-
var gridViewDataSource = new GridViewDataSource(table.Rows.Select((line, index) => new GridViewRow
32+
/// <summary>
33+
/// Creates the header and data source for the GridView.
34+
/// </summary>
35+
/// <param name="listViewOffset"> Dictates where the header should actually start considering
36+
/// some offset is needed to factor in the checkboxes
37+
/// </param>
38+
/// <param name="applicationData"></param>
39+
/// <param name="leftMargin">Dictates where the header should actually start considering some offset is needed to factor in the checkboxes</param>
40+
/// <returns><see cref="GridViewHeader"/> and <see cref="GridViewDataSource"/> from commandlet inputs.</returns>
41+
internal static (GridViewHeader Header, GridViewDataSource DataSource) CreateGridViewInputs(int listViewOffset, int leftMargin, ApplicationData applicationData, object[] properties)
5142
{
52-
DisplayString = line,
53-
OriginalIndex = index
54-
}));
55-
56-
return (
57-
gridViewHeader,
58-
gridViewDataSource);
43+
var table = FormatHelper.FormatTable(applicationData.Input, applicationData.Force, properties);
44+
45+
var gridViewHeader = new GridViewHeader
46+
{
47+
HeaderText = string.Concat(new string(' ', listViewOffset), table.Header),
48+
HeaderUnderLine = string.Concat(new string(' ', listViewOffset), table.HeaderLine)
49+
};
50+
51+
var gridViewDataSource = new GridViewDataSource(table.Rows.Select((line, index) => new GridViewRow
52+
{
53+
DisplayString = line,
54+
OriginalIndex = index
55+
}));
56+
57+
return (
58+
gridViewHeader,
59+
gridViewDataSource);
60+
}
5961
}
6062
}
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
namespace Microsoft.PowerShell.ConsoleGuiTools.Models;
4+
namespace Microsoft.PowerShell.ConsoleGuiTools.Models
5+
{
56

6-
using System.Collections.Generic;
7-
using System.Collections.ObjectModel;
8-
using System.Management.Automation;
7+
using System.Collections.Generic;
8+
using System.Collections.ObjectModel;
9+
using System.Management.Automation;
910

10-
internal class ApplicationData
11-
{
12-
public string Title { get; set; }
13-
public OutputModeOption OutputMode { get; set; }
14-
public bool PassThru { get; set; }
15-
public string Filter { get; set; }
16-
public bool MinUI { get; set; }
17-
18-
public bool UseNetDriver { get; set; }
19-
public bool Verbose { get; set; }
20-
public bool Debug { get; set; }
21-
22-
public string ModuleVersion { get; set; }
23-
24-
/// <summary>
25-
/// Get's the objects from the pipeline.
26-
/// </summary>
27-
public IReadOnlyList<PSObject> Input { get; init; }
28-
29-
/// <summary>
30-
/// Gets the Properties parameter.
31-
/// </summary>
32-
public object[] Properties { get; init; }
33-
34-
/// <summary>
35-
/// Gets the Force parameter.
36-
/// </summary>
37-
public bool Force { get; init; }
11+
internal class ApplicationData
12+
{
13+
public string Title { get; set; }
14+
public OutputModeOption OutputMode { get; set; }
15+
public bool PassThru { get; set; }
16+
public string Filter { get; set; }
17+
public bool MinUI { get; set; }
18+
19+
public bool UseNetDriver { get; set; }
20+
public bool Verbose { get; set; }
21+
public bool Debug { get; set; }
22+
23+
public string ModuleVersion { get; set; }
24+
25+
/// <summary>
26+
/// Get's the objects from the pipeline.
27+
/// </summary>
28+
public IReadOnlyList<PSObject> Input { get; init; }
29+
30+
/// <summary>
31+
/// Gets the Properties parameter.
32+
/// </summary>
33+
public object[] Properties { get; init; }
34+
35+
/// <summary>
36+
/// Gets the Force parameter.
37+
/// </summary>
38+
public bool Force { get; init; }
39+
}
3840
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
namespace Microsoft.PowerShell.ConsoleGuiTools.Models;
5-
6-
public enum OutputModeOption
4+
namespace Microsoft.PowerShell.ConsoleGuiTools.Models
75
{
8-
/// <summary>
9-
/// None is the default and it means OK and Cancel will not be present
10-
/// and no objects will be written to the pipeline.
11-
/// The selectionMode of the actual list will still be multiple.
12-
/// </summary>
13-
None,
14-
/// <summary>
15-
/// Allow selection of one single item to be written to the pipeline.
16-
/// </summary>
17-
Single,
18-
/// <summary>
19-
///Allow select of multiple items to be written to the pipeline.
20-
/// </summary>
21-
Multiple
6+
public enum OutputModeOption
7+
{
8+
/// <summary>
9+
/// None is the default and it means OK and Cancel will not be present
10+
/// and no objects will be written to the pipeline.
11+
/// The selectionMode of the actual list will still be multiple.
12+
/// </summary>
13+
None,
14+
/// <summary>
15+
/// Allow selection of one single item to be written to the pipeline.
16+
/// </summary>
17+
Single,
18+
/// <summary>
19+
///Allow select of multiple items to be written to the pipeline.
20+
/// </summary>
21+
Multiple
22+
}
2223
}

src/Microsoft.PowerShell.ConsoleGuiTools/Models/Serializers.cs

+30-28
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,38 @@
66
using System.Text;
77
//TODO: switch to JSON.NET
88

9-
namespace Microsoft.PowerShell.ConsoleGuiTools.Models;
10-
11-
public class Serializers
9+
namespace Microsoft.PowerShell.ConsoleGuiTools.Models
1210
{
13-
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
14-
{
15-
TypeNameHandling = TypeNameHandling.All
16-
};
17-
18-
public static string ObjectToJson<T>(T obj)
19-
{
20-
var jsonString = JsonConvert.SerializeObject(obj, jsonSerializerSettings);
21-
22-
return ToBase64String(jsonString);
23-
}
24-
25-
public static T ObjectFromJson<T>(string base64Json)
26-
{
27-
var jsonString = FromBase64String(base64Json);
28-
29-
return JsonConvert.DeserializeObject<T>(jsonString, jsonSerializerSettings);
30-
}
31-
32-
private static string FromBase64String(string base64string)
33-
{
34-
return Encoding.UTF8.GetString(Convert.FromBase64String(base64string));
35-
}
3611

37-
private static string ToBase64String(string str)
12+
public class Serializers
3813
{
39-
return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
14+
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
15+
{
16+
TypeNameHandling = TypeNameHandling.All
17+
};
18+
19+
public static string ObjectToJson<T>(T obj)
20+
{
21+
var jsonString = JsonConvert.SerializeObject(obj, jsonSerializerSettings);
22+
23+
return ToBase64String(jsonString);
24+
}
25+
26+
public static T ObjectFromJson<T>(string base64Json)
27+
{
28+
var jsonString = FromBase64String(base64Json);
29+
30+
return JsonConvert.DeserializeObject<T>(jsonString, jsonSerializerSettings);
31+
}
32+
33+
private static string FromBase64String(string base64string)
34+
{
35+
return Encoding.UTF8.GetString(Convert.FromBase64String(base64string));
36+
}
37+
38+
private static string ToBase64String(string str)
39+
{
40+
return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
41+
}
4042
}
4143
}

0 commit comments

Comments
 (0)