bun icon indicating copy to clipboard operation
bun copied to clipboard

Improve ZSH completions

Open srealmoreno opened this issue 3 years ago • 6 comments

Refactoring the code and adding improvements

Autocomplete for --define flag

  • Reads the environment variables and autocomplete them in json format
  • Autocomplete the environment variable process.env.NODE_ENV

define completion

Autocomplete for --load flag

Auto complete file extensions with valid loaders

load completion

Fixed bug when autocompleting recent packages

Previously, when adding multiple packages in the same command, the autocomplete did not work well.

bun add react vite esbuild nanoid

Before: recent packages completion before

After: Segmentation in

  • recent
  • popular

recent packages completion after

Autocomplete for remove command

Segmentation in

  • Dependencies
  • Dev Dependencies
  • Optional Dependencies
  • Peer Dependencies

remove package completion

Segmentation for the run command

Segmentation in

  • files
  • binaries
  • scripts

run completion

srealmoreno avatar Apr 19 '22 06:04 srealmoreno

Overall, this is great. The only part I'm not sure about is cross-platform compatibility with grep and sed

I ran into a similar issue with bun's Makefile, ended up doing this:

SED = $(shell which gsed || which sed)

homebrew aliases GNU versions of coreutils with g, so ggrep and gsed – but not everyone bothers to install the coreutils (or grep) homebrew package

Maybe we could assume the commands will work but re-run without the sed or grep or jq specific features if it returns a non-zero exit code? That way the happy path still works

Jarred-Sumner avatar Apr 19 '22 07:04 Jarred-Sumner

Updating RegExp to be compatible with MacOS and BSD and checking if the jq command is installed

  • RegExp

Since grep and sed in Macos don't support Perl RegExp I updated to BRE RegExp.

Before:

_envs=(
  ${(f)"$(export | sed -r "/^\w+=(true|false|null|[0-9]+)$/! s/='?(.*?)('|$)/=\"\1\"/")"}
)

_recent=($(history -n | grep -oP '(?<=^bun add ).+'))

After:

_envs=(
  ${(f)"$(export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/")"}
)

_recent=($(history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'))
  • jq

Since jq is not an essential tool for distributions there is a chance that it is not installed, so I added a check to determine if it is installed.

if ! command -v jq >/dev/null 2>&1; then
  _message 'jq is required to complete bun remove command'
  return
fi

srealmoreno avatar Apr 20 '22 02:04 srealmoreno

Unfortunately, sed has a similar issue on macOS as grep

bun add completions don't work after the changes: image

bun remove works though! image

Jarred-Sumner avatar Apr 21 '22 22:04 Jarred-Sumner

Would you mind providing more information about the error? Unfortunately I don't have a MacOs to perform the necessary tests.

Please run this and show me the output:

# Test of the `add` command:
history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'
# Test of the `--define` option:
export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/"

srealmoreno avatar Apr 22 '22 01:04 srealmoreno

@srealmoreno if this is still relevant: on Mac, it would work like this

history -n | sed -n 's/^\(bun[[:space:]]\{1,\}add[[:space:]]\{1,\}\)\(.\{1,\}\)$/\2/p'

or with extended regex:

history -n | sed -nE 's/^(bun[[:space:]]+add[[:space:]]+)(.+)$/\2/p'

Similarly for export (slightly corrected version):

export | sed -E "/^[[:alnum:]_]+=(true|false|null|[0-9])$/! s/='(.*)'$|=(.*)$/=\"\1\2\"/"

(branches don't work in BRE if I'm not mistaken)

alexkuz avatar May 15 '22 04:05 alexkuz

@alexkuz Thanks, I'll add it right now.

srealmoreno avatar Jun 08 '22 00:06 srealmoreno