zsh select

This commit is contained in:
2026-03-30 19:30:00 +09:00
parent 3207bab41a
commit cedbc23a14
3 changed files with 39 additions and 3 deletions

View File

@@ -36,12 +36,24 @@ cdselect() {
[[ $i -gt 20 ]] && break [[ $i -gt 20 ]] && break
done done
read -k1 key 2>/dev/null read -k1 key 2>/dev/null
if [[ "$key" == $'\x1b' ]]; then
read -k1 -t 0.1 k2 2>/dev/null
if [[ "$k2" == "[" ]]; then
read -k1 -t 0.1 k3 2>/dev/null
case "$k3" in
A) ((cursor--)) ;; # up
B) ((cursor++)) ;; # down
esac
continue
else
break # plain ESC
fi
fi
case "$key" in case "$key" in
$'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;; $'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;;
$'\x0e') ((cursor++)) ;; # C-n $'\x0e') ((cursor++)) ;; # C-n
$'\x10') ((cursor--)) ;; # C-p $'\x10') ((cursor--)) ;; # C-p
$'\x7f'|$'\b') query="${query%?}"; cursor=1 ;; $'\x7f'|$'\b') query="${query%?}"; cursor=1 ;;
$'\x1b') break ;;
*) query+="$key"; cursor=1 ;; *) query+="$key"; cursor=1 ;;
esac esac
done done

View File

@@ -29,12 +29,24 @@ fileselect() {
[[ $i -gt 20 ]] && break [[ $i -gt 20 ]] && break
done done
read -k1 key 2>/dev/null read -k1 key 2>/dev/null
if [[ "$key" == $'\x1b' ]]; then
read -k1 -t 0.1 k2 2>/dev/null
if [[ "$k2" == "[" ]]; then
read -k1 -t 0.1 k3 2>/dev/null
case "$k3" in
A) ((cursor--)) ;;
B) ((cursor++)) ;;
esac
continue
else
break
fi
fi
case "$key" in case "$key" in
$'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;; $'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;;
$'\x0e') ((cursor++)) ;; # C-n $'\x0e') ((cursor++)) ;; # C-n
$'\x10') ((cursor--)) ;; # C-p $'\x10') ((cursor--)) ;; # C-p
$'\x7f'|$'\b') query="${query%?}"; cursor=1 ;; $'\x7f'|$'\b') query="${query%?}"; cursor=1 ;;
$'\x1b') break ;;
*) query+="$key"; cursor=1 ;; *) query+="$key"; cursor=1 ;;
esac esac
done done

View File

@@ -29,12 +29,24 @@ histselect() {
[[ $i -gt 20 ]] && break [[ $i -gt 20 ]] && break
done done
read -k1 key 2>/dev/null read -k1 key 2>/dev/null
if [[ "$key" == $'\x1b' ]]; then
read -k1 -t 0.1 k2 2>/dev/null
if [[ "$k2" == "[" ]]; then
read -k1 -t 0.1 k3 2>/dev/null
case "$k3" in
A) ((cursor--)) ;;
B) ((cursor++)) ;;
esac
continue
else
break
fi
fi
case "$key" in case "$key" in
$'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;; $'\n'|$'\r') [[ ${#filtered} -gt 0 ]] && selected="${filtered[$cursor]}"; break ;;
$'\x0e') ((cursor++)) ;; # C-n $'\x0e') ((cursor++)) ;; # C-n
$'\x10') ((cursor--)) ;; # C-p $'\x10') ((cursor--)) ;; # C-p
$'\x7f'|$'\b') query="${query%?}"; cursor=1 ;; $'\x7f'|$'\b') query="${query%?}"; cursor=1 ;;
$'\x1b') break ;;
*) query+="$key"; cursor=1 ;; *) query+="$key"; cursor=1 ;;
esac esac
done done