From 685752803777c4c2dde5fec3d1b6a02f85a6b3f8 Mon Sep 17 00:00:00 2001 From: moeneuron Date: Sat, 31 Aug 2024 22:44:54 +0200 Subject: [PATCH] feat(zfunctions): add search and select history --- zfunctions/peco_select_history.zsh | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 zfunctions/peco_select_history.zsh diff --git a/zfunctions/peco_select_history.zsh b/zfunctions/peco_select_history.zsh new file mode 100755 index 0000000..89944aa --- /dev/null +++ b/zfunctions/peco_select_history.zsh @@ -0,0 +1,36 @@ +#!/usr/bin/env zsh + +# Default configurations +typeset -gA PECO_HISTORY_CONFIG=( + [layout]="bottom-up" + [prompt]="HISTORY>" + [initial_filter]="Fuzzy" + [extra_flags]="" + [keymap]="^R" +) + +peco_select_history() { + local peco_flags=( + "--layout=${PECO_HISTORY_CONFIG[layout]}" + "--prompt" "${PECO_HISTORY_CONFIG[prompt]}" + "--initial-filter=${PECO_HISTORY_CONFIG[initial_filter]}" + ) + [ -n "${PECO_HISTORY_CONFIG[extra_flags]}" ] && peco_flags+=("${(z)PECO_HISTORY_CONFIG[extra_flags]}") + [ $# -ne 0 ] && peco_flags+=("--query" "$1") + + local selected=$(fc -rl 1 | + awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | + peco "${peco_flags[@]}" | + awk '{$1=""; print substr($0,2)}') + + if [ -n "$selected" ]; then + BUFFER=$selected + CURSOR=$#BUFFER + zle redisplay + else + zle reset-prompt + fi +} + +zle -N peco_select_history +bindkey "${PECO_HISTORY_CONFIG[keymap]}" peco_select_history \ No newline at end of file