GithubHelp home page GithubHelp logo

mdgrs-mei / powershellrun Goto Github PK

View Code? Open in Web Editor NEW
145.0 3.0 8.0 177 KB

App, Utility and Function Launcher for PowerShell.

License: MIT License

PowerShell 33.98% C# 66.02%
fuzzy-search launcher powershell terminal cli tui

powershellrun's Introduction

PowerShellRun

GitHub license PowerShell Gallery PowerShell Gallery

Pester Test

Hashnode

App, Utility and Function Launcher for PowerShell.

Demo

PowerShellRun is a PowerShell module that lets you fuzzy search applications, utilities and functions you define and launch them with ease. It is a customizable launcher app on the PowerShell terminal.

Installation

Install-Module -Name PowerShellRun -Scope CurrentUser

Requirements

  • Windows or macOS
  • PowerShell 7.2 or newer

Quick Start

Enable-PSRunEntry -Category All
Invoke-PSRun

This code enables entries of all categories and opens up this TUI:

Invoke-PSRun

Type characters to search entries and hit Enter to launch the selected item. There are some other actions that can be performed depending on the item. Hit Ctrl+k to open the Action Window and see what actions are available.

ActionWindow

You can assign a shortcut key to quickly launch PowerShellRun.

Set-PSRunPSReadLineKeyHandler -InvokePsRunChord 'Ctrl+j'

Entry Categories

There are some entry categories that you can selectively enable by passing an array of the category names to Enable-PSRunEntry.

Enable-PSRunEntry -Category Function, Favorite

ใƒป๐Ÿš€ Application

Installed applications are listed by the Application category. You can launch (or launch as admin on Windows) the application by pressing the action key.

ใƒป๐Ÿ”ง Executable

Executable files under the PATH are listed by Executable category. You can invoke them on the same console where PowerShellRun is running.

ใƒป๐Ÿ”Ž Utility

File Manager (PSRun)

File Manager (PSRun) navigates the folder hierarchy from the current directory using the PowerShellRun TUI.

FiileManager

On the file entries, there is an action described as "Edit with Default Editor". You can customize the script for this action like below:

Set-PSRunDefaultEditorScript -ScriptBlock {
    param ($path)
    & code $path
}

WinGet (PSRun)

WinGet (PSRun) helps you install, upgrade and uninstall applications using winget. You need winget and Microsoft.WinGet.Client module installed to use this utility entry.

image

ใƒป๐Ÿ“ Favorite

You can register folders or files that you frequently access. The available actions are the same as the ones in File Manager (PSRun).

Add-PSRunFavoriteFolder -Path 'D:/PowerShellRun'
Add-PSRunFavoriteFile -Path 'D:/PowerShellRun/README.md' -Icon '๐Ÿ“–' -Preview @"
-------------------------------
๐Ÿ’– This is a custom preview ๐Ÿ’–
-------------------------------
"@

Favarites

ใƒป๐Ÿ“ Function

The ability to call PowerShell functions is what makes PowerShellRun special. The functions defined between Start-PSRunFunctionRegistration and Stop-PSRunFunctionRegistration are registered as entries. The scope of the functions needs to be global so that PowerShellRun can call them.

Start-PSRunFunctionRegistration

#.SYNOPSIS
# git pull with rebase option.
function global:GitPullRebase() {
    git pull --rebase
}
# ... Define functions here as many as you want.

Stop-PSRunFunctionRegistration

FunctionBasic

SYNOPSIS or DESCRIPTION in the comment based help is used as a description of the entry. You can also optionally specify parameters using the COMPONENT. It uses ConvertFrom-StringData to extract the parameters.

<#
.SYNOPSIS
git pull with rebase option.

.COMPONENT
PSRun(
    Icon = ๐ŸŒฟ
    Preview = This is a custom preview.\nNew lines need to be written like this.)
#>
function global:GitPullRebase() {
    git pull --rebase
}

Function

It's even possible to open PowerShellRun's TUI inside a registered function entry using the commands described in the following section. To create a pseudo nested menu, we recommend that you use Restore-PSRunFunctionParentSelector command to restore the parent menu with Backspace key. File Manager (PSRun) is a good example of the nested menu.

function global:OpenNestedMenu() {
    $option = Get-PSRunDefaultSelectorOption
    $option.QuitWithBackspaceOnEmptyQuery = $true

    $result = Get-ChildItem | ForEach-Object {
        $entry = [PowerShellRun.SelectorEntry]::new()
        $entry.UserData = $_
        $entry.Name = $_.Name
        $entry
    } | Invoke-PSRunSelectorCustom -Option $option

    if ($result.KeyCombination -eq 'Backspace') {
        Restore-PSRunFunctionParentSelector
        return
    }
    # ... Other key handlings here
}

History Search

