microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.02k stars 8.23k forks source link

Enhance shell autocompletion with a cool new user interface and shell completion protocol #3121

Open anki-code opened 4 years ago

anki-code commented 4 years ago

Hello! Thank you for the new interesting project!

I just want to let you know about 🚀 Upterm — really great proof of concept but it stopped because maintainer was gone. This terminal looks like 21st century terminal. Very sad that it isn't supported.

image

up30485947-aeaa5398-9a37-11e7-927c-769304744844

up29716319-95f9a8d8-89b3-11e7-8515-fcb236eb4454

image

image

cereschen commented 9 months ago

I have implemented the most basic fig like automatic completion, git.json is generated by the npm package @ withfig/autocomplete/build/git

import * as fig from '@withfig/autocomplete/build/git'
import { writeFileSync } from 'fs'
writeFileSync('./git.json',JSON.stringify(fig.default))

Note that I changed the shortcut key to ctrl+b

$directory = (Split-Path -Parent $PROFILE)
$jsonFilePath = Join-Path -Path $directory -ChildPath 'Completions/git.json'
$jsonContent = Get-Content -Raw -Path $jsonFilePath
$jsonObject = $jsonContent | ConvertFrom-Json
function Get-Items {
  param (
    $obj
  )
  $array = @()
  $mergedArray = $obj.subcommands + $obj.options
  $mergedArray | ForEach-Object {
    $name = $_.name
    $CompletionText = $_.name
    if ($_.name.GetType().BaseType.Name -eq "Array") {
      $name = $name -join "  "
      $CompletionText = $_.name[0]
    }
    $description = $_.description
    $newObject = New-Object PSObject -Property @{
      CompletionText = $CompletionText
      ListItemText   = $name + "   " + $description
      ResultType     = 2
      ToolTip        = $description
    }
    $array += $newObject
  }
  return $array
}
function Send-Completions {
  $commandLine = ""
  $cursorIndex = 0
  [Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$commandLine, [ref]$cursorIndex)
  $completionPrefix = $commandLine
  $result2 = "`e]633;Completions"
  # $result = "`e]633;Completions"
  if ($completionPrefix.Length -gt 0) {
    $completions = TabExpansion2 -inputScript $completionPrefix -cursorColumn $cursorIndex
    if ($null -ne $completions.CompletionMatches) {
      # $result += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);"
      # $result += $completions.CompletionMatches | ConvertTo-Json -Compress
      $result2 += ";$($completions.ReplacementIndex);$($completions.ReplacementLength);$($cursorIndex);"
      $subStr = $completionPrefix.Substring(0, $cursorIndex)
      $strArray = $subStr -split '\s+'
      $pushItem = $jsonObject.subcommands | Where-Object { $_.name -eq $strArray[1] }
      if ($null -ne $pushItem) {
        $res = Get-Items -obj $pushItem
        $result2 += $res | ConvertTo-Json -Compress
      }
      else {
        $res = Get-Items -obj $jsonObject
        $result2 += $res | ConvertTo-Json -Compress
      }
    }
  }
  # $result += "`a"
  $result2 += "`a"
  Write-Host -NoNewLine $result2
}

function Set-MappedKeyHandlers {
  # Terminal suggest - always on keybindings
  Set-PSReadLineKeyHandler -Chord 'Ctrl+b' -ScriptBlock {
    Send-Completions
  }
}

# Register key handlers if PSReadLine is available
if (Get-Module -Name PSReadLine) {
  Set-MappedKeyHandlers
}
else {
  Write-Host "PsReadline was disabled. Shell Completion was not enabled."
}
rsteube commented 9 months ago

@cereschen have a look at microsoft/inshellisense. With https://github.com/microsoft/inshellisense/pull/78 you can invoke the fig completions with inshellisense complete 'command args...'. I've got a bridge for it at carapace-bridge as well which will be include in the next release of carapace-bin.

Things to note though:

Inshellisense uses a lexer to split the command which which is very basic. E.g. at the moment it can't handle spaces, redirects, quotes, and so on. The fig completions got quite a lot of issues as well. So a lot of things don't work yet (and won't for quite a long time).

cereschen commented 9 months ago

@cereschen看看microsoft/inshellisense。 使用microsoft/inshellisense#78,您可以使用 调用无花果补全inshellisense complete 'command args...'我在carapace-bridge 上也有一个桥,它将包含在下一版本的carapace-bin中。

但需要注意的事项:

Inshellisense 使用词法分析器来拆分命令,这是非常基本的。 例如,目前它无法处理空格、重定向、引号等。 无花果的完成也遇到了很多问题。 所以很多事情还不起作用(而且在很长一段时间内都不起作用)。

I successfully used is today, but it took me too much time to call is using Invoke Expression. Perhaps there is a better way, as I am not proficient in Powershell

This seems to be an issue with IS, as it will take some time for it to end..

Welding-Torch commented 6 months ago

I just started using Warp for WSL and it feels like it's lightyears ahead of this stuff. I think it would be great if Windows Terminal took some notes from it, especially on this feature request that's been open since 2019.

Also mentioned earlier in this thread by: @just1a-person in 2021 and @orcmid.