Update for latest fzf and add a few additional scripts

This commit is contained in:
Konstantin Nazarov 2023-05-14 18:24:43 +01:00
parent f9731dd7ca
commit fe69664195
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
8 changed files with 285 additions and 204 deletions

36
flake.nix Normal file
View file

@ -0,0 +1,36 @@
{
description = "Nix q.sh dev environment";
# Flake inputs
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs";
};
# Flake outputs
outputs = { self, nixpkgs }:
let
# Systems supported
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
# Helper to provide system-specific attributes
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit system; };
});
in
{
# Development environment output
devShells = forAllSystems ({ pkgs }: {
default = pkgs.mkShell {
# The Nix packages provided in the environment
packages = with pkgs; [
fzf
];
};
});
};
}

250
q
View file

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# #
# Distributed under the terms of the BSD License # Distributed under the terms of the BSD License
# #
@ -29,181 +29,181 @@
set -e set -e
if [[ -z "$Q_SCRIPT_DIR" ]]; then if [[ -z "$Q_SCRIPT_DIR" ]]; then
Q_SCRIPT_DIR=~/.config/q.sh Q_SCRIPT_DIR=~/.config/q.sh
fi fi
SELF="$0" SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")")
INDEX_FILE=~/.q.index INDEX_FILE=~/.q.index
HINT_FILE=~/.q.hints HINT_FILE=~/.q.hints
new_scripts_exist() { new_scripts_exist() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return 0 return 0
fi fi
if [[ ! -s "$INDEX_FILE" ]]; then if [[ ! -s "$INDEX_FILE" ]]; then
return 0 return 0
fi fi
NEWER_FILES="$(find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")" NEWER_FILES="$(find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")"
if [[ -z "$NEWER_FILES" ]]; then if [[ -z "$NEWER_FILES" ]]; then
return 1 return 1
fi fi
return 0 return 0
} }
rebuild_index() { rebuild_index() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return return
fi fi
pushd "$Q_SCRIPT_DIR" >/dev/null pushd "$Q_SCRIPT_DIR" >/dev/null
find . -type f -name 'q-*' | while read -r FN find . -type f -name 'q-*' | while read -r FN
do do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')" FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" | while read -r line "$Q_SCRIPT_DIR/$FILENAME" | while read -r line
do do
echo "/$line/ {print \"$FILENAME\"}" echo "/$line/ {print \"$FILENAME\"}"
done done
done done
popd > /dev/null popd > /dev/null
} }
rebuild_hints() { rebuild_hints() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return return
fi fi
pushd "$Q_SCRIPT_DIR" >/dev/null pushd "$Q_SCRIPT_DIR" >/dev/null
find . -type f -name 'q-*' | while read -r FN find . -type f -name 'q-*' | while read -r FN
do do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')" FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" --hint "$Q_SCRIPT_DIR/$FILENAME" --hint
done done
popd > /dev/null popd > /dev/null
} }
get_scripts_for_cmd() { get_scripts_for_cmd() {
if [[ ! -f "$INDEX_FILE" ]]; then if [[ ! -f "$INDEX_FILE" ]]; then
return return
fi fi
echo "$1" | awk -f "$INDEX_FILE" | while read -r line echo "$1" | awk -f "$INDEX_FILE" | while read -r line
do do
echo "$line" echo "$line"
done done
} }
complete_command() { complete_command() {
COMMAND="$@" COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")" SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
cat "$HINT_FILE" cat "$HINT_FILE"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --complete "$@" "$Q_SCRIPT_DIR/$SCRIPTS" --complete "$@"
} }
run_command() { run_command() {
COMMAND="$@" COMMAND="$@"
FIRST=${COMMAND%%" "*} FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "} REST=${COMMAND#*" "}
SCRIPTS="$(get_scripts_for_cmd "$REST")" SCRIPTS="$(get_scripts_for_cmd "$REST")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --run "$FIRST" "$REST" "$Q_SCRIPT_DIR/$SCRIPTS" --run "$FIRST" "$REST"
} }
run_command_cli() { run_command_cli() {
COMMAND="$@" COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")" SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --cli "$COMMAND" "$Q_SCRIPT_DIR/$SCRIPTS" --cli "$COMMAND"
} }
preview_command() { preview_command() {
COMMAND="$@" COMMAND="$@"
FIRST=${COMMAND%%" "*} FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "} REST=${COMMAND#*" "}
SCRIPTS="$(get_scripts_for_cmd "$REST")" SCRIPTS="$(get_scripts_for_cmd "$REST")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --preview "$FIRST" "$REST" "$Q_SCRIPT_DIR/$SCRIPTS" --preview "$FIRST" "$REST"
} }
if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then
rebuild_index > "$INDEX_FILE" rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE" rebuild_hints > "$HINT_FILE"
fi fi
while (( "$#" )); do while (( "$#" )); do
case "$1" in case "$1" in
-c|--complete) -c|--complete)
shift shift
complete_command "$@" complete_command "$@"
exit 0 exit 0
;; ;;
-r|--run) -r|--run)
shift shift
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
exit 0 exit 0
fi fi
run_command "$@" run_command "$@"
exit 0 exit 0
;; ;;
-p|--preview) -p|--preview)
shift shift
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
exit 0 exit 0
fi fi
preview_command "$@" preview_command "$@"
exit 0 exit 0
;; ;;
-u|--update) -u|--update)
shift shift
rebuild_index > "$INDEX_FILE" rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE" rebuild_hints > "$HINT_FILE"
exit 0 exit 0
;; ;;
*) *)
break break
;; ;;
esac esac
done done
COMMAND="$@" COMMAND="$@"
if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then
COMMAND="$(cat)" COMMAND="$(cat)"
fi fi
if [[ -z "$COMMAND" ]]; then if [[ -z "$COMMAND" ]]; then
PREF="$SELF -c" PREF="$SELF -c"
INITIAL="" INITIAL=""
FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" fzf \ FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" fzf \
--bind "change:reload:$PREF {q} || true" \ --bind "change:reload-sync:$PREF {q} || true" \
--ansi --query "$INITIAL" \ --ansi --query "$INITIAL" \
--preview "$SELF -p {}" \ --preview "$SELF -p {}" \
--with-nth="2..-1" \ --with-nth="2..-1" \
--preview-window wrap \ --preview-window wrap \
--tiebreak=index | xargs -o "$SELF" -r --tiebreak=index | xargs -o "$SELF" -r
else else
run_command_cli "$COMMAND" run_command_cli "$COMMAND"
fi fi

