Code Monkey home page Code Monkey logo

Comments (3)

vors avatar vors commented on August 23, 2024 2

Hey, thank you for the kind words, glad that you find it useful!

One easy workaround about how to call private functions from any PowerShell module is the following syntax & $m {...} - invoke scriptblock in the context of the module $m. For example

$m = Get-Module ZLocation
$query = 'se'                                                          
& $m {Find-Matches (Get-ZLocation) $query | & $m Get-EscapedPath } 

I hope that would help you build the native completers. There are native completers examples available here https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/register-argumentcompleter?view=powershell-6

from zlocation.

devkabiir avatar devkabiir commented on August 23, 2024

Hi, I tried this today, thanks for the tip.

Here is a working example for anyone who is looking for it

if (Get-Command -Name Register-ArgumentCompleter -ErrorAction Ignore) {
 
    Register-ArgumentCompleter -CommandName 'Invoke-ExplorerZ' -ParameterName 'match' -ScriptBlock {
        param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
        # Omit first item (command name) and empty strings
        $i = $commandAst.CommandElements.Count
        [string[]]$query = if ($i -gt 1) {
            $commandAst.CommandElements[1..($i - 1)] | ForEach-Object { $_.toString() }
        }
        $m = Get-Module ZLocation
        & $m { param($query) Find-Matches (Get-ZLocation) $query | Get-EscapedPath } -query $query
    }
}



function Invoke-ExplorerZ {
    param(
        [Parameter(ValueFromRemainingArguments, ValueFromPipeline = $true, Position = 0)][Object[]]$match
    )

    if ($null -eq $match) {
        explorer.exe $pwd
        return
    }

    $match | ForEach-Object { explorer.exe $_ }
}

Set-Alias -Name e -Value Invoke-ExplorerZ

You can now open directories in explore with tab completions

e <tab>
e C:\my-directory

Feel free to close the issue.

I haven't been able to pass the $query properly though. Did I do something wrong there?

Edit: Fixed it, solution was to pass the $query argument by name

& $m { param($query) Find-Matches (Get-ZLocation) $query | Get-EscapedPath } -query $query

from zlocation.

vors avatar vors commented on August 23, 2024

Awesome!

from zlocation.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.