plugins/psreadline/Load.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
$PreLoad = {
    if (-not (Test-OMPProfileSetting -Name 'PSReadlineHistoryPath')) {
        try {
            Add-OMPProfileSetting -Name 'PSReadlineHistoryPath' -Value (Join-Path (Split-Path $Profile) '.powershell.history')
        }
        catch {
            throw 'Unable to add psreadline profile settting PSReadlineHistoryPath!'
        }

        if (Test-Path (Get-OMPProfileSetting -Name 'PSReadlineHistoryPath')) {
            Write-Output "NOTE: PSReadline history file already exists: $(Join-Path (Split-Path $Profile) '.powershell.history')"
        }
    }
    if (-not (Test-OMPProfileSetting -Name 'PSReadlineHistoryLoaded')) {
        try {
            Add-OMPProfileSetting -Name 'PSReadlineHistoryLoaded' -Value $false
        }
        catch {
            throw 'Unable to add psreadline profile settting PSReadlineHistoryLoaded!'
        }
    }
    if ((Test-Path "$(Get-OMPProfileSetting -Name 'PSReadlineHistoryPath').csv") -and
         (-not (Get-OMPProfileSetting -Name 'PSReadlineHistoryLoaded'))) {
        $null = Import-CSV "$(Get-OMPProfileSetting -Name 'PSReadlineHistoryPath').csv" | Add-History
        Set-OMPProfileSetting -Name 'PSReadlineHistoryLoaded' -Value $true
    }
    if (-not (Test-OMPProfileSetting -Name 'PSReadlinePersistantHistoryCount')) {
        try {
            Add-OMPProfileSetting -Name 'PSReadlinePersistantHistoryCount' -Value 50
        }
        catch {
            throw 'Unable to add psreadline profile settting PSReadlinePersistantHistoryCount!'
        }
    }
    Import-OMPModule 'PSReadline'
}

$PostLoad = {}
$Config = {
    <#
        psreadline configuration
    #>

    Set-PSReadLineOption -HistoryNoDuplicates
    Set-PSReadlineOption -HistorySavePath (Get-OMPProfileSetting -Name 'PSReadlineHistoryPath')
    Set-PSReadLineOption -HistorySearchCursorMovesToEnd
    Set-PSReadLineOption -HistorySaveStyle SaveIncrementally
    Set-PSReadLineOption -MaximumHistoryCount 4000
    Set-PSReadlineOption -ShowToolTips:$true

    Set-PSReadlineKeyHandler -Key "Ctrl+Delete"       -Function "KillWord"
    Set-PSReadlineKeyHandler -Key "Ctrl+Backspace"    -Function "BackwardKillWord"
    Set-PSReadlineKeyHandler -Key "Shift+Backspace"   -Function "BackwardKillWord"
    Set-PSReadlineKeyHandler -Key "UpArrow"           -Function "HistorySearchBackward"
    Set-PSReadlineKeyHandler -Key "DownArrow"         -Function "HistorySearchForward"
    Set-PSReadlineKeyHandler -Key "Tab"               -Function "MenuComplete"
    Set-PSReadlineKeyHandler -Chord 'Shift+Tab' -Function "Complete"
    Set-PSReadlineKeyHandler -Key "Ctrl+Q"            -Function "TabCompleteNext"
    Set-PSReadlineKeyHandler -Key "Ctrl+Shift+Q"      -Function "TabCompletePrevious"

    Set-PSReadlineKeyHandler -Key F1 -BriefDescription CommandHelp -LongDescription "Open the help window for the current command" -ScriptBlock {
        # Get current line(s) of input
        $ast = $null
        $cursor = $null
        [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$null, [ref]$null, [ref]$cursor)

        # Find the current command, use Ast to find the currently processed command, even if we are currently typing parameters for it.
        $commandAst = $ast.FindAll({
                $node = $args[0]
                $node -is [System.Management.Automation.Language.CommandAst] -and
                $node.Extent.StartOffset -le $cursor -and
                $node.Extent.EndOffset -ge $cursor
            }, $true) | Select-Object -Last 1

        # If we are in the process of typing a command ...
        if ($commandAst -ne $null) {
            # Get its name
            $commandName = $commandAst.GetCommandName()
            if ($commandName -ne $null) {
                # Ensure it really is its name
                $command = $ExecutionContext.InvokeCommand.GetCommand($commandName, 'All')
                if ($command -is [System.Management.Automation.AliasInfo]) {
                    $commandName = $command.ResolvedCommandName
                }

                # Get Help
                if ($commandName -ne $null) {
                    # Call help based on preference
                    switch ($PSReadlineHelpPreference) {
                        "detail" { Start-Process powershell.exe -ArgumentList "-NoExit -Command Get-Help $commandName -Detailed" }
                        "details" { Start-Process powershell.exe -ArgumentList "-NoExit -Command Get-Help $commandName -Detailed" }
                        "example" { Start-Process powershell.exe -ArgumentList "-NoExit -Command Get-Help $commandName -Examples" }
                        "examples" { Start-Process powershell.exe -ArgumentList "-NoExit -Command Get-Help $commandName -Examples" }
                        "online" { Get-Help $commandName -Online }
                        default { Get-Help $commandName -Online }
                    }
                }
            }
        }
    }
}
$ShutDown = {
    $null = Get-History -Count (Get-OMPProfileSetting -Name 'PSReadlinePersistantHistoryCount') |
        Export-CSV "$(Get-OMPProfileSetting -Name 'PSReadlineHistoryPath').csv" -NoTypeInformation
    Set-OMPProfileSetting -Name 'PSReadlineHistoryLoaded' -Value $false
}
$Unload = {
    Remove-OMPModule -Name 'psreadline'
    Remove-OMPProfileSetting -Name 'PSReadlineHistoryLoaded'
    Remove-OMPProfileSetting -Name 'PSReadlineHistoryPath'
    Remove-OMPProfileSetting -Name 'PSReadlinePersistantHistoryCount'
}