History search functionality is provided outside the PowerShellRun menu using PSReadLineKeyHandler. It searches PSReadLine history. Multi-line entries are also supported. You can enable it with the following command:

Set-PSRunPSReadLineKeyHandler -PSReadLineHistoryChord 'Ctrl+r'

image

Options

You can customize PowerShellRun's behavior and themes through options. Create a SelectorOption instance and pass it to Set-PSRunDefaultSelectorOption.

$option = [PowerShellRun.SelectorOption]::new()
$option.Prompt = 'Type words๐Ÿ‘‰ '
$option.QuitWithBackspaceOnEmptyQuery = $true
Set-PSRunDefaultSelectorOption $option
Invoke-PSRun

The option you set as default can be returned by Get-PSRunDefaultSelectorOption. Note that the returned option is always deep cloned. It is useful when you create a nested menu.

$option = Get-PSRunDefaultSelectorOption
$option.Prompt = 'Nested menu prompt > '
$option.QuitWithBackspaceOnEmptyQuery = $true
'a', 'b' | Invoke-PSRunSelector -Option $option

ใƒปKey Bindings

Key bindings are stored in $option.KeyBinding. You can set a string of KeyModifier and Key concatenated with + to the key.

$keyBinding = $option.KeyBinding
$keyBinding.QuitKeys = @(
    'Escape'
    'Ctrl+j'
)
$keyBinding.MarkerKeys = 'Ctrl+f'

ใƒปTheme

The theme can be customized with $option.Theme property. We hope someone creates a cool theme library for PowerShellRun๐Ÿ™.

$default = [PowerShellRun.FontColor]::FromHex('#CBCCC6')
$gray = [PowerShellRun.FontColor]::FromHex('#707070')
$highlight = [PowerShellRun.FontColor]::FromHex('#61FFCA')
$focusHighlight = [PowerShellRun.FontColor]::FromHex('#4CBF99')
$roundBorder = [PowerShellRun.BorderSymbol]::new()
$roundBorder.TopLeft = 'โ•ญ'
$roundBorder.TopRight = 'โ•ฎ'
$roundBorder.BottomLeft = 'โ•ฐ'
$roundBorder.BottomRight = 'โ•ฏ'

$option.Prompt = ' '
$theme = $option.Theme
$theme.CanvasHeightPercentage = 80
$theme.Cursor = 'โ–ธ '
$theme.IconEnable = $false
$theme.PreviewPosition = [PowerShellRun.PreviewPosition]::Right
$theme.CanvasBorderFlags = [PowerShellRun.BorderFlag]::All
$theme.SearchBarBorderFlags = [PowerShellRun.BorderFlag]::None
$theme.CanvasBorderSymbol = $roundBorder
$theme.PreviewBorderSymbol = $roundBorder
$theme.DefaultForegroundColor = $default
$theme.CanvasBorderForegroundColor = $gray
$theme.PromptForegroundColor = $gray
$theme.PreviewBorderForegroundColor = $gray
$theme.EntryScrollBarForegroundColor = $gray
$theme.PreviewScrollBarForegroundColor = $gray
$theme.CursorForegroundColor = $highlight
$theme.NameHighlightForegroundColor = $highlight
$theme.DescriptionHighlightForegroundColor = $highlight
$theme.NameFocusHighlightBackgroundColor = $focusHighlight
$theme.DescriptionFocusHighlightBackgroundColor = $focusHighlight

Theme


PowerShellRun as a Generic Selector

The underlying fuzzy selector in PowerShellRun is accessible with the following commands.

Invoke-PSRunSelector

Invoke-PSRunSelector is designed to be used interactively on the terminal. It takes an array of objects and returns objects. It uses Name, Description and Preview properties of the object by default but you can change them with parameters like -NameProperty, -DescriptionProperty and -PreviewProperty.

Get-ChildItem | Invoke-PSRunSelector -DescriptionProperty FullName -MultiSelection

SelectorDemo

-Expression parameter is useful if you need to build custom strings.

Get-ChildItem | Invoke-PSRunSelector -Expression {@{
    Name = $_.Name
    Preview = Get-Item $_ | Out-String
}}

Invoke-PSRunSelectorCustom

Invoke-PSRunSelectorCustom offers you a full access to the selector and is designed to create your own tool. It takes an array of SelectorEntry instances and returns a SelectorResult object. A SelectorResult object holds information such as the selected entry and the pressed key.

PS> Get-ChildItem | ForEach-Object {
    $entry = [PowerShellRun.SelectorEntry]::new()
    $entry.UserData = $_
    $entry.Name = $_.Name
    $entry.Preview = $_.FullName
    $entry
} | Invoke-PSRunSelectorCustom

FocusedEntry                MarkedEntries KeyCombination Context
------------                ------------- -------------- -------
PowerShellRun.SelectorEntry               Enter          PowerShellRun.SelectorContext

