Skip to content

Instantly share code, notes, and snippets.

@wsgac
Last active November 22, 2022 09:32
Show Gist options
  • Save wsgac/338019084bbeaa4ad1d96408680c29bf to your computer and use it in GitHub Desktop.
Save wsgac/338019084bbeaa4ad1d96408680c29bf to your computer and use it in GitHub Desktop.
Fallback selector - uses 'dmenu' under Xorg, 'select' otherwise
# -*- bash -*-
# This is a small selector function inspired, and making use of,
# 'dmenu' from Suckless Tools. Based on the lines piped to it, it
# prompts the user to select one and then returns that choice. When
# run under Xorg, it will make use of 'dmenu', otherwise the builtin
# 'select' construct will be used, providing a fallback mechanism.
function menu-selector {
declare -a items=()
# Capture input items into an array
while IFS= read -r item; do
items+=( "$item" )
done
if xhost &> /dev/null; then
# Xorg environment
print -l ${items[@]} | dmenu -l 10
else
# TTY environment
select item in ${items[@]}; do
echo $item
break
done < /dev/tty
fi
}
@wsgac
Copy link
Author

wsgac commented Feb 11, 2019

Example usage:

 ls -1 | menu-selector | awk '{printf "Your selected file: %s\n",$1}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment