Skip to content

Commit

Permalink
improved powershell support, including hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed May 16, 2023
1 parent 11ae04c commit a58bcc3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
if: matrix.os == 'windows-latest'
shell: pwsh
run: |
Compress-Archive -Path .\target\${{ matrix.target }}\release\intelli-shell.exe -DestinationPath intelli-shell-${{ matrix.target }}.zip
Compress-Archive -Path ".\target\${{ matrix.target }}\release\intelli-shell.exe", ".\intelli-shell.ps1" -DestinationPath intelli-shell-${{ matrix.target }}.zip
- name: Release
if: matrix.os == 'windows-latest'
Expand Down
70 changes: 40 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ It currently works on Bash, Zsh and Fish and should be compatible with most Linu

- Standalone binaries
- Autocomplete currently typed command
- Full Text Search in both command and description with hashtag support
- Full Text Search in both command and description with hashtag support on descriptions
- Find & replace labels of currently typed command
- Non-intrusive (inline) and full-screen interfaces
- Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!)
Expand All @@ -48,18 +48,11 @@ After installing it using bash, it should work in any supported shell.
### PowerShell (Windows)

```powershell
New-Item -Path $env:APPDATA\IntelliShell\Intelli-Shell\bin -Type Directory
Invoke-WebRequest -UseBasicParsing -URI "https://github.com/lasantosr/intelli-shell/releases/latest/download/intelli-shell-x86_64-pc-windows-msvc.zip" -OutFile .\intelli-shell.zip
Expand-Archive -Path intelli-shell.zip -DestinationPath $env:APPDATA\IntelliShell\Intelli-Shell\bin
Remove-Item intelli-shell.zip
$Path = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User)
if ($Path -NotLike "*IntelliShell*") {
$Path = $Path + [IO.Path]::PathSeparator + "$env:APPDATA\IntelliShell\Intelli-Shell\bin"
[Environment]::SetEnvironmentVariable("Path", $Path, [EnvironmentVariableTarget]::User)
}
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
irm https://raw.githubusercontent.com/lasantosr/intelli-shell/main/install.ps1 | iex
```

After installing it with powershell, it should also work on cmd.
After installing it with powershell, it should also work on cmd (without hotkeys).

### Source