View file

@ -1,24 +1,24 @@
#!/bin/bash #!/bin/sh
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
echo "^b .*$" echo "^b .*$"
elif [[ "$1" == "--hint" ]]; then elif [[ "$1" == "--hint" ]]; then
shift shift
if [[ -f ~/.bookmarks.txt ]]; then if [[ -f ~/.bookmarks.txt ]]; then
cat ~/.bookmarks.txt | awk '{print $1 " b " $2}' cat ~/.bookmarks.txt | awk '{print $1 " b " $2}'
fi fi
elif [[ "$1" == "--complete" ]]; then elif [[ "$1" == "--complete" ]]; then
shift shift
if [[ -f ~/.bookmarks.txt ]]; then if [[ -f ~/.bookmarks.txt ]]; then
cat ~/.bookmarks.txt | awk '{print $1 " b " $2}' cat ~/.bookmarks.txt | awk '{print $1 " b " $2}'
fi fi
elif [[ "$1" == "--preview" ]]; then elif [[ "$1" == "--preview" ]]; then
shift shift
echo "$1" echo "$1"
elif [[ "$1" == "--run" ]]; then elif [[ "$1" == "--run" ]]; then
shift shift
xdg-open "$1" xdg-open "$1"
else else
echo "Unexpected arguments: $@" echo "Unexpected arguments: $@"
fi fi

19
q-browse Executable file
View file

@ -0,0 +1,19 @@
#!/bin/sh
if [[ -z "$@" ]]; then
echo "^f .*$"
elif [[ "$1" == "--hint" ]]; then
shift
elif [[ "$1" == "--complete" ]]; then
shift
echo "0 $@"
elif [[ "$1" == "--preview" ]]; then
shift
URL="$(echo "$@" | cut -d ' ' -f3-)"
echo "Will open '$URL' in firefox"
elif [[ "$1" == "--run" ]]; then
shift
URL="$(echo "$@" | cut -d ' ' -f3-)"
(nohup xdg-open "$URL" 2>/dev/null &)
sleep 0.01
fi

42
q-calc
View file

@ -1,33 +1,33 @@
#!/bin/bash #!/bin/sh
calc() { calc() {
res="$(echo "$1" | bc 2>/dev/null)" res="$(echo "$1" | bc 2>/dev/null)"
if [ "$res" == "" ]; then if [ "$res" == "" ]; then
res="-" res="-"
fi fi
echo "$res" echo "$res"
} }
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
echo "^= .*$" echo "^= .*$"
elif [[ "$1" == "--hint" ]]; then elif [[ "$1" == "--hint" ]]; then
shift shift
echo "- = " echo "- = "
elif [[ "$1" == "--complete" ]]; then elif [[ "$1" == "--complete" ]]; then
shift shift
EXPR="$(echo "$@" | cut -d ' ' -f2-)" EXPR="$(echo "$@" | cut -d ' ' -f2-)"
RES="$(calc "$EXPR")" RES="$(calc "$EXPR")"
echo "$RES = $EXPR -> $(calc "$EXPR")" echo "$RES = $EXPR -> $(calc "$EXPR")"
elif [[ "$1" == "--preview" ]]; then elif [[ "$1" == "--preview" ]]; then
shift shift
echo "$1" echo "$1"
elif [[ "$1" == "--run" ]]; then elif [[ "$1" == "--run" ]]; then
shift shift
echo "$1" echo "$1"
echo "$1" | (nohup wl-copy -n 2>/dev/null &) echo "$1" | (nohup wl-copy -n 2>/dev/null &)
elif [[ "$1" == "--cli" ]]; then elif [[ "$1" == "--cli" ]]; then
shift shift
calc "$(echo "$@" | cut -d ' ' -f2-)" calc "$(echo "$@" | cut -d ' ' -f2-)"
else else
echo "Unexpected arguments: $@" echo "Unexpected arguments: $@"
fi fi

