Skip to content

Commit

Permalink
Merge pull request #85 from mdgrs-mei/add-string-constructor
Browse files Browse the repository at this point in the history
Add conversion from string to ActionKey and RemapKey
  • Loading branch information
mdgrs-mei authored Nov 4, 2024
2 parents 43627aa + 860d38e commit 38cba4b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/PowerShellRun/Application/ActionKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public ActionKey(KeyCombination keyCombination, string description)
Description = description;
}

public ActionKey(string str)
{
var keyAndDescription = str.Split(':');
var key = keyAndDescription[0];
KeyCombination = new KeyCombination(key);
if (keyAndDescription.Length > 1)
{
Description = keyAndDescription[1];
}
}

private ActionKey()
{
}
Expand Down
11 changes: 11 additions & 0 deletions src/PowerShellRun/Application/RemapKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ public RemapKey(KeyCombination source, KeyCombination destination)
Destination = destination;
}

public RemapKey(string str)
{
var sourceAndDestination = str.Split(':');
var source = sourceAndDestination[0];
Source = new KeyCombination(source);
if (sourceAndDestination.Length > 1)
{
Destination = new KeyCombination(sourceAndDestination[1]);
}
}

private RemapKey()
{
}
Expand Down
6 changes: 5 additions & 1 deletion src/PowerShellRun/Base/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void SetFromString(string str)
if (string.IsNullOrEmpty(str))
{
SetString();
return;
throw new ArgumentException("Can't construct KeyCombination from an empty string.");
}

var keys = str.Split('+');
Expand Down Expand Up @@ -147,6 +147,10 @@ private void SetFromString(string str)
}

SetString();
if (_modifier == KeyModifier.None && _key == Key.None)
{
throw new ArgumentException($"Invalid string format [{str}]. The format must be like 'Ctrl+Shift+A'.");
}
}

internal static KeyCombination LeftArrow { get; } = new KeyCombination(KeyModifier.None, Key.LeftArrow);
Expand Down
18 changes: 18 additions & 0 deletions tests/Public/ActionKey.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Describe 'ActionKey' {
BeforeEach {
Import-Module $PSScriptRoot/../../module/PowerShellRun -Force
}

It 'is set by a string' {
[PowerShellRun.ActionKey]$actionKey = 'A:Hello'
$actionKey.ToString() | Should -Be ([PowerShellRun.ActionKey]::new('A', 'Hello').ToString())
}

It 'should throw with an invalid string' {
{ [PowerShellRun.ActionKey]$actionKey = 'Hello' } | Should -Throw
}

AfterEach {
Remove-Module PowerShellRun -Force
}
}
4 changes: 2 additions & 2 deletions tests/Public/FontColor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Import-Module $PSScriptRoot/../../module/PowerShellRun -Force
}

It 'should be able to be set by a preset name' {
It 'is set by a preset name' {
$option = Get-PSRunDefaultSelectorOption
$theme = $option.Theme
$theme.DefaultForegroundColor = 'Black'
$theme.DefaultForegroundColor -eq [PowerShellRun.FontColor]::Black | Should -BeTrue
}

It 'should be able to be set by a hex string' {
It 'is set by a hex string' {
$hex = '#20F230'
$option = Get-PSRunDefaultSelectorOption
$theme = $option.Theme
Expand Down
18 changes: 18 additions & 0 deletions tests/Public/RemapKey.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Describe 'RemapKey' {
BeforeEach {
Import-Module $PSScriptRoot/../../module/PowerShellRun -Force
}

It 'is set by a string' {
[PowerShellRun.RemapKey]$remapKey = 'A:B'
$remapKey.ToString() | Should -Be ([PowerShellRun.RemapKey]::new('A', 'B').ToString())
}

It 'should throw with an invalid string' {
{ [PowerShellRun.RemapKey]$remapKey = 'A:hello' } | Should -Throw
}

AfterEach {
Remove-Module PowerShellRun -Force
}
}

0 comments on commit 38cba4b

Please sign in to comment.