瀏覽代碼

add support for limiting list length and ensure fzf query takes part in find command

Daniel Sheffield 3 月之前
父節點
當前提交
d332cea0b0
共有 1 個文件被更改,包括 60 次插入15 次删除
  1. 60 15
      recall.sh

+ 60 - 15
recall.sh

@@ -8,7 +8,6 @@ fi
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 
 LIST_MODE=false
-NUM=1
 
 usage () {
 	cat <<EOF
@@ -19,52 +18,98 @@ the captured exit status.
 
   examples:
     - Print the output of last captured find command
-      "${0##*/} find
+
+      ${0##*/} find
 
     - List paths to all captured commands
-      "${0##*/} -l
+
+      ${0##*/} -l
 
     - List paths to all captured find commands
-      "${0##*/} -l find
+
+      ${0##*/} -l find
 EOF
 }
-OPTSTRING=":hl"
-while getopts "${OPTSTRING}" opt; do
+
+OPTSTRING=":hln:"
+parse_opt () {
 	case "${opt}" in
-		l)
-			LIST_MODE=true; shift 1;
-		;;
 		h)
 			usage
 			exit 0
 		;;
+		l)
+			LIST_MODE=true
+		;;
+		n)
+			if [ "${OPTARG:-}" ] && [[ "$OPTARG" =~ [0-9]+ ]] && (( OPTARG > 0 ))
+			then
+				NUM="$OPTARG"
+			else
+				echo "-n paremeter must be an integer greater than zero" >&2
+				echo >&2
+				usage >&2
+				exit 1
+			fi
+		;;
 		\?)
-			echo "Unknown switch: $OPTARG" >&2
+			echo "Unknown switch: -$OPTARG" >&2
 			echo >&2
 			usage >&2
 			exit 1
 		;;
 	esac
+}
+
+validate () {
+	# exit if vars are not valid
+	# ie, set any dynamic defaults here
+	if ! "$LIST_MODE" && [ "${NUM:=1}" ] && ! (( NUM == 1 ))
+	then
+		echo "-n != 1 can only be specified with -l" >&2
+		echo >&2
+		usage >&2
+		exit 1
+	fi
+	if ! "$LIST_MODE" && [ "$#" == "0" ]
+	then
+		echo "PROG must be provided" >&2
+		echo >&2
+		usage >&2
+		exit 1
+	fi
+}
+
+while getopts "${OPTSTRING}" opt
+do
+	parse_opt
 done
+shift $((OPTIND-1))
+
+validate "$@"
 
 if "$LIST_MODE"
 then
-	query=""
+	fzf_query=()
+	find_name=()
 	if [ "${1:-}" ]
 	then
-		query="--query=$1"
+		fzf_query=( "--query=$1" )
+		find_name=( -wholename "**/$1/**" )
 	fi
 	(
 		cd "$LOG_ROOT"
-		find . -type d \
-			\( \
+		find . \
+			\( -type l -o -type d \) -wholename './????????_??????_?????????' -prune -o -type d "${find_name[@]}" \( \
 				-links 2 \
 				-o \( \
 					-links 1 -exec bash -c '! [ "$(find "$1" -mindepth 1 -type d)" ]' find-bash {} \; \
 				\) \
 			\) \
 			-print0 \
-		| fzf --read0 $query -d / --nth 1,2,3 --preview='cat {}/info; head {}/dat'
+		| sort -zV \
+		| head -zn "${NUM:--0}" \
+		| fzf --read0 --no-sort "${fzf_query[@]}" -d / --nth 1,2,3 --preview='cat {}/info; head {}/dat'
 	)
 else
 	SUBPROG=""