Skip to content

Instantly share code, notes, and snippets.

@alexandair
Created October 30, 2020 23:09
Show Gist options
  • 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

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