From 213a01ae992a27969b572d981cd06889d3e96dcf Mon Sep 17 00:00:00 2001 From: Yang Liu Date: Wed, 16 Aug 2017 15:22:36 -0700 Subject: [PATCH] Added percol plugin --- plugins/percol/README.md | 31 ++++++++++++++++++++++++++ plugins/percol/percol.plugin.zsh | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 plugins/percol/README.md create mode 100644 plugins/percol/percol.plugin.zsh diff --git a/plugins/percol/README.md b/plugins/percol/README.md new file mode 100644 index 00000000..c405c5f4 --- /dev/null +++ b/plugins/percol/README.md @@ -0,0 +1,31 @@ +## percol + +**Maintainer:** [@robturtle](https://github.com/robturtle) + +It provides two functionalities that allow you search history and resume +background jobs with interactively incremental searching utility powered by +[Percol](https://github.com/mooz/percol). + +### Usage + +1. Use `Ctrl-R` to search the history. + + ![interactively search history](https://www.dropbox.com/s/2ke80q5uswz7sqf/percol.plugin1.gif?raw=1) + +2. Use `Ctrl-Q` to resume background jobs. + + ![interactively resume background jobs](https://www.dropbox.com/s/u5t5l7jeznv06y8/percol.plugin2.gif?raw=1) + +### Installation + +1. [Install percol](https://github.com/mooz/percol) from pip: + + ``` + pip install percol + ``` + +2. Enable the plugin by adding it to your `plugins` definition in `~/.zshrc`: + + ``` + plugins=(percol) + ``` diff --git a/plugins/percol/percol.plugin.zsh b/plugins/percol/percol.plugin.zsh new file mode 100644 index 00000000..8edfc04c --- /dev/null +++ b/plugins/percol/percol.plugin.zsh @@ -0,0 +1,38 @@ +#!/bin/zsh + +function exists { + which $1 &> /dev/null +} + +if exists percol; then + ## Searching zsh history + function percol_select_history() { + local tac + exists gtac && tac="gtac" || { exists tac && tac="tac" || { tac="tail -r" } } + BUFFER=$(fc -l -n 1 | eval $tac | percol --query "$LBUFFER") + CURSOR=$#BUFFER # move cursor + zle -R -c # refresh + } + zle -N percol_select_history + bindkey '^R' percol_select_history + + ## use `fg 1` just like under BASH, no need to type `fg %1` + fg() { + if [[ $# -eq 1 && $1 = - ]]; then + builtin fg %- + else + builtin fg %"$@" + fi + } + + ## Interactively resume background jobs + function percol_resume_job { + ID=$(jobs | grep '\[\d\d*\]' | percol | sed -E 's/\[([0-9]+)\].*/\1/') + if [[ -n ${ID} ]]; then + fg ${ID} + fi + zle -R -c + } + zle -N percol_resume_job + bindkey '^Q' percol_resume_job +fi