Skip to content

Commit

Permalink
Feature: Powershell color profile (#1613)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
BornToBeRoot authored Oct 16, 2022
1 parent 8537e65 commit 3ac5492
Show file tree
Hide file tree
Showing 41 changed files with 526 additions and 243 deletions.
105 changes: 55 additions & 50 deletions Source/NETworkManager.Documentation/DocumentationIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,6 @@ public enum DocumentationIdentifier
/// </summary>
Default,

/// <summary>
/// Settings\General documentation page.
/// </summary>
SettingsGeneral,

/// <summary>
/// Settings\Window documentation page.
/// </summary>
SettingsWindow,

/// <summary>
/// Settings\Appearance documentation page.
/// </summary>
SettingsAppearance,

/// <summary>
/// Settings\Language documentation page.
/// </summary>
SettingsLanguage,

/// <summary>
/// Settings\Status documentation page.
/// </summary>
SettingsStatus,

/// <summary>
/// Settings\HotKeys documentation page.
/// </summary>
SettingsHotKeys,

/// <summary>
/// Settings\Autostart documentation page.
/// </summary>
SettingsAutostart,

/// <summary>
/// Settings\Update documentation page.
/// </summary>
SettingsUpdate,

/// <summary>
/// Settings\Profiles documentation page.
/// </summary>
SettingsProfiles,

/// <summary>
/// Settings\Settings documentation page.
/// </summary>
SettingsSettings,

/// <summary>
/// Dashboard documentation page.
/// </summary>
Expand Down Expand Up @@ -175,6 +125,61 @@ public enum DocumentationIdentifier
/// </summary>
ApplicationArpTable,

/// <summary>
/// Settings\General documentation page.
/// </summary>
SettingsGeneral,

/// <summary>
/// Settings\Window documentation page.
/// </summary>
SettingsWindow,

/// <summary>
/// Settings\Appearance documentation page.
/// </summary>
SettingsAppearance,

/// <summary>
/// Settings\Language documentation page.
/// </summary>
SettingsLanguage,

/// <summary>
/// Settings\Status documentation page.
/// </summary>
SettingsStatus,

/// <summary>
/// Settings\HotKeys documentation page.
/// </summary>
SettingsHotKeys,

/// <summary>
/// Settings\Autostart documentation page.
/// </summary>
SettingsAutostart,

/// <summary>
/// Settings\Update documentation page.
/// </summary>
SettingsUpdate,

/// <summary>
/// Settings\Profiles documentation page.
/// </summary>
SettingsProfiles,

/// <summary>
/// Settings\Settings documentation page.
/// </summary>
SettingsSettings,

/// <summary>
/// Profiles documentation page.
/// </summary>
Profiles,

/// <summary>
/// Command line arguments.
/// </summary>
Expand Down
21 changes: 11 additions & 10 deletions Source/NETworkManager.Documentation/DocumentationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ public static class DocumentationManager
/// </summary>
private static List<DocumentationInfo> List => new()
{
new DocumentationInfo(DocumentationIdentifier.SettingsGeneral, @"Documentation/Settings/General"),
new DocumentationInfo(DocumentationIdentifier.SettingsWindow, @"Documentation/Settings/Window"),
new DocumentationInfo(DocumentationIdentifier.SettingsAppearance, @"Documentation/Settings/Appearance"),
new DocumentationInfo(DocumentationIdentifier.SettingsLanguage, @"Documentation/Settings/Language"),
new DocumentationInfo(DocumentationIdentifier.SettingsStatus, @"Documentation/Settings/Status"),
new DocumentationInfo(DocumentationIdentifier.SettingsHotKeys, @"Documentation/Settings/HotKeys"),
new DocumentationInfo(DocumentationIdentifier.SettingsAutostart, @"Documentation/Settings/Autostart"),
new DocumentationInfo(DocumentationIdentifier.SettingsUpdate, @"Documentation/Settings/Update"),
new DocumentationInfo(DocumentationIdentifier.SettingsProfiles, @"Documentation/Settings/Profiles"),
new DocumentationInfo(DocumentationIdentifier.SettingsSettings, @"Documentation/Settings/Settings"),
new DocumentationInfo(DocumentationIdentifier.ApplicationDashboard, @"Documentation/Application/Dashboard"),
new DocumentationInfo(DocumentationIdentifier.ApplicationNetworkInterface, @"Documentation/Application/NetworkInterface"),
new DocumentationInfo(DocumentationIdentifier.ApplicationWiFi, @"Documentation/Application/WiFi"),
Expand All @@ -55,6 +45,17 @@ public static class DocumentationManager
new DocumentationInfo(DocumentationIdentifier.ApplicationConnections, @"Documentation/Application/Connection"),
new DocumentationInfo(DocumentationIdentifier.ApplicationListeners, @"Documentation/Application/Listeners"),
new DocumentationInfo(DocumentationIdentifier.ApplicationArpTable, @"Documentation/Application/ARPTable"),
new DocumentationInfo(DocumentationIdentifier.SettingsGeneral, @"Documentation/Settings/General"),
new DocumentationInfo(DocumentationIdentifier.SettingsWindow, @"Documentation/Settings/Window"),
new DocumentationInfo(DocumentationIdentifier.SettingsAppearance, @"Documentation/Settings/Appearance"),
new DocumentationInfo(DocumentationIdentifier.SettingsLanguage, @"Documentation/Settings/Language"),
new DocumentationInfo(DocumentationIdentifier.SettingsStatus, @"Documentation/Settings/Status"),
new DocumentationInfo(DocumentationIdentifier.SettingsHotKeys, @"Documentation/Settings/HotKeys"),
new DocumentationInfo(DocumentationIdentifier.SettingsAutostart, @"Documentation/Settings/Autostart"),
new DocumentationInfo(DocumentationIdentifier.SettingsUpdate, @"Documentation/Settings/Update"),
new DocumentationInfo(DocumentationIdentifier.SettingsProfiles, @"Documentation/Settings/Profiles"),
new DocumentationInfo(DocumentationIdentifier.SettingsSettings, @"Documentation/Settings/Settings"),
new DocumentationInfo(DocumentationIdentifier.Profiles, @"Documentation/Profiles/Profiles"),
new DocumentationInfo(DocumentationIdentifier.CommandLineArguments, @"Documentation/CommandLine/CommandLineArguments"),
};

Expand Down
31 changes: 31 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@
</data>
<data name="RestartRequired" xml:space="preserve">
<value>Restart required</value>
</data>
</data>
<data name="SaveSettingsInApplicationFolder" xml:space="preserve">
<value>Save settings in the application folder</value>
</data>
Expand Down Expand Up @@ -3227,4 +3227,19 @@ If not set, the default AWS CLI settings are used.</value>
<data name="Command" xml:space="preserve">
<value>Command</value>
</data>
<data name="ModifyPowerShellConsoleGlobalProfilesToMatchAppTheme" xml:space="preserve">
<value>Modify the PowerShell console global profile(s) to match the application theme</value>
</data>
<data name="PowerShellConsoleColorCanBeChangedUnderGeneralApperance" xml:space="preserve">
<value>The color of the PowerShell console can be changed to the application theme under General &gt; Appearance</value>
</data>
<data name="HelpMessage_ModifyPowerShellConsoleGlobalProfilesToMatchAppTheme" xml:space="preserve">
<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.

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.

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.

If the option is disabled again, the values are no longer modified. However, the original values are NOT restored.</value>
</data>
</root>
90 changes: 88 additions & 2 deletions Source/NETworkManager.Models/PowerShell/PowerShell.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,101 @@
namespace NETworkManager.Models.PowerShell
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NETworkManager.Models.PowerShell
{
public static partial class PowerShell
{

/// <summary>
/// Default SZ registry keys for the global PowerShell profile.
/// </summary>
private static readonly List<Tuple<string, string>> DefaultProfileRegkeysSZBase = new()
{
new Tuple<string, string>("FaceName", "Consolas"),

};

private static readonly List<Tuple<string, int>> DefaultProfileRegkeysDwordBase = new()
{
new Tuple<string, int>("CursorType", 1),
new Tuple<string, int>("FontFamiliy", 54), // 36
new Tuple<string, int>("FontSize", 1179648), // 120000
new Tuple<string, int>("FontWeight", 400) // 190
};

private static List<Tuple<string, int>> GetProfileRegkeysDwordDark()
{
return DefaultProfileRegkeysDwordBase.Concat(
new[] {
new Tuple<string, int>("DefaultBackground", 2434341 ), // HEX: 252525
new Tuple<string, int>("ColorTable00", 2434341), // HEX: 252525
new Tuple<string, int>("ColorTable07", 13421772), // HEX: cccccc
}).ToList();
}
private static List<Tuple<string, int>> GetProfileRegkeysDwordWhite()
{
return DefaultProfileRegkeysDwordBase.Concat(
new[] {
new Tuple<string, int>("DefaultBackground", 16777215 ), // HEX: FFFFFF
new Tuple<string, int>("ColorTable00", 16777215), // HEX: FFFFFF
new Tuple<string, int>("ColorTable07", 2434341), // HEX: 252525
}).ToList();
}

private static List<string> DefaultProfileRegkeysDwordDelete = new()
{
"ScreenColors"
};

public static void WriteDefaultProfileToRegistry(string theme, string powerShellPath)
{
var registryPath = @"Console\";

var systemRoot = Environment.ExpandEnvironmentVariables("%SystemRoot%");

// Windows PowerShell --> HKCU:\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
if (powerShellPath.StartsWith(systemRoot))
registryPath += "%SystemRoot%" + powerShellPath.Substring(systemRoot.Length, powerShellPath.Length - systemRoot.Length).Replace(@"\", "_");
// PWSH --> HKCU:\Console\C:_Program Files_PowerShell_7_pwsh.exe
else
registryPath += powerShellPath.Replace(@"\", "_");

RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(registryPath, true);

registryKey ??= Registry.CurrentUser.CreateSubKey(registryPath);

if (registryKey != null)
{
foreach (var item in theme == "Dark" ? GetProfileRegkeysDwordDark() : GetProfileRegkeysDwordWhite())
registryKey.SetValue(item.Item1, item.Item2);

foreach (var item in DefaultProfileRegkeysSZBase)
registryKey.SetValue(item.Item1, item.Item2);

foreach (var item in DefaultProfileRegkeysDwordDelete)
{
registryKey.DeleteValue(item, false);
}
}

registryKey.Close();
}

/// <summary>
/// Build command line arguments based on a <see cref="PowerShellSessionInfo"/>.
/// </summary>
/// <param name="sessionInfo">Instance of <see cref="PowerShellSessionInfo"/>.</param>
/// <returns>Command line arguments like "-NoExit -ExecutionPolicy RemoteSigned ...".</returns>
public static string BuildCommandLine(PowerShellSessionInfo sessionInfo)
{
var command = $"-NoExit -ExecutionPolicy {sessionInfo.ExecutionPolicy} {sessionInfo.AdditionalCommandLine}";

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

return command;
Expand Down
Loading

0 comments on commit 3ac5492

Please sign in to comment.