Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom completers for complex user aliases #973

Closed
wants to merge 3 commits into from

Conversation

baflo
Copy link

@baflo baflo commented Sep 4, 2024

Creating custom completers for complex aliases

posh-git will search the $env:PSModulePath for modules whose name match posh-git-extras-* and use any
function in the found modules that has the name GitTabCustomExpansion and expects a single parameter of type
[string].

These functions receive the command as written on the console, stripped by the git binary command
and should return/write custom suggestions.

Here's how you can do it:

# Configure an alias:
> git config --global alias.wttr '!f() { curl https://wttr.in/$1; }; f'

# Find your PS-Module-Paths:
> $env:PSModulePath
C:\Users\me\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

You probably want to select a folder in your user space. In the Modules folder, create another folder,
e.g. posh-git-extras-aliases and in that folder a file named posh-git-extras-aliases.psm1.

In this module file, you must declare and export the function GitTabCustomExpansion:

# C:\Users\me\Documents\WindowsPowerShell\Modules\posh-git-extras-aliases\posh-git-extras-aliases.psm1

function GitTabCustomExpansion()
{
    param(
        [string]$gitCommandBlock
    )

    switch -regex ($gitCommandBlock) {
        "^wttr\s+(?<loc>[\S]*)$" {
            @('London', 'Berlin', 'Amsterdam') |
            Where-Object { $_ -like "$($Matches['loc'])*" }
        }
    }
}

Export-ModuleMember -Function GitTabCustomExpansion

@baflo baflo changed the title Enable custom completers Enable custom completers for complex user aliases Sep 4, 2024
@baflo
Copy link
Author

baflo commented Sep 9, 2024

nvm, found out you can easily register a second ArgumentCompleter which makes this obsolete.

@baflo baflo closed this Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant