Improve zsh widget (#92)

This commit is contained in:
Denis Isidoro 2019-09-27 14:31:56 -03:00
parent b9084c0439
commit 260966ba6c
6 changed files with 45 additions and 5 deletions

View file

@ -21,6 +21,7 @@ Table of Contents
* [Preventing execution](#preventing-execution)
* [Pre-filtering](#pre-filtering)
* [Searching online repositories](#searching-online-repositories)
* [Shell widget](#shell-widget)
* [More options](#more-options)
* [Trying out online](#trying-out-online)
* [Motivation](#motivation)
@ -84,6 +85,23 @@ If you run `navi search <cmd>`, **navi** will try to download cheatsheets from o
Please note that these cheatsheets aren't curated by **navi**'s maintainers and should be taken with a grain of salt. If you're not sure about executing these snippets, make sure to check the preview window or use the `--print` option.
### Shell widget
You can use **navi** as a widget to your shell:
![Widget](https://user-images.githubusercontent.com/3226564/65788699-03361080-e132-11e9-9e93-b2a4bc405bed.gif)
This way, your history is correctly populated and you can edit the command as you wish before executing it.
Right now, there's only a widget for zsh. If you want for other shells, please upvote [this issue](https://github.com/denisidoro/navi/issues/37).
In order to use it, add this line to your `.zshrc`-like file:
```sh
source "$(navi widget zsh)"
```
Then, simply hit `Alt+G`.
### More options
Please refer to `navi --help` for more details.

View file

@ -1,4 +1,4 @@
% awk, string
# Print last column
echo "1 2 3" | awk '{print $NF}'
awk '{print $NF}'

View file

@ -1,7 +1,9 @@
_call_navi() {
# zle kill-whole-line
zle -U "$(NAVI_FZF_ALL_INPUTS=true navi --print "$(echo "$CUTBUFFER" | xargs)" <> /dev/tty)"
zle accept-line
local buff="$BUFFER"
zle kill-whole-line
local cmd="$(NAVI_USE_FZF_ALL_INPUTS=true navi --print <> /dev/tty)"
zle -U "$buff$(echo "$cmd")"
# zle accept-line
}
zle -N _call_navi

View file

@ -60,7 +60,7 @@ arg::pick() {
if [ -n "$suggestions" ]; then
echo "$suggestions" | ui::pick --prompt "$arg: " --header-lines "${headers:-0}" | str::column "${column:-}"
fi
elif ${NAVI_FZF_ALL_INPUTS:-false}; then
elif ${NAVI_USE_FZF_ALL_INPUTS:-false}; then
echo "" | ui::pick --prompt "$arg: " --print-query --height 1
else
printf "\033[0;36m${arg}:\033[0;0m " > /dev/tty

View file

@ -89,6 +89,21 @@ handler::home() {
echo "${SCRIPT_DIR}"
}
handler::widget() {
local widget
case "$SH" in
zsh) widget="${SCRIPT_DIR}/shell/widget.zsh" ;;
*) echoerr "Invalid shell: $SH"; exit 1 ;;
esac
if "$(dict::get "$OPTIONS" print)"; then
cat "$widget"
else
echo "$widget"
fi
}
main() {
case "$(dict::get "$OPTIONS" entry_point)" in
preview)
@ -117,6 +132,9 @@ main() {
help)
handler::help
;;
widget)
handler::widget
;;
*)
health::fzf
handler::main

View file

@ -27,6 +27,7 @@ opts::eval() {
best|b) best=true; wait_for="best"; shift ;;
home) entry_point="home"; shift ;;
script) entry_point="script"; shift; SCRIPT_ARGS="$@" ;;
widget) entry_point="widget"; shift; wait_for="widget" ;;
esac
for arg in "$@"; do
@ -35,6 +36,7 @@ opts::eval() {
preview) query="$(arg::deserialize "$arg")"; wait_for="" ;;
search) query="$arg"; wait_for=""; path="${path}:$(search::full_path "$query")"; ;;
query|best) query="$arg"; wait_for="" ;;
widget) SH="$arg"; wait_for="" ;;
esac
case $arg in