diff --git a/src/init.ts b/src/init.ts index c4c9608..45c6485 100644 --- a/src/init.ts +++ b/src/init.ts @@ -4,15 +4,26 @@ import { Command } from '@cliffy/command' export const initCommand = new Command() .description('Render the widget for zsh.') - .option('--bindkey ', 'The key to bind the widget to.') + .option( + '--bindkey ', + ` + The key to bind the widget to. + The widget is triggered only when a key is pressed while the prompt buffer is empty. + + If you want the widget to be triggered regardless of the state of the prompt buffer, add the --bindkey-global option. + `, + ) + .option('--bindkey-global', 'Whether to bind the widget globally.', { default: false, depends: ['bindkey'] }) .example('eval "$(wk init)"', 'Register the widget without binding it to a key.') - .example(`eval "$(wk init --bindkey '^G')"`, 'Register the widget and bind it to Ctrl-G.') - .action(({ bindkey }) => { + .example(`eval "$(wk init --bindkey ',')"`, 'Register the widget and bind it to the key `,`.') + .example(`eval "$(wk init --bindkey '^G' --bindkey-global)"`, 'Register the widget and bind it to Ctrl-G globally.') + .action(({ bindkey, bindkeyGlobal }) => { const eta = new Eta() const rendered = eta.renderString(WIDGET_TEMPLATE, { wk_path: Deno.execPath(), bindkey, + bindkeyGlobal, }) console.log(rendered) diff --git a/src/widget.eta b/src/widget.eta index f72eadd..3ad2575 100644 --- a/src/widget.eta +++ b/src/widget.eta @@ -43,10 +43,21 @@ _wk_widget() { zle $accept_widget fi } - zle -N _wk_widget + +<% if (!it.bindkeyGlobal) { %> +_wk_or_self_insert() { + if [[ -z "$BUFFER" ]]; then + _wk_widget + else + zle self-insert + fi +} +zle -N _wk_or_self_insert +<% } %> + <% if (typeof it.bindkey === 'string' && it.bindkey === "'") { %> -bindkey "'" _wk_widget +bindkey "'" <%= it.bindkeyGlobal ? '_wk_widget' : '_wk_or_self_insert' %> <% } else if (typeof it.bindkey === 'string' && it.bindkey !== '') { %> -bindkey '<%~ it.bindkey %>' _wk_widget +bindkey '<%~ it.bindkey %>' <%= it.bindkeyGlobal ? '_wk_widget' : '_wk_or_self_insert' %> <% } %>