Expand All @@ -69,37 +62,61 @@ To install from source you'll need to have Rust installed, which is recommended
cargo install intelli-shell --locked
```

To enable hotkeys, additional steps are required:

<details>
<summary>Linux considerations</summary>
<summary>Linux</summary>

To include hotkey integrations with current line, you'll need to download the source script also:
Download source script:

- Bash / Zsh:

```sh
mkdir -p ~/.local/share/intelli-shell
curl -sSf https://raw.githubusercontent.com/lasantosr/intelli-shell/main/intelli-shell.sh > ~/.local/share/intelli-shell/intelli-shell.sh
mkdir -p ~/.local/share/intelli-shell/bin
curl -sSf https://raw.githubusercontent.com/lasantosr/intelli-shell/main/intelli-shell.sh > ~/.local/share/intelli-shell/bin/intelli-shell.sh
```

Or, if using fish:
- Fish:

```sh
mkdir -p ~/.local/share/intelli-shell
curl -sSf https://raw.githubusercontent.com/lasantosr/intelli-shell/main/intelli-shell.fish > ~/.local/share/intelli-shell/intelli-shell.fish
mkdir -p ~/.local/share/intelli-shell/bin
curl -sSf https://raw.githubusercontent.com/lasantosr/intelli-shell/main/intelli-shell.fish > ~/.local/share/intelli-shell/bin/intelli-shell.fish
```

After that, you should edit your `~/.bashrc`, `~/.zshrc` or `~/.bash_profile` to source it:
Edit your profile to source it:

- Bash / Zsh: `~/.bashrc`, `~/.zshrc` or `~/.bash_profile`

```sh
source ~/.local/share/intelli-shell/intelli-shell.sh
source ~/.local/share/intelli-shell/bin/intelli-shell.sh
```

Or, if using fish you should edit `~/.config/fish/config.fish`:
- Fish: `~/.config/fish/config.fish`:

```sh
source ~/.local/share/intelli-shell/intelli-shell.fish
source ~/.local/share/intelli-shell/bin/intelli-shell.fish
```

</details>

<details>
<summary>Windows</summary>

Download the source script also:

```powershell
New-Item -Path $env:APPDATA\IntelliShell\Intelli-Shell\bin -Type Directory
Invoke-WebRequest -UseBasicParsing -URI "https://raw.githubusercontent.com/lasantosr/intelli-shell/main/intelli-shell.ps1" -OutFile $env:APPDATA\IntelliShell\Intelli-Shell\bin\intelli-shell.ps1
```

Edit your `$Profile` to execute it:

```powershell
. $env:APPDATA\IntelliShell\Intelli-Shell\bin\intelli-shell.ps1
```

</details>

## Usage

You can view supported actions by running `intelli-shell -h`. Most used standalone commands are:
Expand All @@ -109,23 +126,16 @@ You can view supported actions by running `intelli-shell -h`. Most used standalo
- `intelli-shell export` to export user-bookmarked commands (won't export _tldr's_ commands)
- `intelli-shell import user_commands.txt` to import commands into the user category

Windows users, as hotkeys are not enabled, will also need those:

- `intelli-shell search` to search for stored commands
- `intelli-shell save "my command"` to save a new command

### Hotkeys

Hotkeys are only available on Linux:

- `ctrl + b` bookmark currently typed command
- `ctrl + space` show suggestions for current line
- `ctrl + l` replace labels of currently typed command
- `esc` clean current line, this binding can be skipped if `INTELLI_SKIP_ESC_BIND=1`

**Note:** When navigating items, selected suggestion can be deleted with `ctrl + d`

You can customize key bindings using environment variables: `INTELLI_SAVE_HOTKEY`, `INTELLI_SEARCH_HOTKEY` and `INTELLI_LABEL_HOTKEY`
You can customize key bindings using environment variables: `INTELLI_BOOKMARK_HOTKEY`, `INTELLI_SEARCH_HOTKEY` and `INTELLI_LABEL_HOTKEY`

## Wishlist

Expand Down
24 changes: 24 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$null = New-Item -Force -Path $env:APPDATA\IntelliShell\Intelli-Shell\bin -Type Directory
Invoke-WebRequest -UseBasicParsing -URI "https://github.com/lasantosr/intelli-shell/releases/latest/download/intelli-shell-x86_64-pc-windows-msvc.zip" -OutFile .\intelli-shell.zip
Expand-Archive -Force -Path intelli-shell.zip -DestinationPath $env:APPDATA\IntelliShell\Intelli-Shell\bin
Remove-Item intelli-shell.zip
$Path = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User)
if ($Path -NotLike "*IntelliShell*") {
$Path = $Path + [IO.Path]::PathSeparator + "$env:APPDATA\IntelliShell\Intelli-Shell\bin"
[Environment]::SetEnvironmentVariable("Path", $Path, [EnvironmentVariableTarget]::User)
}
$ProfileContent = $null
if (Test-Path -Path $Profile -PathType Leaf) {
$ProfileContent = Get-Content -Raw $Profile
} else {
$Parent = Split-Path -parent $Profile
$null = New-Item -Force -ItemType Directory -Path $Parent
$null = New-Item -ItemType File -Path $Profile
}
if (($null -eq $ProfileContent) -Or ($ProfileContent -NotLike "*IntelliShell*")) {
Add-Content $Profile "`n# IntelliShell"
Add-Content $Profile "# `$env:INTELLI_SEARCH_HOTKEY = 'Ctrl+Spacebar'"
Add-Content $Profile "# `$env:INTELLI_BOOKMARK_HOTKEY = 'Ctrl+b'"
Add-Content $Profile "# `$env:INTELLI_LABEL_HOTKEY = 'Ctrl+l'"
Add-Content $Profile ". `$env:APPDATA\IntelliShell\Intelli-Shell\bin\intelli-shell.ps1"
}
6 changes: 3 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ case "$OSTYPE" in
INTELLI_HOME="${INTELLI_HOME:-$HOME/.local/share/intelli-shell}"
;;
darwin*) os="apple-darwin"
INTELLI_HOME="${INTELLI_HOME:-$HOME/Library/Application Support/org.IntelliShell.Intelli-Shell}"
INTELLI_HOME="${INTELLI_HOME:-$HOME/Library/Application\ Support/org.IntelliShell.Intelli-Shell}"
;;
msys*) os="pc-windows-msvc"
POSIX_APPDATA=$(echo "/$APPDATA" | sed 's/\\/\//g' | sed 's/://')
Expand Down Expand Up @@ -42,7 +42,7 @@ function update_rc () {
echo "INTELLI_HOME=$INTELLI_HOME" >> "$1"
echo '# export INTELLI_SEARCH_HOTKEY=\\C-@' >> "$1"
echo '# export INTELLI_LABEL_HOTKEY=\\C-l' >> "$1"
echo '# export INTELLI_SAVE_HOTKEY=\\C-b' >> "$1"
echo '# export INTELLI_BOOKMARK_HOTKEY=\\C-b' >> "$1"
echo '# export INTELLI_SKIP_ESC_BIND=0' >> "$1"
echo 'alias intelli-shell="$INTELLI_HOME/bin/intelli-shell"' >> "$1"
echo 'source $INTELLI_HOME/bin/intelli-shell.sh' >> "$1"
Expand Down Expand Up @@ -71,7 +71,7 @@ if [[ -f "/usr/bin/fish" ]]; then
echo "set INTELLI_HOME $INTELLI_HOME" >> "$config"
echo '# set INTELLI_SEARCH_HOTKEY \cr' >> "$config"
echo '# set INTELLI_LABEL_HOTKEY \cl' >> "$config"
echo '# set INTELLI_SAVE_HOTKEY \cb' >> "$config"
echo '# set INTELLI_BOOKMARK_HOTKEY \cb' >> "$config"
echo '# set INTELLI_SKIP_ESC_BIND 0' >> "$config"
echo 'source $INTELLI_HOME/bin/intelli-shell.fish' >> "$config"
fi
Expand Down
63 changes: 63 additions & 0 deletions intelli-shell.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
$IntelliSearchChord = if ($null -eq $env:INTELLI_SEARCH_HOTKEY) { 'Ctrl+Spacebar' } else { $env:INTELLI_SEARCH_HOTKEY }
$IntelliBookmarkChord = if ($null -eq $env:INTELLI_BOOKMARK_HOTKEY) { 'Ctrl+b' } else { $env:INTELLI_BOOKMARK_HOTKEY }
$IntelliLabelChord = if ($null -eq $env:INTELLI_LABEL_HOTKEY) { 'Ctrl+l' } else { $env:INTELLI_LABEL_HOTKEY }

Set-PSReadLineKeyHandler -Chord $IntelliSearchChord -BriefDescription "IntelliShell Search" -Description "Searches for a bookmarked command" -ScriptBlock {
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

$TempFile = New-TemporaryFile
$line = $line -replace '"','""""""""""""'
$Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" search """"' + $line + '""""'
Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command ""$Command""" -RedirectStandardError "NUL"
$IntelliOutput = Get-Content -Raw $TempFile
Remove-Item $TempFile

[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine()
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
if (-Not [string]::IsNullOrWhiteSpace($IntelliOutput)) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($IntelliOutput)
}
}

Set-PSReadLineKeyHandler -Chord $IntelliBookmarkChord -BriefDescription "IntelliShell Bookmark" -Description "Bookmarks current command" -ScriptBlock {
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

$TempFile = New-TemporaryFile
$line = $line -replace '"','""""""""""""'
$Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" save """"' + $line + '""""'
Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command ""$Command""" -RedirectStandardError "NUL"
$IntelliOutput = Get-Content -Raw $TempFile
Remove-Item $TempFile

[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine()
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
if (-Not [string]::IsNullOrWhiteSpace($IntelliOutput)) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($IntelliOutput)
}
}

Set-PSReadLineKeyHandler -Chord $IntelliLabelChord -BriefDescription "IntelliShell Label" -Description "Triggers label replace for current command" -ScriptBlock {
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

$TempFile = New-TemporaryFile
$line = $line -replace '"','""""""""""""'
$Command = 'intelli-shell.exe --file-output=""""' + $TempFile.FullName + '"""" label """"' + $line + '""""'
Start-Process powershell.exe -Wait -NoNewWindow -ArgumentList "-command ""$Command""" -RedirectStandardError "NUL"
$IntelliOutput = Get-Content -Raw $TempFile
Remove-Item $TempFile

[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
[Microsoft.PowerShell.PSConsoleReadLine]::BeginningOfLine()
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
if (-Not [string]::IsNullOrWhiteSpace($IntelliOutput)) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($IntelliOutput)
}
}
6 changes: 3 additions & 3 deletions intelli-shell.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Default bindings
intelli_search_key="${INTELLI_SEARCH_HOTKEY:-\C-@}"
intelli_save_key="${INTELLI_SAVE_HOTKEY:-\C-b}"
intelli_bookmark_key="${INTELLI_BOOKMARK_HOTKEY:-\C-b}"
intelli_label_key="${INTELLI_LABEL_HOTKEY:-\C-l}"

if [[ -n "$ZSH_VERSION" ]]; then
Expand Down Expand Up @@ -46,7 +46,7 @@ if [[ -n "$ZSH_VERSION" ]]; then
zle -N _intelli_save
zle -N _intelli_label
bindkey "$intelli_search_key" _intelli_search
bindkey "$intelli_save_key" _intelli_save
bindkey "$intelli_bookmark_key" _intelli_save
bindkey "$intelli_label_key" _intelli_label

elif [[ -n "$BASH" ]]; then
Expand Down Expand Up @@ -87,6 +87,6 @@ elif [[ -n "$BASH" ]]; then

if [[ "${INTELLI_SKIP_ESC_BIND:-0}" == "0" ]]; then bind '"\e": kill-whole-line'; fi
bind -x '"'"$intelli_search_key"'":_intelli_search'
bind -x '"'"$intelli_save_key"'":_intelli_save'
bind -x '"'"$intelli_bookmark_key"'":_intelli_save'
bind -x '"'"$intelli_label_key"'":_intelli_label'
fi
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//!
//! - Standalone binaries
//! - Autocomplete currently typed command
//! - Full Text Search in both command and description with hashtag support
//! - Full Text Search in both command and description with hashtag support on descriptions
//! - Find & replace labels of currently typed command
//! - Non-intrusive (inline) and full-screen interfaces
//! - Fetch command to parse and store [tldr](https://github.com/tldr-pages/tldr) pages (Thanks to them!)
Expand Down

0 comments on commit a58bcc3

Please sign in to comment.