Skip to content

Instantly share code, notes, and snippets.

@alexandair
Created October 30, 2020 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandair/09e4ec3d90ccc23a6e8eea1bda4f24e9 to your computer and use it in GitHub Desktop.
Save alexandair/09e4ec3d90ccc23a6e8eea1bda4f24e9 to your computer and use it in GitHub Desktop.
$options = @{
AddToHistoryHandler = {
param( [string]$line)
return $line.Length -gt 3 -and $line[0] -ne ' ' -and $line[0] -ne ';'
}
}
Set-PSReadLineOption @options
@SeeminglyScience
Copy link

SeeminglyScience commented Oct 30, 2020

Here's an example workaround using ILAssembler:

$handler = il ([Func[string, object]]) {
    ldarg.0
    brfalse.s fail_test

    ldarg.0
    callvirt { [int] [string].get_Length() }
    ldc.i4.3
    ble.s fail_test

    ldarg.0
    ldc.i4.0
    callvirt { [char] [string].get_Chars([int]) }
    ldc.i4.s 32
    beq.s fail_test

    ldarg.0
    ldc.i4.0
    callvirt { [char] [string].get_Chars([int]) }
    ldc.i4.s 59
    ceq
    ldc.i4.0
    ceq
    box { [bool] }
    ret

fail_test:
    ldc.i4.0
    box { [bool] }
    ret
}

Set-PSReadLineOption -AddToHistoryHandler $handler

@SeeminglyScience
Copy link

Add-Type version:

Add-Type -TypeDefinition @'
namespace ProfileUtility
{
    public class PSRLUtilities
    {
        public static object AddToHistoryHandler(string line)
        {
            return line != null && line.Length > 3 && line[0] != ' ' && line[0] != ';';
        }
    }
}
'@


$handler = [ProfileUtility.PSRLUtilities].
    GetMethod('AddToHistoryHandler').
    CreateDelegate([Func[string, object]])

Set-PSReadLineOption -AddToHistoryHandler $handler

Probably slower startup time than the ILAssembler method, but for sure easier to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment