mirror of
https://github.com/zsh-users/zsh-autosuggestions.git
synced 2025-12-06 23:30:39 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
31452887d2 | ||
|
|
cf146b6696 | ||
|
|
74197498fc | ||
|
|
abe577d519 | ||
|
|
0ae5907294 | ||
|
|
03bd381112 | ||
|
|
00bd0e9125 | ||
|
|
48d2dc1091 |
1
DESCRIPTION
Normal file
1
DESCRIPTION
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||||
3
INFO
3
INFO
@@ -1,3 +0,0 @@
|
|||||||
Fish-like fast/unobtrusive autosuggestions for zsh.
|
|
||||||
https://github.com/tarruda/zsh-autosuggestions
|
|
||||||
v0.1.1
|
|
||||||
36
Makefile
36
Makefile
@@ -1,8 +1,7 @@
|
|||||||
DIST_DIR := ./dist
|
|
||||||
SRC_DIR := ./src
|
SRC_DIR := ./src
|
||||||
SCRIPT_DIR := ./script
|
SCRIPT_DIR := ./script
|
||||||
|
|
||||||
SRC_TARGETS := \
|
SRC_FILES := \
|
||||||
$(SRC_DIR)/config.zsh \
|
$(SRC_DIR)/config.zsh \
|
||||||
$(SRC_DIR)/deprecated.zsh \
|
$(SRC_DIR)/deprecated.zsh \
|
||||||
$(SRC_DIR)/bind.zsh \
|
$(SRC_DIR)/bind.zsh \
|
||||||
@@ -11,17 +10,32 @@ SRC_TARGETS := \
|
|||||||
$(SRC_DIR)/suggestion.zsh \
|
$(SRC_DIR)/suggestion.zsh \
|
||||||
$(SRC_DIR)/start.zsh
|
$(SRC_DIR)/start.zsh
|
||||||
|
|
||||||
$(DIST_DIR)/autosuggestions.zsh: $(SRC_TARGETS) LICENSE
|
HEADER_FILES := \
|
||||||
mkdir -p $(DIST_DIR)
|
DESCRIPTION \
|
||||||
cat INFO | sed -e 's/^/# /g' > $@
|
URL \
|
||||||
echo "#" >> $@
|
VERSION \
|
||||||
cat LICENSE | sed -e 's/^/# /g' >> $@
|
LICENSE
|
||||||
cat >> $@ $(SRC_TARGETS)
|
|
||||||
|
PLUGIN_TARGET := zsh-autosuggestions.zsh
|
||||||
|
OH_MY_ZSH_LINK_TARGET := zsh-autosuggestions.plugin.zsh
|
||||||
|
|
||||||
|
ALL_TARGETS := \
|
||||||
|
$(PLUGIN_TARGET) \
|
||||||
|
$(OH_MY_ZSH_LINK_TARGET)
|
||||||
|
|
||||||
|
all: $(ALL_TARGETS)
|
||||||
|
|
||||||
|
$(PLUGIN_TARGET): $(HEADER_FILES) $(SRC_FILES)
|
||||||
|
cat $(HEADER_FILES) | sed -e 's/^/# /g' > $@
|
||||||
|
cat $(SRC_FILES) >> $@
|
||||||
|
|
||||||
|
$(OH_MY_ZSH_LINK_TARGET): $(PLUGIN_TARGET)
|
||||||
|
ln -s $(PLUGIN_TARGET) $@
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(DIST_DIR)
|
rm $(ALL_TARGETS)
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: $(DIST_DIR)/autosuggestions.zsh $(SCRIPT_DIR)/test.sh
|
test: all
|
||||||
$(SCRIPT_DIR)/test.sh
|
$(SCRIPT_DIR)/test.zsh
|
||||||
|
|||||||
56
README.md
56
README.md
@@ -7,6 +7,8 @@ It suggests commands as you type, based on command history.
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
### Manual
|
||||||
|
|
||||||
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.
|
1. Clone this repository somewhere on your machine. This guide will assume `~/.zsh/zsh-autosuggestions`.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -16,11 +18,29 @@ It suggests commands as you type, based on command history.
|
|||||||
2. Add the following to your `.zshrc`:
|
2. Add the following to your `.zshrc`:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
|
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
|
||||||
autosuggest_start
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Note:** If you're using other zle plugins like `zsh-syntax-highlighting` or `zsh-history-substring-search`, check out the [section on compatibility](#compatibility-with-other-zle-plugins) below.
|
3. Start a new terminal session.
|
||||||
|
|
||||||
|
|
||||||
|
### Oh My Zsh
|
||||||
|
|
||||||
|
1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
git clone git://github.com/tarruda/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Add the plugin to the list of plugins for Oh My Zsh to load:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
plugins=(zsh-autosuggestions)
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Start a new terminal session.
|
||||||
|
|
||||||
|
> **Note:** There is an open issue ([#102](https://github.com/tarruda/zsh-autosuggestions/issues/102)) when using this plugin with `bracketed-paste-magic`, which is enabled by default by Oh My Zsh.
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -34,7 +54,9 @@ If you invoke the `forward-word` widget, it will partially accept the suggestion
|
|||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
You may want to override the default global config variables after sourcing the plugin. Default values of these variables can be found [here](https://github.com/tarruda/zsh-autosuggestions/blob/master/src/config.zsh).
|
You may want to override the default global config variables after sourcing the plugin. Default values of these variables can be found [here](src/config.zsh).
|
||||||
|
|
||||||
|
**Note:** If you are using Oh My Zsh, you can put this configuration in a file in the `$ZSH_CUSTOM` directory. See their comments on [overriding internals](https://github.com/robbyrussell/oh-my-zsh/wiki/Customization#overriding-internals).
|
||||||
|
|
||||||
|
|
||||||
### Suggestion Highlight Style
|
### Suggestion Highlight Style
|
||||||
@@ -51,8 +73,6 @@ This plugin works by triggering custom behavior when certain [zle widgets](http:
|
|||||||
- `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
|
- `ZSH_AUTOSUGGEST_ACCEPT_WIDGETS`: Widgets in this array will accept the suggestion when invoked.
|
||||||
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked.
|
- `ZSH_AUTOSUGGEST_PARTIAL_ACCEPT_WIDGETS`: Widgets in this array will partially accept the suggestion when invoked.
|
||||||
|
|
||||||
**Note:** These arrays must be set before calling `autosuggest_start`.
|
|
||||||
|
|
||||||
**Note:** A widget shouldn't belong to more than one of the above arrays.
|
**Note:** A widget shouldn't belong to more than one of the above arrays.
|
||||||
|
|
||||||
|
|
||||||
@@ -72,22 +92,6 @@ bindkey '^ ' autosuggest-accept
|
|||||||
|
|
||||||
## Compatibility With Other ZLE Plugins
|
## Compatibility With Other ZLE Plugins
|
||||||
|
|
||||||
### [`zsh-syntax-highlighting`](https://github.com/zsh-users/zsh-syntax-highlighting)
|
|
||||||
|
|
||||||
Source `zsh-autosuggestions.zsh` *before* `zsh-syntax-highlighting`.
|
|
||||||
|
|
||||||
Call `autosuggest_start` *after* sourcing `zsh-syntax-highlighting`.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
|
|
||||||
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
|
|
||||||
|
|
||||||
autosuggest_start
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search)
|
### [`zsh-history-substring-search`](https://github.com/zsh-users/zsh-history-substring-search)
|
||||||
|
|
||||||
When the buffer is empty and one of the `history-substring-search-up/down` widgets is invoked, it will call the `up/down-line-or-history` widget. If the `up/down-line-or-history` widgets are in `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` (the list of widgets that clear the suggestion), this can create an infinite recursion, crashing the shell session.
|
When the buffer is empty and one of the `history-substring-search-up/down` widgets is invoked, it will call the `up/down-line-or-history` widget. If the `up/down-line-or-history` widgets are in `ZSH_AUTOSUGGEST_CLEAR_WIDGETS` (the list of widgets that clear the suggestion), this can create an infinite recursion, crashing the shell session.
|
||||||
@@ -106,18 +110,14 @@ Additionally, the `history-substring-search-up` and `history-substring-search-do
|
|||||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
|
||||||
```
|
```
|
||||||
|
|
||||||
Make sure you add/remove these widgets *before* calling `autosuggest_start`.
|
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
source ~/.zsh/zsh-autosuggestions/dist/autosuggestions.zsh
|
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
|
||||||
source ~/Code/zsh-history-substring-search/zsh-history-substring-search.zsh
|
source ~/Code/zsh-history-substring-search/zsh-history-substring-search.zsh
|
||||||
|
|
||||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS=("${(@)ZSH_AUTOSUGGEST_CLEAR_WIDGETS:#(up|down)-line-or-history}")
|
||||||
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
|
ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(history-substring-search-up history-substring-search-down)
|
||||||
|
|
||||||
autosuggest_start
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ When reporting an issue, please include:
|
|||||||
|
|
||||||
### Build Process
|
### Build Process
|
||||||
|
|
||||||
Edit the source files in `src/`. Run `make` to build `dist/autosuggestions.zsh` from those source files.
|
Edit the source files in `src/`. Run `make` to build `zsh-autosuggestions.zsh` from those source files.
|
||||||
|
|
||||||
|
|
||||||
### Pull Requests
|
### Pull Requests
|
||||||
|
|||||||
229
script/test.zsh
229
script/test.zsh
@@ -2,11 +2,18 @@
|
|||||||
|
|
||||||
SCRIPT_DIR=$(dirname "$0")
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
TEST_DIR=$SCRIPT_DIR/../test
|
TEST_DIR=$SCRIPT_DIR/../test
|
||||||
DIST_DIR=$SCRIPT_DIR/../dist
|
DIST_DIR=$SCRIPT_DIR/../
|
||||||
|
|
||||||
source $DIST_DIR/autosuggestions.zsh
|
# Use stub.sh for stubbing/mocking
|
||||||
|
source $TEST_DIR/stub-1.0.2.sh
|
||||||
|
|
||||||
testDefaultHighlightStyle() {
|
source $DIST_DIR/zsh-autosuggestions.zsh
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Highlighting #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
testHighlightDefaultStyle() {
|
||||||
assertEquals \
|
assertEquals \
|
||||||
"fg=8" \
|
"fg=8" \
|
||||||
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
"$ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE"
|
||||||
@@ -23,12 +30,12 @@ testHighlightApplyWithSuggestion() {
|
|||||||
_zsh_autosuggest_highlight_apply
|
_zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
assertEquals \
|
assertEquals \
|
||||||
"adds to region_highlight with correct style" \
|
"highlight did not use correct style" \
|
||||||
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
|
"0 2 fg=1 2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
|
||||||
"$region_highlight"
|
"$region_highlight"
|
||||||
|
|
||||||
assertEquals \
|
assertEquals \
|
||||||
"saves the higlight to be removed later" \
|
"higlight was not saved to be removed later" \
|
||||||
"2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
|
"2 10 $ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE" \
|
||||||
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
||||||
|
|
||||||
@@ -43,12 +50,12 @@ testHighlightApplyWithoutSuggestion() {
|
|||||||
_zsh_autosuggest_highlight_apply
|
_zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
assertEquals \
|
assertEquals \
|
||||||
"leaves region_highlight alone" \
|
"region_highlight was modified" \
|
||||||
"0 4 fg=1" \
|
"0 4 fg=1" \
|
||||||
"$region_highlight"
|
"$region_highlight"
|
||||||
|
|
||||||
assertNull \
|
assertNull \
|
||||||
"clears the last highlight" \
|
"last highlight region was not cleared" \
|
||||||
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,15 +68,219 @@ testHighlightReset() {
|
|||||||
_zsh_autosuggest_highlight_reset
|
_zsh_autosuggest_highlight_reset
|
||||||
|
|
||||||
assertEquals \
|
assertEquals \
|
||||||
"removes last highlight region" \
|
"last highlight region was not removed" \
|
||||||
"0 1 fg=1 1 2 fg=1" \
|
"0 1 fg=1 1 2 fg=1" \
|
||||||
"$region_highlight"
|
"$region_highlight"
|
||||||
|
|
||||||
assertNull \
|
assertNull \
|
||||||
"clears the last highlight" \
|
"last highlight variable was not cleared" \
|
||||||
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
"$_ZSH_AUTOSUGGEST_LAST_HIGHLIGHT"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
# Widgets #
|
||||||
|
#--------------------------------------------------------------------#
|
||||||
|
|
||||||
|
testWidgetFunctionClear() {
|
||||||
|
BUFFER="ec"
|
||||||
|
POSTDISPLAY="ho hello"
|
||||||
|
|
||||||
|
_zsh_autosuggest_clear
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was modified" \
|
||||||
|
"ec" \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertNull \
|
||||||
|
"POSTDISPLAY was not cleared" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetFunctionModify() {
|
||||||
|
BUFFER=""
|
||||||
|
POSTDISPLAY=""
|
||||||
|
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'BUFFER+="e"'
|
||||||
|
|
||||||
|
stub_and_echo \
|
||||||
|
_zsh_autosuggest_suggestion \
|
||||||
|
"echo hello"
|
||||||
|
|
||||||
|
_zsh_autosuggest_modify
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"original widget not invoked" \
|
||||||
|
"stub_called _zsh_autosuggest_invoke_original_widget"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was not modified" \
|
||||||
|
"e" \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"POSTDISPLAY does not contain suggestion" \
|
||||||
|
"cho hello" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
|
||||||
|
restore _zsh_autosuggest_invoke_original_widget
|
||||||
|
restore _zsh_autosuggest_suggestion
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetFunctionAcceptCursorAtEnd() {
|
||||||
|
BUFFER="echo"
|
||||||
|
POSTDISPLAY=" hello"
|
||||||
|
CURSOR=4
|
||||||
|
|
||||||
|
stub _zsh_autosuggest_invoke_original_widget
|
||||||
|
|
||||||
|
_zsh_autosuggest_accept
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"original widget not invoked" \
|
||||||
|
"stub_called _zsh_autosuggest_invoke_original_widget"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was not modified" \
|
||||||
|
"echo hello" \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"POSTDISPLAY was not cleared" \
|
||||||
|
"" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetFunctionAcceptCursorNotAtEnd() {
|
||||||
|
BUFFER="echo"
|
||||||
|
POSTDISPLAY=" hello"
|
||||||
|
CURSOR=2
|
||||||
|
|
||||||
|
stub _zsh_autosuggest_invoke_original_widget
|
||||||
|
|
||||||
|
_zsh_autosuggest_accept
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"original widget not invoked" \
|
||||||
|
"stub_called _zsh_autosuggest_invoke_original_widget"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was modified" \
|
||||||
|
"echo" \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"POSTDISPLAY was modified" \
|
||||||
|
" hello" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetFunctionPartialAcceptCursorMovesOutOfBuffer() {
|
||||||
|
BUFFER="ec"
|
||||||
|
POSTDISPLAY="ho hello"
|
||||||
|
CURSOR=1
|
||||||
|
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
|
||||||
|
|
||||||
|
_zsh_autosuggest_partial_accept
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"original widget not invoked" \
|
||||||
|
"stub_called _zsh_autosuggest_invoke_original_widget"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was not modified correctly" \
|
||||||
|
"echo " \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"POSTDISPLAY was not modified correctly" \
|
||||||
|
"hello" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetFunctionPartialAcceptCursorStaysInBuffer() {
|
||||||
|
BUFFER="echo hello"
|
||||||
|
POSTDISPLAY=" world"
|
||||||
|
CURSOR=1
|
||||||
|
|
||||||
|
stub_and_eval \
|
||||||
|
_zsh_autosuggest_invoke_original_widget \
|
||||||
|
'CURSOR=5; LBUFFER="echo "; RBUFFER="hello"'
|
||||||
|
|
||||||
|
_zsh_autosuggest_partial_accept
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"original widget not invoked" \
|
||||||
|
"stub_called _zsh_autosuggest_invoke_original_widget"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"BUFFER was modified" \
|
||||||
|
"echo hello" \
|
||||||
|
"$BUFFER"
|
||||||
|
|
||||||
|
assertEquals \
|
||||||
|
"POSTDISPLAY was modified" \
|
||||||
|
" world" \
|
||||||
|
"$POSTDISPLAY"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetAccept() {
|
||||||
|
stub _zsh_autosuggest_highlight_reset
|
||||||
|
stub _zsh_autosuggest_accept
|
||||||
|
stub _zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
|
# Call the function pointed to by the widget since we can't call
|
||||||
|
# the widget itself when zle is not active
|
||||||
|
${widgets[autosuggest-accept]#*:}
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"autosuggest-accept widget does not exist" \
|
||||||
|
"zle -l autosuggest-accept"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"highlight_reset was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_highlight_reset"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"widget function was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_accept"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"highlight_apply was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_highlight_apply"
|
||||||
|
}
|
||||||
|
|
||||||
|
testWidgetClear() {
|
||||||
|
stub _zsh_autosuggest_highlight_reset
|
||||||
|
stub _zsh_autosuggest_clear
|
||||||
|
stub _zsh_autosuggest_highlight_apply
|
||||||
|
|
||||||
|
# Call the function pointed to by the widget since we can't call
|
||||||
|
# the widget itself when zle is not active
|
||||||
|
${widgets[autosuggest-clear]#*:}
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"autosuggest-clear widget does not exist" \
|
||||||
|
"zle -l autosuggest-clear"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"highlight_reset was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_highlight_reset"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"widget function was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_clear"
|
||||||
|
|
||||||
|
assertTrue \
|
||||||
|
"highlight_apply was not called" \
|
||||||
|
"stub_called _zsh_autosuggest_highlight_apply"
|
||||||
|
}
|
||||||
|
|
||||||
# For zsh compatibility
|
# For zsh compatibility
|
||||||
setopt shwordsplit
|
setopt shwordsplit
|
||||||
SHUNIT_PARENT=$0
|
SHUNIT_PARENT=$0
|
||||||
|
|||||||
@@ -4,7 +4,10 @@
|
|||||||
#-------#
|
#-------#
|
||||||
|
|
||||||
# Start the autosuggestion widgets
|
# Start the autosuggestion widgets
|
||||||
autosuggest_start() {
|
_zsh_autosuggest_start() {
|
||||||
_zsh_autosuggest_check_deprecated_config
|
_zsh_autosuggest_check_deprecated_config
|
||||||
_zsh_autosuggest_bind_widgets
|
_zsh_autosuggest_bind_widgets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
add-zsh-hook precmd _zsh_autosuggest_start
|
||||||
|
|||||||
419
test/stub-1.0.2.sh
Normal file
419
test/stub-1.0.2.sh
Normal file
@@ -0,0 +1,419 @@
|
|||||||
|
# !/usr/bin/env bash
|
||||||
|
#
|
||||||
|
# stub.sh 1.0.2 - stubbing helpers for simplifying bash script tests.
|
||||||
|
# https://github.com/jimeh/stub.sh
|
||||||
|
#
|
||||||
|
# (The MIT License)
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Jim Myhrberg.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to
|
||||||
|
# deal in the Software without restriction, including without limitation the
|
||||||
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||||
|
# sell copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
# IN THE SOFTWARE.
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Stub given command.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of command to stub.
|
||||||
|
# - $2: (optional) When set to "STDOUT", echo a default message to STDOUT.
|
||||||
|
# When set to "STDERR", echo default message to STDERR.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
|
stub() {
|
||||||
|
local redirect="null"
|
||||||
|
if [ "$2" = "stdout" ] || [ "$2" = "STDOUT" ]; then redirect=""; fi
|
||||||
|
if [ "$2" = "stderr" ] || [ "$2" = "STDERR" ]; then redirect="stderr"; fi
|
||||||
|
|
||||||
|
stub_and_echo "$1" "$1 stub: \$@" "$redirect"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Stub given command, and echo given string.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of command to stub.
|
||||||
|
# - $2: String to echo when stub is called.
|
||||||
|
# - $3: (optional) When set to "STDERR", echo to STDERR instead of STDOUT.
|
||||||
|
# When set to "null", all output is redirected to /dev/null.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
|
stub_and_echo() {
|
||||||
|
local redirect=""
|
||||||
|
if [ "$3" = "stderr" ] || [ "$3" = "STDERR" ]; then redirect=" 1>&2"; fi
|
||||||
|
if [ "$3" = "null" ]; then redirect=" &>/dev/null"; fi
|
||||||
|
|
||||||
|
stub_and_eval "$1" "echo \"$2\"$redirect"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Stub given command, and execute given string with eval.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of command to stub.
|
||||||
|
# - $2: String to eval when stub is called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
|
stub_and_eval() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
# Setup empty list of active stubs.
|
||||||
|
if [ -z "$STUB_ACTIVE_STUBS" ]; then STUB_ACTIVE_STUBS=(); fi
|
||||||
|
|
||||||
|
# If stubbing a function, store non-stubbed copy of it required for restore.
|
||||||
|
if [ -n "$(command -v "$cmd")" ]; then
|
||||||
|
if [ -z "$(command -v "non_stubbed_${cmd}")" ]; then
|
||||||
|
if [[ "$(type "$cmd" | head -1)" == *"is a function" ]]; then
|
||||||
|
local source="$(type "$cmd" | tail -n +2)"
|
||||||
|
source="${source/$cmd/non_stubbed_${cmd}}"
|
||||||
|
eval "$source"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Prepare stub index and call list for this stub.
|
||||||
|
__stub_register "$cmd"
|
||||||
|
|
||||||
|
# Keep track of what is currently stubbed to ensure restore only acts on
|
||||||
|
# actual stubs.
|
||||||
|
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $cmd "* ]]; then
|
||||||
|
STUB_ACTIVE_STUBS+=("$cmd")
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create the stub.
|
||||||
|
eval "$( printf "%s" "${cmd}() { __stub_call \"${cmd}\" \$@; $2;}")"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called. Returns 0 if yes, 1 if no.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) is stub has been called, 1 (error) otherwise.
|
||||||
|
stub_called() {
|
||||||
|
if [ "$(stub_called_times "$1")" -lt 1 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called with specific arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# stub uname
|
||||||
|
# uname
|
||||||
|
# uname -r -a
|
||||||
|
# stub_called_with uname # Returns 0 (success).
|
||||||
|
# stub_called_with uname -r # Returns 1 (error).
|
||||||
|
# stub_called_with uname -r -a # Returns 0 (success).
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if specified stub has been called with given arguments,
|
||||||
|
# otherwise returns 1 (error).
|
||||||
|
stub_called_with() {
|
||||||
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt 1 ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out how many times a stub has been called.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
#
|
||||||
|
# Echoes number of times stub has been called if $2 is not given, otherwise
|
||||||
|
# echoes nothing.
|
||||||
|
# Returns 0 (success) if $2 is not given, or if it is given and it matches the
|
||||||
|
# number of times the stub has been called. Otherwise 1 (error) is returned if
|
||||||
|
# it doesn't match..
|
||||||
|
stub_called_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
local index="$(__stub_index "$1")"
|
||||||
|
local count=0
|
||||||
|
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "count=\"\${#STUB_${index}_CALLS[@]}\""
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called exactly the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Exact number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_exactly_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" != "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called at least the given number of times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times, otherwise 1 (error) is returned.
|
||||||
|
stub_called_at_least_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" -lt "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
|
# times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
|
# times, otherwise 1 (error) is returned.
|
||||||
|
stub_called_at_most_times() {
|
||||||
|
if [ "$(stub_called_times "$1")" -gt "$2" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out how many times a stub has been called with specific
|
||||||
|
# arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes number of times stub has been called with given arguments.
|
||||||
|
# Return 0 (success).
|
||||||
|
stub_called_with_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" = "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local count=0
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "local calls=(\"\${STUB_${index}_CALLS[@]}\")"
|
||||||
|
for call in "${calls[@]}"; do
|
||||||
|
if [ "$call" = "$args" ]; then ((count++)); fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $count
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called exactly the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Exact number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_exactly_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" != "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called at least the given number of times
|
||||||
|
# with specified arguments.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Minimum required number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called at least the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_at_least_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -lt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Find out if stub has been called no more than the given number of
|
||||||
|
# times.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of stubbed command.
|
||||||
|
# - $2: Maximum allowed number of times stub has been called.
|
||||||
|
# - $@: Any/all additional arguments are used to specify what stub was
|
||||||
|
# called with.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns 0 (success) if stub has been called no more than the given number of
|
||||||
|
# times with specified arguments, otherwise 1 (error) is returned.
|
||||||
|
stub_called_with_at_most_times() {
|
||||||
|
local cmd="$1"
|
||||||
|
local count="$2"
|
||||||
|
shift 2
|
||||||
|
|
||||||
|
if [ "$(stub_called_with_times "$cmd" $@)" -gt "$count" ]; then
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Public: Restore the original command/function that was stubbed.
|
||||||
|
#
|
||||||
|
# Arguments:
|
||||||
|
# - $1: Name of command to restore.
|
||||||
|
#
|
||||||
|
# Echoes nothing.
|
||||||
|
# Returns nothing.
|
||||||
|
restore() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
# Don't do anything if the command isn't currently stubbed.
|
||||||
|
if [[ " ${STUB_ACTIVE_STUBS[@]} " != *" $1 "* ]]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Remove stub functions.
|
||||||
|
unset -f "$cmd"
|
||||||
|
|
||||||
|
# Remove stub from list of active stubs.
|
||||||
|
STUB_ACTIVE_STUBS=(${STUB_ACTIVE_STUBS[@]/$cmd/})
|
||||||
|
|
||||||
|
# If stub was for a function, restore the original function.
|
||||||
|
if type "non_stubbed_${cmd}" &>/dev/null; then
|
||||||
|
local original_type="$(type "non_stubbed_${cmd}" | head -1)"
|
||||||
|
if [[ "$original_type" == *"is a function" ]]; then
|
||||||
|
local source="$(type "non_stubbed_$cmd" | tail -n +2)"
|
||||||
|
source="${source/non_stubbed_${cmd}/$cmd}"
|
||||||
|
eval "$source"
|
||||||
|
unset -f "non_stubbed_${cmd}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Internal functions
|
||||||
|
#
|
||||||
|
|
||||||
|
# Private: Used to keep track of which stubs have been called and how many
|
||||||
|
# times.
|
||||||
|
__stub_call() {
|
||||||
|
local cmd="$1"
|
||||||
|
shift 1
|
||||||
|
local args="$@"
|
||||||
|
if [ "$args" = "" ]; then args="<none>"; fi
|
||||||
|
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "STUB_${index}_CALLS+=(\"\$args\")"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Get index value of stub. Required to access list of stub calls.
|
||||||
|
__stub_index() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
for item in ${STUB_INDEX[@]}; do
|
||||||
|
if [[ "$item" = "${cmd}="* ]]; then
|
||||||
|
local index="$item"
|
||||||
|
index="${index/${cmd}=/}"
|
||||||
|
echo "$index"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# Private: Prepare for the creation of a new stub. Adds stub to index and
|
||||||
|
# sets up an empty call list.
|
||||||
|
__stub_register() {
|
||||||
|
local cmd="$1"
|
||||||
|
|
||||||
|
if [ -z "$STUB_NEXT_INDEX" ]; then STUB_NEXT_INDEX=0; fi
|
||||||
|
if [ -z "$STUB_INDEX" ]; then STUB_INDEX=(); fi
|
||||||
|
|
||||||
|
# Clean up after any previous stub for the same command.
|
||||||
|
__stub_clean "$cmd"
|
||||||
|
|
||||||
|
# Add stub to index.
|
||||||
|
STUB_INDEX+=("${cmd}=${STUB_NEXT_INDEX}")
|
||||||
|
eval "STUB_${STUB_NEXT_INDEX}_CALLS=()"
|
||||||
|
|
||||||
|
# Increment stub count.
|
||||||
|
((STUB_NEXT_INDEX++))
|
||||||
|
}
|
||||||
|
|
||||||
|
# Private: Cleans out and removes a stub's call list, and removes stub from
|
||||||
|
# index.
|
||||||
|
__stub_clean() {
|
||||||
|
local cmd="$1"
|
||||||
|
local index="$(__stub_index "$cmd")"
|
||||||
|
|
||||||
|
# Remove all relevant details from any previously existing stub for the same
|
||||||
|
# command.
|
||||||
|
if [ -n "$index" ]; then
|
||||||
|
eval "unset STUB_${index}_CALLS"
|
||||||
|
STUB_INDEX=(${STUB_INDEX[@]/${cmd}=*/})
|
||||||
|
fi
|
||||||
|
}
|
||||||
1
zsh-autosuggestions.plugin.zsh
Symbolic link
1
zsh-autosuggestions.plugin.zsh
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
zsh-autosuggestions.zsh
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
# Fish-like fast/unobtrusive autosuggestions for zsh.
|
||||||
# https://github.com/tarruda/zsh-autosuggestions
|
# https://github.com/tarruda/zsh-autosuggestions
|
||||||
# v0.1.1
|
# v0.2.0
|
||||||
#
|
|
||||||
# Copyright (c) 2013 Thiago de Arruda
|
# Copyright (c) 2013 Thiago de Arruda
|
||||||
# Copyright (c) 2016 Eric Freese
|
# Copyright (c) 2016 Eric Freese
|
||||||
#
|
#
|
||||||
@@ -339,7 +338,10 @@ _zsh_autosuggest_suggestion() {
|
|||||||
#-------#
|
#-------#
|
||||||
|
|
||||||
# Start the autosuggestion widgets
|
# Start the autosuggestion widgets
|
||||||
autosuggest_start() {
|
_zsh_autosuggest_start() {
|
||||||
_zsh_autosuggest_check_deprecated_config
|
_zsh_autosuggest_check_deprecated_config
|
||||||
_zsh_autosuggest_bind_widgets
|
_zsh_autosuggest_bind_widgets
|
||||||
}
|
}
|
||||||
|
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
add-zsh-hook precmd _zsh_autosuggest_start
|
||||||
Reference in New Issue
Block a user