Prechádzať zdrojové kódy

add ability to search and preview capture history

Daniel Sheffield 3 mesiacov pred
rodič
commit
e53b4ef398
1 zmenil súbory, kde vykonal 76 pridanie a 27 odobranie
  1. 76 27
      recall.sh

+ 76 - 27
recall.sh

@@ -7,32 +7,81 @@ then
 fi
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 
-SUBPROG=""
-PROG="$1"
-
-#TODO: eval, really?
-#      not fully working anyway (aliases, functions)
-#while [ "$(eval "$PROG")" == "capture" ]
-#do
-	#TODO: eval not sufficient if
-	#      capture is aliased
-	#      or invoked by a function
-#	shift 1; PROG=$1;
-#done
-
-# TODO: and maybe su, runuser et al
-if [ "$PROG" == "sudo" ]
-then
-	SUBPROG="$2"
-fi
+LIST_MODE=false
+NUM=1
+
+usage () {
+	cat <<EOF
+Recall prints the last captured output from the specified PROG and exits with
+the captured exit status.
+
+  usage: "${0##*/} [ -l ] [ -n NUM ] PROG [ ARGS ... ]
+
+  examples:
+    - Print the output of last captured find command
+      "${0##*/} find
 
-LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"
-LOG_DIR="$LOG_DIR"/"$(ls -1 "$LOG_DIR" | sort -V | tail -n1)"
+    - List paths to all captured commands
+      "${0##*/} -l
 
-OUT="$LOG_DIR"/stdout
-ERR="$LOG_DIR"/stderr
-DAT="$LOG_DIR"/dat
-INF="$LOG_DIR"/info
-. <(tail -n +2 "$INF")
-cat "$OUT"
-exit "$status"
+    - List paths to all captured find commands
+      "${0##*/} -l find
+EOF
+}
+OPTSTRING=":hl"
+while getopts "${OPTSTRING}" opt; do
+	case "${opt}" in
+		l)
+			LIST_MODE=true; shift 1;
+		;;
+		h)
+			usage
+			exit 0
+		;;
+		\?)
+			echo "Unknown switch: $OPTARG" >&2
+			echo >&2
+			usage >&2
+			exit 1
+		;;
+	esac
+done
+
+if "$LIST_MODE"
+then
+	query=""
+	if [ "${1:-}" ]
+	then
+		query="--query=$1"
+	fi
+	(
+		cd "$LOG_ROOT"
+		find . -type d \
+			\( \
+				-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'
+	)
+else
+	SUBPROG=""
+	PROG="$1"
+
+	# TODO: and maybe su, runuser et al
+	if [ "$PROG" == "sudo" ]
+	then
+		SUBPROG="$2"
+	fi
+	LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"
+	LOG_DIR="$LOG_DIR"/"$(ls -1 "$LOG_DIR" | sort -V | tail -n1)"
+	OUT="$LOG_DIR"/stdout
+	ERR="$LOG_DIR"/stderr
+	DAT="$LOG_DIR"/dat
+	INF="$LOG_DIR"/info
+	. <(tail -n +2 "$INF")
+	cat "$OUT"
+	exit "$status"
+fi