View file

@ -1,26 +1,26 @@
#!/bin/bash #!/bin/sh
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
echo "^c .*$" echo "^c .*$"
elif [[ "$1" == "--hint" ]]; then elif [[ "$1" == "--hint" ]]; then
shift shift
if [[ -f ~/.clipboard.txt ]]; then if [[ -f ~/.clipboard.txt ]]; then
cat ~/.clipboard.txt | tac cat ~/.clipboard.txt | tac
fi fi
elif [[ "$1" == "--complete" ]]; then elif [[ "$1" == "--complete" ]]; then
shift shift
if [[ -f ~/.clipboard.txt ]]; then if [[ -f ~/.clipboard.txt ]]; then
cat ~/.clipboard.txt | tac cat ~/.clipboard.txt | tac
fi fi
elif [[ "$1" == "--preview" ]]; then elif [[ "$1" == "--preview" ]]; then
shift shift
echo "$1" | base64 -d echo "$1" | base64 -d
elif [[ "$1" == "--run" ]]; then elif [[ "$1" == "--run" ]]; then
shift shift
TEXT="$(echo "$1" | base64 -d)" TEXT="$(echo "$1" | base64 -d)"
echo "$TEXT" echo "$TEXT"
echo -n "$TEXT" | (nohup wl-copy -n 2>/dev/null &) echo -n "$TEXT" | (nohup wl-copy -n 2>/dev/null &)
else else
echo "Unexpected arguments: $@" echo "Unexpected arguments: $@"
fi fi

52
q-pass
View file

@ -1,34 +1,34 @@
#!/bin/bash #!/bin/sh
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
# Return a regular expression that should trigger # Return a regular expression that should trigger
# this subcommand. q.sh will then pass control to # this subcommand. q.sh will then pass control to
# this script whenever the input line matches. # this script whenever the input line matches.
echo "^p .*$" echo "^p .*$"
elif [[ "$1" == "--hint" ]]; then elif [[ "$1" == "--hint" ]]; then
# This subcommand has no hints # This subcommand has no hints
shift shift
elif [[ "$1" == "--complete" ]]; then elif [[ "$1" == "--complete" ]]; then
shift shift
# Return a list of all accounts for which we store # Return a list of all accounts for which we store
# passwords. This will be used to provide a list of # passwords. This will be used to provide a list of
# completions. # completions.
cd ~/.password-store cd ~/.password-store
find . -name "*.gpg" | sed -E 's/\.\/(.*)\.gpg/\1 p \1/' find . -name "*.gpg" | sed -E 's/\.\/(.*)\.gpg/\1 p \1/'
elif [[ "$1" == "--preview" ]]; then elif [[ "$1" == "--preview" ]]; then
shift shift
# This information will be displayed in the preview # This information will be displayed in the preview
# pane on the right. # pane on the right.
echo "Will get password for $1" echo "Will get password for $1"
elif [[ "$1" == "--run" ]]; then elif [[ "$1" == "--run" ]]; then
shift shift
# This will be triggered when enter is pressed on # This will be triggered when enter is pressed on
# one of the options. We now decrypt the password # one of the options. We now decrypt the password
# and place it to the clipboard. # and place it to the clipboard.
NAME="$1" NAME="$1"
pass "$NAME" | (nohup wl-copy -n 2>/dev/null &) pass "$NAME" | (nohup wl-copy -n 2>/dev/null &)
# A short sleep is needed to wait for child process # A short sleep is needed to wait for child process
# to spawn before exiting. # to spawn before exiting.
sleep 0.01 sleep 0.01
fi fi

26
q-run Executable file
View file

@ -0,0 +1,26 @@
#!/bin/sh
export PATH=~/.prefix/bin:~/.local/bin:$PATH
find_all_executables() {
{
IFS=:; for d in $PATH; do for f in $d/*; do [ -f $f ] && [ -x $f ] && echo ${f##*/}; done; done;
} | sort
#flatpak --columns=application --app list | tail -n +1 | awk '{print "flatpak run " $0}';
}
if [[ -z "$@" ]]; then
echo "^run [a-zA-Z0-9_-]*$"
elif [[ "$1" == "--hint" ]]; then
find_all_executables | awk '{print $1 " run " $1}'
elif [[ "$1" == "--complete" ]]; then
shift
find_all_executables | awk '{print $1 " run " $1}'
elif [[ "$1" == "--preview" ]]; then
shift
echo "Will run $1"
elif [[ "$1" == "--run" ]]; then
shift
(nohup "$1" 2>/dev/null &)
sleep 0.01
fi