q.sh/q

222 lines
5 KiB
Text
Raw Normal View History

#!/bin/sh
2021-09-04 17:56:19 +00:00
#
# Distributed under the terms of the BSD License
#
# Copyright (c) 2021, Konstantin Nazarov <mail@knazarov.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -e
SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")")
2021-10-10 20:26:06 +00:00
2023-05-14 19:24:27 +00:00
if [[ -z "$Q_SCRIPT_DIR" ]]; then
2024-08-19 22:52:14 +00:00
Q_SCRIPT_DIR="$(dirname -- $SELF)"
2023-05-14 19:24:27 +00:00
fi
2021-09-04 17:56:19 +00:00
INDEX_FILE=~/.q.index
2021-10-10 20:26:06 +00:00
HINT_FILE=~/.q.hints
2021-09-04 17:56:19 +00:00
new_scripts_exist() {
2024-08-19 22:52:14 +00:00
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return 0
fi
2021-09-04 17:56:19 +00:00
2024-08-19 22:52:14 +00:00
if [[ ! -s "$INDEX_FILE" ]]; then
return 0
fi
2021-09-04 17:56:19 +00:00
2024-11-03 23:36:22 +00:00
NEWER_FILES="$(find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")"
2021-09-04 17:56:19 +00:00
2024-08-19 22:52:14 +00:00
if [[ -z "$NEWER_FILES" ]]; then
return 1
fi
2021-09-04 17:56:19 +00:00
2024-08-19 22:52:14 +00:00
return 0
2021-09-04 17:56:19 +00:00
}
rebuild_index() {
2024-08-19 22:52:14 +00:00
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return
fi
pushd "$Q_SCRIPT_DIR" >/dev/null
2024-11-03 23:36:22 +00:00
find . -type f -name 'q-*' | while read -r FN
2024-08-19 22:52:14 +00:00
do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" | while read -r line
do
echo "/$line/ {print \"$FILENAME\"}"
done
done
popd > /dev/null
2021-09-04 17:56:19 +00:00
}
2021-10-10 20:26:06 +00:00
rebuild_hints() {
2024-08-19 22:52:14 +00:00
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return
fi
pushd "$Q_SCRIPT_DIR" >/dev/null
2024-11-03 23:36:22 +00:00
find . -type f -name 'q-*' | while read -r FN
2024-08-19 22:52:14 +00:00
do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" --hint
done
popd > /dev/null
2021-10-10 20:26:06 +00:00
}
2021-09-04 17:56:19 +00:00
get_scripts_for_cmd() {
2024-08-19 22:52:14 +00:00
if [[ ! -f "$INDEX_FILE" ]]; then
return
fi
2024-11-03 23:36:22 +00:00
echo "$1" | awk -f "$INDEX_FILE" | while read -r line
2024-08-19 22:52:14 +00:00
do
echo "$line"
done
2021-09-04 17:56:19 +00:00
}
2021-10-10 20:26:06 +00:00
complete_command() {
2024-08-19 22:52:14 +00:00
COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
cat "$HINT_FILE"
exit 0
fi
"$Q_SCRIPT_DIR/$SCRIPTS" --complete "$@"
2021-10-10 20:26:06 +00:00
}
run_command() {
INPUT="$1"
COMMAND="$2"
2024-08-19 22:52:14 +00:00
FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "}
2024-11-20 07:45:08 +00:00
if [ ! -z "$REST" ]; then
# This branch is taken in most cases when an existing
# item is selected in fzf
SCRIPTS="$(get_scripts_for_cmd "$REST")"
else
# In case when zero items have matched, but a command
# has support for a default action to take (e.g. creating
# a new entry) - use the actual user's input
SCRIPTS="$(get_scripts_for_cmd "$INPUT")"
fi
2024-08-19 22:52:14 +00:00
if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS"
exit 0
fi
"$Q_SCRIPT_DIR/$SCRIPTS" --run "$INPUT" "$FIRST" "$REST"
2021-10-10 20:26:06 +00:00
}
run_command_cli() {
2024-08-19 22:52:14 +00:00
COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS"
exit 0
fi
"$Q_SCRIPT_DIR/$SCRIPTS" --cli "$COMMAND"
2021-10-10 20:26:06 +00:00
}
preview_command() {
2024-08-19 22:52:14 +00:00
COMMAND="$@"
FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "}
SCRIPTS="$(get_scripts_for_cmd "$REST")"
if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS"
exit 0
fi
"$Q_SCRIPT_DIR/$SCRIPTS" --preview "$FIRST" "$REST"
2021-10-10 20:26:06 +00:00
}
2021-09-04 17:56:19 +00:00
if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then
2024-08-19 22:52:14 +00:00
rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE"
2021-09-04 17:56:19 +00:00
fi
2021-10-10 20:26:06 +00:00
while (( "$#" )); do
2024-08-19 22:52:14 +00:00
case "$1" in
-c|--complete)
shift
complete_command "$@"
exit 0
;;
-r|--run)
shift
if [[ -z "$@" ]]; then
exit 0
fi
run_command "$1" "$2"
2024-08-19 22:52:14 +00:00
exit 0
;;
-p|--preview)
shift
if [[ -z "$@" ]]; then
exit 0
fi
preview_command "$@"
exit 0
;;
-u|--update)
shift
rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE"
exit 0
;;
*)
break
;;
esac
2021-10-10 20:26:06 +00:00
done
2021-09-04 17:56:19 +00:00
COMMAND="$@"
if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then
2024-08-19 22:52:14 +00:00
COMMAND="$(cat)"
2021-09-04 17:56:19 +00:00
fi
2021-10-10 20:26:06 +00:00
if [[ -z "$COMMAND" ]]; then
2024-08-19 22:52:14 +00:00
PREF="$SELF -c"
INITIAL=""
2024-11-03 23:36:22 +00:00
FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" fzf \
2024-08-19 22:52:14 +00:00
--bind "change:reload-sync:$PREF {q} || true" \
--ansi --query "$INITIAL" \
--preview "$SELF -p {}" \
--with-nth="2..-1" \
--preview-window wrap \
--tiebreak=index \
--print-query \
--bind 'alt-enter:print-query' | xargs -o -d "\n" -n 2 "$SELF" -r
2021-10-10 20:26:06 +00:00
else
2024-08-19 22:52:14 +00:00
run_command_cli "$COMMAND"
2021-09-04 17:56:19 +00:00
fi