Browse Source

refactor subprog method and support in find query

Daniel Sheffield 3 months ago
parent
commit
641b1b1275
3 changed files with 29 additions and 17 deletions
  1. 3 5
      capture.sh
  2. 12 0
      helpers/common.sh
  3. 14 12
      recall.sh

+ 3 - 5
capture.sh

@@ -5,14 +5,12 @@ then
 	echo capture
 	echo capture
 	exit 0
 	exit 0
 fi
 fi
+
+. "${BASH_SOURCE[0]%/*}"/helpers/common.sh
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 
 
-SUBPROG=""
 PROG="$1"
 PROG="$1"
-if [ "$PROG" == "sudo" ]
-then
-	SUBPROG="$2"
-fi
+SUBPROG="$(get_subprog "$@")"
 
 
 TIMESTAMP=$(date +%Y%m%d_%H%M%S_%N)
 TIMESTAMP=$(date +%Y%m%d_%H%M%S_%N)
 LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"/"$TIMESTAMP"
 LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"/"$TIMESTAMP"

+ 12 - 0
helpers/common.sh

@@ -0,0 +1,12 @@
+get_subprog () {
+	# NOTE: for progams that wrap calls to others
+	#       such as sudo, and time, the actual
+	#       executable is not the first positional arg
+	#
+	# TODO: support su, runuser, runas et al
+	# TODO: support time
+	if [ "$PROG" = "sudo" ]
+	then
+		echo "$2" # naive - what if sudo is invoked with options?
+	fi
+}

+ 14 - 12
recall.sh

@@ -5,6 +5,9 @@ then
 	echo recall
 	echo recall
 	exit 0
 	exit 0
 fi
 fi
+
+. "${BASH_SOURCE[0]%/*}"/helpers/common.sh
+
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 LOG_ROOT="$HOME"/.local/var/log/shell # source this
 
 
 EXIT_SUCCESS=false
 EXIT_SUCCESS=false
@@ -114,8 +117,8 @@ fzf_rw0_inplace_preview () {
 	# shellcheck disable=SC2016
 	# shellcheck disable=SC2016
 	fzf \
 	fzf \
 		--read0 --print0 \
 		--read0 --print0 \
-		-m --no-sort \
-		"$@" \
+		-m \
+		--no-sort \
 		-d / \
 		-d / \
 		--nth 1,2,3 \
 		--nth 1,2,3 \
 		--preview-window="~2" \
 		--preview-window="~2" \
@@ -159,12 +162,16 @@ awk_r0_add_prefix () {
 
 
 if "$LIST_MODE"
 if "$LIST_MODE"
 then
 then
-	fzf_query=()
 	find_name=()
 	find_name=()
 	if [ "${1:-}" ]
 	if [ "${1:-}" ]
 	then
 	then
-		fzf_query=( "--query=$1" )
-		find_name=( -wholename "**/$1/**" )
+		slugs=( "$1" )
+		if [ "${2:-}" ]
+		then
+			slugs+=( "$2" )
+		fi
+		query="$(bash -ac 'IFS=/; echo "$*"' "recall-bash" "${slugs[@]}")"
+		find_name=( -wholename "**/$query/**" )
 	fi
 	fi
 	(
 	(
 		cd "$LOG_ROOT"
 		cd "$LOG_ROOT"
@@ -178,20 +185,15 @@ then
 			-print0 \
 			-print0 \
 		| sort -zrV \
 		| sort -zrV \
 		| head -zn "${NUM:--0}" \
 		| head -zn "${NUM:--0}" \
-		| fzf_rw0_inplace_preview "${fzf_query[@]}" \
+		| fzf_rw0_inplace_preview "2" \
 		| awk_rw0_strip_first_path_part \
 		| awk_rw0_strip_first_path_part \
 		| awk_r0_add_prefix "$ORS" "$LOG_ROOT" \
 		| awk_r0_add_prefix "$ORS" "$LOG_ROOT" \
 		&& :
 		&& :
 	)
 	)
 else
 else
-	SUBPROG=""
 	PROG="$1"
 	PROG="$1"
+	SUBPROG="$(get_subprog "$@")"
 
 
-	# TODO: and maybe su, runuser et al
-	if [ "$PROG" == "sudo" ]
-	then
-		SUBPROG="$2"
-	fi
 	LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"
 	LOG_DIR="$LOG_ROOT"/"$PROG"/"$SUBPROG"
 	LOG_DIR="$LOG_DIR"/"$(cd "$LOG_DIR" && find . -mindepth 1 -maxdepth 1 -print0 | sort -zV | tail -zn1 | xargs -r -0 echo)"
 	LOG_DIR="$LOG_DIR"/"$(cd "$LOG_DIR" && find . -mindepth 1 -maxdepth 1 -print0 | sort -zV | tail -zn1 | xargs -r -0 echo)"
 	OUT="$LOG_DIR"/stdout
 	OUT="$LOG_DIR"/stdout