Skip to content

Commit 3ac5492

Browse files
authored
Feature: Powershell color profile (#1613)
* Docs: Methods documented * Feature: Windows PowerShell global profile * Fix: Hide context menu items if profile is dynamic * Chore: Code refactoring * Chore: Code cleanup * Feature: PowerShell profile * Feature: PowerShell profile * Feature: Add documentation for profiles * Docs: PowerShell profile help * Chore: Code cleanup & improvements * Feature: PowerShell profile * Fix: Dashboard custom IPv4 api could not be set * Docs: Add #1613
1 parent 8537e65 commit 3ac5492

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+526
-243
lines changed

Source/NETworkManager.Documentation/DocumentationIdentifier.cs

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,6 @@ public enum DocumentationIdentifier
1010
/// </summary>
1111
Default,
1212

13-
/// <summary>
14-
/// Settings\General documentation page.
15-
/// </summary>
16-
SettingsGeneral,
17-
18-
/// <summary>
19-
/// Settings\Window documentation page.
20-
/// </summary>
21-
SettingsWindow,
22-
23-
/// <summary>
24-
/// Settings\Appearance documentation page.
25-
/// </summary>
26-
SettingsAppearance,
27-
28-
/// <summary>
29-
/// Settings\Language documentation page.
30-
/// </summary>
31-
SettingsLanguage,
32-
33-
/// <summary>
34-
/// Settings\Status documentation page.
35-
/// </summary>
36-
SettingsStatus,
37-
38-
/// <summary>
39-
/// Settings\HotKeys documentation page.
40-
/// </summary>
41-
SettingsHotKeys,
42-
43-
/// <summary>
44-
/// Settings\Autostart documentation page.
45-
/// </summary>
46-
SettingsAutostart,
47-
48-
/// <summary>
49-
/// Settings\Update documentation page.
50-
/// </summary>
51-
SettingsUpdate,
52-
53-
/// <summary>
54-
/// Settings\Profiles documentation page.
55-
/// </summary>
56-
SettingsProfiles,
57-
58-
/// <summary>
59-
/// Settings\Settings documentation page.
60-
/// </summary>
61-
SettingsSettings,
62-
6313
/// <summary>
6414
/// Dashboard documentation page.
6515
/// </summary>
@@ -175,6 +125,61 @@ public enum DocumentationIdentifier
175125
/// </summary>
176126
ApplicationArpTable,
177127

128+
/// <summary>
129+
/// Settings\General documentation page.
130+
/// </summary>
131+
SettingsGeneral,
132+
133+
/// <summary>
134+
/// Settings\Window documentation page.
135+
/// </summary>
136+
SettingsWindow,
137+
138+
/// <summary>
139+
/// Settings\Appearance documentation page.
140+
/// </summary>
141+
SettingsAppearance,
142+
143+
/// <summary>
144+
/// Settings\Language documentation page.
145+
/// </summary>
146+
SettingsLanguage,
147+
148+
/// <summary>
149+
/// Settings\Status documentation page.
150+
/// </summary>
151+
SettingsStatus,
152+
153+
/// <summary>
154+
/// Settings\HotKeys documentation page.
155+
/// </summary>
156+
SettingsHotKeys,
157+
158+
/// <summary>
159+
/// Settings\Autostart documentation page.
160+
/// </summary>
161+
SettingsAutostart,
162+
163+
/// <summary>
164+
/// Settings\Update documentation page.
165+
/// </summary>
166+
SettingsUpdate,
167+
168+
/// <summary>
169+
/// Settings\Profiles documentation page.
170+
/// </summary>
171+
SettingsProfiles,
172+
173+
/// <summary>
174+
/// Settings\Settings documentation page.
175+
/// </summary>
176+
SettingsSettings,
177+
178+
/// <summary>
179+
/// Profiles documentation page.
180+
/// </summary>
181+
Profiles,
182+
178183
/// <summary>
179184
/// Command line arguments.
180185
/// </summary>

Source/NETworkManager.Documentation/DocumentationManager.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@ public static class DocumentationManager
2222
/// </summary>
2323
private static List<DocumentationInfo> List => new()
2424
{
25-
new DocumentationInfo(DocumentationIdentifier.SettingsGeneral, @"Documentation/Settings/General"),
26-
new DocumentationInfo(DocumentationIdentifier.SettingsWindow, @"Documentation/Settings/Window"),
27-
new DocumentationInfo(DocumentationIdentifier.SettingsAppearance, @"Documentation/Settings/Appearance"),
28-
new DocumentationInfo(DocumentationIdentifier.SettingsLanguage, @"Documentation/Settings/Language"),
29-
new DocumentationInfo(DocumentationIdentifier.SettingsStatus, @"Documentation/Settings/Status"),
30-
new DocumentationInfo(DocumentationIdentifier.SettingsHotKeys, @"Documentation/Settings/HotKeys"),
31-
new DocumentationInfo(DocumentationIdentifier.SettingsAutostart, @"Documentation/Settings/Autostart"),
32-
new DocumentationInfo(DocumentationIdentifier.SettingsUpdate, @"Documentation/Settings/Update"),
33-
new DocumentationInfo(DocumentationIdentifier.SettingsProfiles, @"Documentation/Settings/Profiles"),
34-
new DocumentationInfo(DocumentationIdentifier.SettingsSettings, @"Documentation/Settings/Settings"),
3525
new DocumentationInfo(DocumentationIdentifier.ApplicationDashboard, @"Documentation/Application/Dashboard"),
3626
new DocumentationInfo(DocumentationIdentifier.ApplicationNetworkInterface, @"Documentation/Application/NetworkInterface"),
3727
new DocumentationInfo(DocumentationIdentifier.ApplicationWiFi, @"Documentation/Application/WiFi"),
@@ -55,6 +45,17 @@ public static class DocumentationManager
5545
new DocumentationInfo(DocumentationIdentifier.ApplicationConnections, @"Documentation/Application/Connection"),
5646
new DocumentationInfo(DocumentationIdentifier.ApplicationListeners, @"Documentation/Application/Listeners"),
5747
new DocumentationInfo(DocumentationIdentifier.ApplicationArpTable, @"Documentation/Application/ARPTable"),
48+
new DocumentationInfo(DocumentationIdentifier.SettingsGeneral, @"Documentation/Settings/General"),
49+
new DocumentationInfo(DocumentationIdentifier.SettingsWindow, @"Documentation/Settings/Window"),
50+
new DocumentationInfo(DocumentationIdentifier.SettingsAppearance, @"Documentation/Settings/Appearance"),
51+
new DocumentationInfo(DocumentationIdentifier.SettingsLanguage, @"Documentation/Settings/Language"),
52+
new DocumentationInfo(DocumentationIdentifier.SettingsStatus, @"Documentation/Settings/Status"),
53+
new DocumentationInfo(DocumentationIdentifier.SettingsHotKeys, @"Documentation/Settings/HotKeys"),
54+
new DocumentationInfo(DocumentationIdentifier.SettingsAutostart, @"Documentation/Settings/Autostart"),
55+
new DocumentationInfo(DocumentationIdentifier.SettingsUpdate, @"Documentation/Settings/Update"),
56+
new DocumentationInfo(DocumentationIdentifier.SettingsProfiles, @"Documentation/Settings/Profiles"),
57+
new DocumentationInfo(DocumentationIdentifier.SettingsSettings, @"Documentation/Settings/Settings"),
58+
new DocumentationInfo(DocumentationIdentifier.Profiles, @"Documentation/Profiles/Profiles"),
5859
new DocumentationInfo(DocumentationIdentifier.CommandLineArguments, @"Documentation/CommandLine/CommandLineArguments"),
5960
};
6061

Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/NETworkManager.Localization/Resources/Strings.resx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@
560560
</data>
561561
<data name="RestartRequired" xml:space="preserve">
562562
<value>Restart required</value>
563-
</data>
563+
</data>
564564
<data name="SaveSettingsInApplicationFolder" xml:space="preserve">
565565
<value>Save settings in the application folder</value>
566566
</data>
@@ -3227,4 +3227,19 @@ If not set, the default AWS CLI settings are used.</value>
32273227
<data name="Command" xml:space="preserve">
32283228
<value>Command</value>
32293229
</data>
3230+
<data name="ModifyPowerShellConsoleGlobalProfilesToMatchAppTheme" xml:space="preserve">
3231+
<value>Modify the PowerShell console global profile(s) to match the application theme</value>
3232+
</data>
3233+
<data name="PowerShellConsoleColorCanBeChangedUnderGeneralApperance" xml:space="preserve">
3234+
<value>The color of the PowerShell console can be changed to the application theme under General &gt; Appearance</value>
3235+
</data>
3236+
<data name="HelpMessage_ModifyPowerShellConsoleGlobalProfilesToMatchAppTheme" xml:space="preserve">
3237+
<value>If you enable this option, the default PowerShell console settings in the registry under HKCU:\Console are overridden so that the PowerShell console window matches the application theme. This is a global system setting that may affect the appearance of the normal PowerShell window.
3238+
3239+
Only the PowerShell consoles configured in the PowerShell and AWS Session Manager settings are modified. Both Windows PowerShell and PWSH (PowerShell 7 and higher) are supported.
3240+
3241+
Click in the upper right corner on the help / documentation button (left of the minimize button) or navigate to https://borntoberoot.net/NETworkManager/Documentation/Settings/Appearance for more details. Here are the values documented which will be adjusted.
3242+
3243+
If the option is disabled again, the values are no longer modified. However, the original values are NOT restored.</value>
3244+
</data>
32303245
</root>

Source/NETworkManager.Models/PowerShell/PowerShell.cs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,101 @@
1-
namespace NETworkManager.Models.PowerShell
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace NETworkManager.Models.PowerShell
27
{
38
public static partial class PowerShell
49
{
10+
11+
/// <summary>
12+
/// Default SZ registry keys for the global PowerShell profile.
13+
/// </summary>
14+
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSZBase = new()
15+
{
16+
new Tuple<string, string>("FaceName", "Consolas"),
17+
18+
};
19+
20+
private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase = new()
21+
{
22+
new Tuple<string, int>("CursorType", 1),
23+
new Tuple<string, int>("FontFamiliy", 54), // 36
24+
new Tuple<string, int>("FontSize", 1179648), // 120000
25+
new Tuple<string, int>("FontWeight", 400) // 190
26+
};
27+
28+
private static List<Tuple<string, int>> GetProfileRegkeysDwordDark()
29+
{
30+
return DefaultProfileRegkeysDwordBase.Concat(
31+
new[] {
32+
new Tuple<string, int>("DefaultBackground", 2434341 ), // HEX: 252525
33+
new Tuple<string, int>("ColorTable00", 2434341), // HEX: 252525
34+
new Tuple<string, int>("ColorTable07", 13421772), // HEX: cccccc
35+
}).ToList();
36+
}
37+
private static List<Tuple<string, int>> GetProfileRegkeysDwordWhite()
38+
{
39+
return DefaultProfileRegkeysDwordBase.Concat(
40+
new[] {
41+
new Tuple<string, int>("DefaultBackground", 16777215 ), // HEX: FFFFFF
42+
new Tuple<string, int>("ColorTable00", 16777215), // HEX: FFFFFF
43+
new Tuple<string, int>("ColorTable07", 2434341), // HEX: 252525
44+
}).ToList();
45+
}
46+
47+
private static List<string> DefaultProfileRegkeysDwordDelete = new()
48+
{
49+
"ScreenColors"
50+
};
51+
52+
public static void WriteDefaultProfileToRegistry(string theme, string powerShellPath)
53+
{
54+
var registryPath = @"Console\";
55+
56+
var systemRoot = Environment.ExpandEnvironmentVariables("%SystemRoot%");
57+
58+
// Windows PowerShell --> HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
59+
if (powerShellPath.StartsWith(systemRoot))
60+
registryPath += "%SystemRoot%" + powerShellPath.Substring(systemRoot.Length, powerShellPath.Length - systemRoot.Length).Replace(@"\", "_");
61+
// PWSH --> HKCU:\Console\C:_Program Files_PowerShell_7_pwsh.exe
62+
else
63+
registryPath += powerShellPath.Replace(@"\", "_");
64+
65+
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(registryPath, true);
66+
67+
registryKey ??= Registry.CurrentUser.CreateSubKey(registryPath);
68+
69+
if (registryKey != null)
70+
{
71+
foreach (var item in theme == "Dark" ? GetProfileRegkeysDwordDark() : GetProfileRegkeysDwordWhite())
72+
registryKey.SetValue(item.Item1, item.Item2);
73+
74+
foreach (var item in DefaultProfileRegkeysSZBase)
75+
registryKey.SetValue(item.Item1, item.Item2);
76+
77+
foreach (var item in DefaultProfileRegkeysDwordDelete)
78+
{
79+
registryKey.DeleteValue(item, false);
80+
}
81+
}
82+
83+
registryKey.Close();
84+
}
85+
86+
/// <summary>
87+
/// Build command line arguments based on a <see cref="PowerShellSessionInfo"/>.
88+
/// </summary>
89+
/// <param name="sessionInfo">Instance of <see cref="PowerShellSessionInfo"/>.</param>
90+
/// <returns>Command line arguments like "-NoExit -ExecutionPolicy RemoteSigned ...".</returns>
591
public static string BuildCommandLine(PowerShellSessionInfo sessionInfo)
692
{
793
var command = $"-NoExit -ExecutionPolicy {sessionInfo.ExecutionPolicy} {sessionInfo.AdditionalCommandLine}";
894

995
// Connect to remote host or execute local command if configured
1096
if (sessionInfo.EnableRemoteConsole)
1197
command += $" -Command \"& {{Enter-PSSession -ComputerName {sessionInfo.Host}}}\"";
12-
else if(!string.IsNullOrEmpty(sessionInfo.Command))
98+
else if (!string.IsNullOrEmpty(sessionInfo.Command))
1399
command += $" -Command \"& {{{sessionInfo.Command}}}\"";
14100

15101
return command;

0 commit comments

Comments
 (0)