Update to work with latest NixOS

This commit is contained in:
Konstantin Nazarov 2024-08-19 23:52:14 +01:00
parent 4db7f63b04
commit e1b69b417b
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 154 additions and 148 deletions

View file

@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1684076341,
"narHash": "sha256-60pNoKP2opEnGoPGrVo43Vt433El4+BcSl0TS36qJgQ=",
"lastModified": 1724104367,
"narHash": "sha256-ZYT8DwJKW6UTUm1F0kSluOGNrpmCrwEWruhtmWDhiNM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1feac2b61756a5a4392a6cc5f5b510325312437d",
"rev": "dfd48cea67e78574522201471bccca03383e9987",
"type": "github"
},
"original": {

View file

@ -22,7 +22,7 @@
pkgs = import nixpkgs { inherit system; };
});
make_package = pkgs: pkgs.stdenv.mkDerivation {
make_package = pkgs: pkgs.stdenv.mkDerivation rec {
name = "q-sh";
src = self;
installPhase = ''
@ -32,28 +32,34 @@
echo "OUT " $out
'';
dontStrip = true;
nativeBuildInputs = [pkgs.makeWrapper];
wrapperPath = pkgs.lib.makeBinPath ( with pkgs; [
fzf
findutils
gnused
gawk
bc
pass
coreutils
wl-clipboard
xdg-utils
]
);
postFixup = ''
q-fixup() {
TOOL="$1"
PATH="$2"
${pkgs.findutils}/bin/find $out -type f -name 'q*' -exec ${pkgs.gnused}/bin/sed -i "s#ENV_$TOOL=.*#ENV_$TOOL=$PATH#g" {} +
}
q-fixup "fzf" "${pkgs.fzf}/bin/fzf"
q-fixup "find" "${pkgs.findutils}/bin/find"
q-fixup "sed" "${pkgs.gnused}/bin/sed"
q-fixup "awk" "${pkgs.gawk}/bin/awk"
q-fixup "bc" "${pkgs.bc}/bin/bc"
q-fixup "pass" "${pkgs.pass}/bin/pass"
q-fixup "cut" "${pkgs.coreutils}/bin/cut"
q-fixup "base64" "${pkgs.coreutils}/bin/base64"
q-fixup "cat" "${pkgs.coreutils}/bin/cat"
q-fixup "tac" "${pkgs.coreutils}/bin/tac"
q-fixup "sort" "${pkgs.coreutils}/bin/sort"
q-fixup "uniq" "${pkgs.coreutils}/bin/uniq"
q-fixup "wl_copy" "${pkgs.wl-clipboard}/bin/wl-copy"
q-fixup "xdg_open" "${pkgs.xdg-utils}/bin/xdg-open"
q-fixup "q" "$out/bin"
'';
substituteInPlace $out/bin/q \
--replace 'ENV_q="''${0##*/}"' "ENV_q=$out/bin/q"
# Ensure all dependencies are in PATH
for fn in $out/bin/q*; do
echo "wrapping $fn"
wrapProgram $fn \
--prefix PATH : "${wrapperPath}"
done
'';
};
in
{

246
q
View file

@ -36,179 +36,179 @@ ENV_q=~/.config/q.sh
SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")")
if [[ -z "$Q_SCRIPT_DIR" ]]; then
Q_SCRIPT_DIR="$ENV_q"
Q_SCRIPT_DIR="$(dirname -- $SELF)"
fi
INDEX_FILE=~/.q.index
HINT_FILE=~/.q.hints
new_scripts_exist() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return 0
fi
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return 0
fi
if [[ ! -s "$INDEX_FILE" ]]; then
return 0
fi
if [[ ! -s "$INDEX_FILE" ]]; then
return 0
fi
NEWER_FILES="$($ENV_find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")"
NEWER_FILES="$($ENV_find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")"
if [[ -z "$NEWER_FILES" ]]; then
return 1
fi
if [[ -z "$NEWER_FILES" ]]; then
return 1
fi
return 0
return 0
}
rebuild_index() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return
fi
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')"
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
"$Q_SCRIPT_DIR/$FILENAME" | while read -r line
do
echo "/$line/ {print \"$FILENAME\"}"
done
done
popd > /dev/null
}
rebuild_hints() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return
fi
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')"
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
"$Q_SCRIPT_DIR/$FILENAME" --hint
done
popd > /dev/null
}
get_scripts_for_cmd() {
if [[ ! -f "$INDEX_FILE" ]]; then
return
fi
if [[ ! -f "$INDEX_FILE" ]]; then
return
fi
echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line
do
echo "$line"
done
echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line
do
echo "$line"
done
}
complete_command() {
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 "$@"
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 "$@"
}
run_command() {
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"
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"
}
run_command_cli() {
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"
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"
}
preview_command() {
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"
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"
}
if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then
rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE"
rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE"
fi
while (( "$#" )); do
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
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
done
COMMAND="$@"
if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then
COMMAND="$(cat)"
COMMAND="$(cat)"
fi
if [[ -z "$COMMAND" ]]; then
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
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
else
run_command_cli "$COMMAND"
run_command_cli "$COMMAND"
fi