q.sh/q

215 lines
4.7 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
2023-05-14 19:24:27 +00:00
ENV_fzf=fzf
ENV_find=find
ENV_awk=awk
ENV_q=~/.config/q.sh
2021-09-04 17:56:19 +00:00
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-08-19 22:52:14 +00:00
NEWER_FILES="$($ENV_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
$ENV_find . -type f -name 'q-*' | while read -r FN
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
$ENV_find . -type f -name 'q-*' | while read -r FN
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
echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line
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() {
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" --run "$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 "$@"
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=""
FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" $ENV_fzf \
--bind "change:reload-sync:$PREF {q} || true" \
--ansi --query "$INITIAL" \
--preview "$SELF -p {}" \
--with-nth="2..-1" \
--preview-window wrap \
--tiebreak=index | xargs -o "$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