By using PreviewAsyncScript, it's even possible to show information that takes some time to generate without blocking the UI. If you have bat installed for syntax highlighting, you can build a Select-String viewer with this script:

$word = Read-Host 'Type word to search for'
$filter = Read-Host 'Type path filter (e.g. "*.cs")'

$option = [PowerShellRun.SelectorOption]::new()
$option.Prompt = "Searching for word '{0}'> " -f $word

$matchLines = Get-ChildItem $filter -Recurse | Select-String $word
$result = $matchLines | ForEach-Object {
    $entry = [PowerShellRun.SelectorEntry]::new()
    $entry.UserData = $_
    $entry.Name = '{0}:{1}' -f $_.Filename, $_.LineNumber
    $entry.Description = $_.Path
    $entry.PreviewAsyncScript = {
        param($match)
        & bat --color=always --highlight-line $match.LineNumber $match.Path
    }
    $entry.PreviewAsyncScriptArgumentList = $_
    $entry.PreviewInitialVerticalScroll = $_.LineNumber
    $entry
} | Invoke-PSRunSelectorCustom -Option $option

$match = $result.FocusedEntry.UserData
if ($match -and ($result.KeyCombination -eq 'Enter')) {
    $argument = '{0}:{1}' -f $match.Path, $match.LineNumber
    code --goto $argument
}

Invoke-PSRunSelectorCustom

Major Limitations

  • No history support
  • Some emojis break the rendering

Changelog

Changelog is available here.


Contributing

Code of Conduct

Please read our Code of Conduct to foster a welcoming environment. By participating in this project, you are expected to uphold this code.

Have a question or want to showcase something?

Please come to our Discussions page and avoid filing an issue to ask a question.

Want to file an issue or make a PR?

Please see our Contribution Guidelines.


Credits

PowerShellRun uses:

Heavily inspired by:

powershellrun's People

Contributors

andrewpla avatar mdgrs-mei avatar seeminglyscience avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

powershellrun's Issues

[Feature Request]: ScriptBlock and Script file entries

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

With Function entries, you have to add functions to the global scope. If we don't want to use the global scope, registering ScriptBlocks directly should be useful.

If you manage many ps1 files instead of making functions in a file, adding those script files as callable entries should be handy too.

[Feature Request]: Named parameters in PreviewAsyncScript

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

Currently the argument list is passed as single element even if it's an array. It should support slitting parameters to named variables defined in param block.

[Bug]: Escape sequence characters are not ignored from search

OS

  • Windows
  • macOS
  • Linux

PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0โ€ฆ}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

What happened?

The characters in escape sequence should not hit with search. My bad...

image

[Feature Request]: PowerShellRun version of Read-Host

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

Like in the Select-String viewer example #25, there are cases where you want to get an input from the user. To make the interface consistent, we need a way to use the prompt without entries or Action Window instead of using Read-Host.

[Feature Request]: Function to get the current default SelectorOption

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

When creating a nested selector menu in the Function entry that modifies the prompt or any other options, you need to get the current default option.

Instead of passing it through global variables, which prevents the functions from being written in a generic way, it should be better to add a function to get the default option.

[Bug]: Setting nvim as a default editor doesn't work

OS

  • Windows
  • macOS
  • Linux

PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0โ€ฆ}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

What happened?

If you open a file in the File Manager or a Favorite file entry with this editor script, the terminal text editor cannot show it's output.

Set-PSRunDefaultEditorScript -ScriptBlock {
    param($path)
    nvim $path
}

You cannot use class methods and you need to use & operator to invoke the editor script same as GlobalStore.invokeFile.

[Bug]: Query error occurs when invoking PowerShellRun with a hot key

OS

  • Windows
  • macOS
  • Linux

PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0โ€ฆ}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

What happened?

  1. Set a hot key with Set-PSRunPSReadLineKeyHandler
  2. Type something in prompt
  3. Launch PowerShellRun with the hot key

Then the following error occurs:

Invoke-PSRun: A parameter cannot be found that matches parameter name 'Query'.

[Feature Request]: Add winget utility

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

Adding winget operation as an utility entry would be useful. It should include Install, Upgrade and Uninstall features as tried in
#12 #24.

[Documentation]: Invoke-PSRunPrompt

What needs to be improved?

We would like to see documentation for Invoke-PSRunPrompt function in the PowerShellRun as a generic selector section of the README.

[Feature Request]: Use & operator to run favorite executables

OS

  • Windows
  • macOS
  • Linux
  • Not specific to an OS

New feature description

Currently favorite files are invoked with Invoke-Item command which leads to opening executable files in another window. It should be better to run them on the same terminal as the one where PowerShellRun is running. It's also better for the consistency with the Executable category entries.

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.