Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ import { Command } from '@cliffy/command'

export const initCommand = new Command()
.description('Render the widget for zsh.')
.option('--bindkey <KEY:string>', 'The key to bind the widget to.')
.option(
'--bindkey <KEY:string>',
`
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)
Expand Down
17 changes: 14 additions & 3 deletions src/widget.eta
Original file line number Diff line number Diff line change
Expand Up @@ -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' %>
<% } %>