Use fzf by default for build-dir selector

This commit is contained in:
Kenneth Benzie 2024-05-01 14:27:19 +01:00
parent f50db402af
commit 0efb635f02

View File

@ -85,97 +85,110 @@ EOF
if [[ ${#local_build_dirs} -eq 0 ]]; then if [[ ${#local_build_dirs} -eq 0 ]]; then
error "no build directories found"; return 1 error "no build directories found"; return 1
elif [[ ${#local_build_dirs} -gt 1 ]]; then elif [[ ${#local_build_dirs} -gt 1 ]]; then
zmodload zsh/curses && { if command -v fzf &> /dev/null; then
# Get the size of the terminal # Use fzf to select a build directory
local size=`stty size` local_build_dir=$(
integer height=${size% *} printf '%s\n' "${local_build_dir[@]}" $local_build_dirs |
integer width=${size#* } fzf --layout=reverse --info=hidden --border=rounded \
--height=$(( ${#local_build_dirs} + 5 ))
)
if [[ $? -ne 0 ]]; then
return 1
fi
else
# Fallback to zcurses selector when fzf is not available
zmodload zsh/curses && {
# Get the size of the terminal
local size=`stty size`
integer height=${size% *}
integer width=${size#* }
# Create the window and hide the cursor # Create the window and hide the cursor
zcurses init zcurses init
zcurses addwin build-dir $height $width 0 0 zcurses addwin build-dir $height $width 0 0
# Hide the cursor for zcurses, trap SIGINT to ensure cleanup in # Hide the cursor for zcurses, trap SIGINT to ensure cleanup in
# always-list occurs below # always-list occurs below
tput civis; trap 'return 130' INT tput civis; trap 'return 130' INT
# Enter display loop # Enter display loop
local key keypad local key keypad
while (( 1 )); do while (( 1 )); do
zcurses clear build-dir zcurses clear build-dir
# Add the prompt text # Add the prompt text
zcurses move build-dir 1 1 zcurses move build-dir 1 1
zcurses string build-dir 'Select a build directory:' zcurses string build-dir 'Select a build directory:'
# Add the selections text # Add the selections text
for (( i = 0; i < ${#local_build_dirs}; i++ )); do for (( i = 0; i < ${#local_build_dirs}; i++ )); do
integer line=$i+3 integer line=$i+3
zcurses move build-dir $line 1 zcurses move build-dir $line 1
[[ $index -eq $i ]] && [[ $index -eq $i ]] &&
zcurses string build-dir "* " || zcurses string build-dir "* " ||
zcurses string build-dir " " zcurses string build-dir " "
zcurses string build-dir ${local_build_dirs[$i+1]} zcurses string build-dir ${local_build_dirs[$i+1]}
done
# Display the text the and wait for input
zcurses refresh build-dir
zcurses input build-dir key keypad
# Handle user input
case $key in
(UP|k|$'\C-P')
[[ $index -gt 0 ]] && index=$index-1 ;;
(DOWN|j|$'\C-N')
[[ $index -lt ${#local_build_dirs}-1 ]] && index=$index+1 ;;
(ENTER|$'\n')
break ;;
esac
done done
} always {
# Restore the cursor and cleanup zcurses
tput cvvis; tput cnorm
zcurses delwin build-dir
zcurses end
}
# Display the text the and wait for input # On success setup the build directory for use
zcurses refresh build-dir if [[ $? -eq 0 ]]; then
zcurses input build-dir key keypad # Set the build directory from selection if empty
[[ -z $local_build_dir ]] && \
# Handle user input local_build_dir=${local_build_dirs[$index+1]}
case $key in fi
(UP|k|$'\C-P') fi
[[ $index -gt 0 ]] && index=$index-1 ;;
(DOWN|j|$'\C-N')
[[ $index -lt ${#local_build_dirs}-1 ]] && index=$index+1 ;;
(ENTER|$'\n')
break ;;
esac
done
} always {
# Restore the cursor and cleanup zcurses
tput cvvis; tput cnorm
zcurses delwin build-dir
zcurses end
}
fi fi
fi fi
# On success setup the build directory for use # If `build.ninja` exists in alias `ninja`, return.
if [[ $? -eq 0 ]]; then local build
# Set the build directory from selection if empty [ -f $local_build_dir/build.ninja ] && \
[[ -z $local_build_dir ]] && \ build="ninja -C $local_build_dir"
local_build_dir=${local_build_dirs[$index+1]}
# If `build.ninja` exists in alias `ninja`, return. # If `Makefile` exists in alias `make`, return.
local build if [ -f $local_build_dir/Makefile ]; then
[ -f $local_build_dir/build.ninja ] && \ [ `uname` = Darwin ] && \
build="ninja -C $local_build_dir" local cpu_count=`sysctl -n hw.ncpu` ||
local cpu_count=`grep -c '^processor' /proc/cpuinfo`
build="make -j $cpu_count -C $local_build_dir"
fi
# If `Makefile` exists in alias `make`, return. # If the build variable is not defined the command could not be determined
if [ -f $local_build_dir/Makefile ]; then if [ -z $build ]; then
[ `uname` = Darwin ] && \ warning "build command detection failed: $local_build_dir"
local cpu_count=`sysctl -n hw.ncpu` || # Prompt user to enter a build command
local cpu_count=`grep -c '^processor' /proc/cpuinfo` vared -p 'enter comand: ' build
build="make -j $cpu_count -C $local_build_dir" fi
fi
# If the build variable is not defined the command could not be determined # Redefine the `build` alias and update the `~build` hash directory
if [ -z $build ]; then alias build="$build"
warning "build command detection failed: $local_build_dir" hash -d build=$local_build_dir
# Prompt user to enter a build command export build_dir=$local_build_dir
vared -p 'enter comand: ' build
fi
# Redefine the `build` alias and update the `~build` hash directory # If `--build` is specified then evaluate the command.
alias build="$build" if [[ -n $do_build ]]; then
hash -d build=$local_build_dir eval build
export build_dir=$local_build_dir
# If `--build` is specified then evaluate the command.
if [[ -n $do_build ]]; then
eval build
fi
fi fi
} }