summary refs log tree commit diff
path: root/bin/libfuzl.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/libfuzl.sh')
-rw-r--r--bin/libfuzl.sh39
1 files changed, 33 insertions, 6 deletions
diff --git a/bin/libfuzl.sh b/bin/libfuzl.sh
index a28c861..c53a71f 100644
--- a/bin/libfuzl.sh
+++ b/bin/libfuzl.sh
@@ -4,9 +4,12 @@
 
 set -eu
 
+# TODO: use custom-1 somehow
+
 # pick up configuration
 FUZL_PROMPT=${FUZL_PROMPT:-fuzl}
 FUZL_ICON=${FUZL_ICON:-󰈲}
+FUZL_CLI_ARGS=${FUZL_CLI_ARGS:-$@}
 
 fuzl_try() {
 	[ "$1" == "--" ] && shift 1
@@ -89,6 +92,11 @@ fuzl_loading_cancel() {
 
 FUZL_ACTIONS=()
 
+fuzl_action_reset() {
+	# reset actions
+	FUZL_ACTIONS=()
+}
+
 fuzl_action_add() {
 	local action="${1:?}" icon="${2:?}"
 	shift 2
@@ -97,17 +105,36 @@ fuzl_action_add() {
 	FUZL_ACTIONS+=("${action}\t${icon}\t${*}")
 }
 
+fuzl_action_help() {
+	echo "usage: $FUZL_PROMPT [action]"
+	echo
+	for action in "${FUZL_ACTIONS[@]}"; do
+		echo -e "       $action"
+	done
+	echo
+	exit 0
+}
+
 fuzl_action_menu() {
 	local action
 
-	action=$(printf '%s\n' "${FUZL_ACTIONS[@]}" |
-		FUZL_PROMPT="$FUZL_PROMPT: $*" fuzl_menu $FUZL_MENU_FILTER --with-nth='{2}  {3..}' --accept-nth=1)
+	# only actually ask if action wasn't provided on cli
+	action=${FUZL_CLI_ARGS}
+	if [[ "$action" == "--help" ]]; then
+		fuzl_action_help
+	fi
+	if test -z "$action"; then
+		action=$(printf '%s\n' "${FUZL_ACTIONS[@]}" |
+			FUZL_PROMPT="$FUZL_PROMPT: $*" fuzl_menu $FUZL_MENU_FILTER --with-nth='{2}  {3..}' --accept-nth=1)
+	fi
 
-	# reset actions
-	FUZL_ACTIONS=()
+	fuzl_action_reset
 
-	test -n "$action" || return
-	eval "${FUZL_PROMPT}_${action}" "'$*'"
+	if declare -f "${FUZL_PROMPT}_${action}" >/dev/null; then
+		eval "${FUZL_PROMPT}_${action}" "'$*'"
+	else
+		return 1
+	fi
 }
 
 # setup common debug helper