Add interactive narrowing
This commit is contained in:
parent
ddc8b84d0d
commit
7b165c8891
5 changed files with 137 additions and 93 deletions
24
README.md
24
README.md
|
@ -3,35 +3,13 @@
|
|||
`q.sh` is a command-line tool that allows you to interact with your
|
||||
computer using natural language.
|
||||
|
||||
Example usage:
|
||||
|
||||
```sh
|
||||
export Q_SCRIPT_DIR=`pwd`
|
||||
|
||||
./q 2.0 kg in pounds
|
||||
4.41
|
||||
|
||||
./q 1 pounds in kg
|
||||
0.45
|
||||
|
||||
./q random number 1-100
|
||||
53
|
||||
```
|
||||
|
||||
## Extending q.sh
|
||||
|
||||
You can extend `q.sh` using any programming language. In order to do so, you
|
||||
need to place an executable file starting with `q-` in `Q_SCRIPT_DIR`
|
||||
(by default, ~/.config/q.sh).
|
||||
|
||||
When called without parameters, your script should return a newline-separated
|
||||
list of regular expressions that match strings it's interested in.
|
||||
|
||||
When called with an argument, this argument will always be what it expects to
|
||||
get as input. `q.sh` will take care of dispatching the calls properly.
|
||||
|
||||
To get an idea how it works, you can stury `q-weight-convert` and `q-random-number`
|
||||
that are located in this repository.
|
||||
To see an example of how to write a `q.sh` extension, take a look at `q-bookmarks`.
|
||||
|
||||
## License
|
||||
|
||||
|
|
143
q
143
q
|
@ -32,7 +32,10 @@ if [[ -z "$Q_SCRIPT_DIR" ]]; then
|
|||
Q_SCRIPT_DIR=~/.config/q.sh
|
||||
fi
|
||||
|
||||
SELF="$0"
|
||||
|
||||
INDEX_FILE=~/.q.index
|
||||
HINT_FILE=~/.q.hints
|
||||
|
||||
new_scripts_exist() {
|
||||
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
|
||||
|
@ -70,6 +73,21 @@ rebuild_index() {
|
|||
popd > /dev/null
|
||||
}
|
||||
|
||||
rebuild_hints() {
|
||||
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
pushd "$Q_SCRIPT_DIR" >/dev/null
|
||||
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
|
||||
}
|
||||
|
||||
get_scripts_for_cmd() {
|
||||
if [[ ! -f "$INDEX_FILE" ]]; then
|
||||
return
|
||||
|
@ -81,21 +99,94 @@ get_scripts_for_cmd() {
|
|||
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 "$@"
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
|
||||
if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then
|
||||
rebuild_index > "$INDEX_FILE"
|
||||
rebuild_hints > "$HINT_FILE"
|
||||
fi
|
||||
|
||||
LIST=0
|
||||
if [[ "$1" == "--list" ]] || [[ "$1" == "-l" ]]; then
|
||||
shift
|
||||
LIST=1
|
||||
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
|
||||
done
|
||||
|
||||
DRYRUN=0
|
||||
if [[ "$1" == "--dry-run" ]] || [[ "$1" == "-d" ]]; then
|
||||
shift
|
||||
DRYRUN=1
|
||||
fi
|
||||
|
||||
COMMAND="$@"
|
||||
|
||||
|
@ -103,25 +194,15 @@ if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then
|
|||
COMMAND="$(cat)"
|
||||
fi
|
||||
|
||||
if [[ ! -z "$COMMAND" ]]; then
|
||||
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
|
||||
|
||||
if [[ "$LIST" == "1" ]]; then
|
||||
echo "$SCRIPTS"
|
||||
else
|
||||
if [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
|
||||
printf "Too many scripts matched:\n$SCRIPTS\n" 1>&2
|
||||
exit 1
|
||||
elif [[ ! -z "$SCRIPTS" ]]; then
|
||||
if [[ "$DRYRUN" == "1" ]]; then
|
||||
"$Q_SCRIPT_DIR/$SCRIPTS" --dry-run "$COMMAND"
|
||||
else
|
||||
echo "$@" >> ~/.q_history
|
||||
"$Q_SCRIPT_DIR/$SCRIPTS" "$COMMAND"
|
||||
fi
|
||||
else
|
||||
printf "No scripts matched:\n$SCRIPTS\n" 1>&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
if [[ -z "$COMMAND" ]]; then
|
||||
PREF="$SELF -c"
|
||||
INITIAL=""
|
||||
FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" fzf \
|
||||
--bind "change:reload:$PREF {q} || true" \
|
||||
--ansi --query "$INITIAL" \
|
||||
--preview "$SELF -p {}" \
|
||||
--with-nth="2..-1" \
|
||||
--tiebreak=index | xargs -o "$SELF" -r
|
||||
else
|
||||
run_command_cli "$COMMAND"
|
||||
fi
|
||||
|
|
24
q-bookmarks
Executable file
24
q-bookmarks
Executable file
|
@ -0,0 +1,24 @@
|
|||
#!/bin/bash
|
||||
|
||||
|
||||
if [[ -z "$@" ]]; then
|
||||
echo "^b .*$"
|
||||
elif [[ "$1" == "--hint" ]]; then
|
||||
shift
|
||||
if [[ -f ~/.bookmarks.txt ]]; then
|
||||
cat ~/.bookmarks.txt | awk '{print $1 " b " $2}'
|
||||
fi
|
||||
elif [[ "$1" == "--complete" ]]; then
|
||||
shift
|
||||
if [[ -f ~/.bookmarks.txt ]]; then
|
||||
cat ~/.bookmarks.txt | awk '{print $1 " b " $2}'
|
||||
fi
|
||||
elif [[ "$1" == "--preview" ]]; then
|
||||
shift
|
||||
echo "$1"
|
||||
elif [[ "$1" == "--run" ]]; then
|
||||
shift
|
||||
xdg-open "$1"
|
||||
else
|
||||
echo "Unexpected arguments: $@"
|
||||
fi
|
|
@ -1,19 +0,0 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Generates random number in a specified range. Example:
|
||||
# random number 1-10
|
||||
|
||||
if [[ -z "$@" ]]; then
|
||||
echo "^random number [0-9]+-[0-9]+$"
|
||||
elif [[ "$1" == "--dry-run" ]]; then
|
||||
shift
|
||||
echo "$@"
|
||||
else
|
||||
BOUNDS="$(echo "$@" | sed -E 's/^random number ([0-9]+)-([0-9]+)$/\1 \2/g')"
|
||||
|
||||
MIN="$(echo $BOUNDS | cut -d' ' -f1)"
|
||||
MAX="$(echo $BOUNDS | cut -d' ' -f2)"
|
||||
|
||||
awk -v min=5 -v max=10 "BEGIN{srand(); print int($MIN+rand()*($MAX-$MIN+1))}"
|
||||
fi
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Converts weights between pounds and kilograms. Examples:
|
||||
# 10 pounds in kg
|
||||
# 2.5 kg in pounds
|
||||
|
||||
if [[ -z "$@" ]]; then
|
||||
echo "^[0-9]+(\.[0-9]+)? kg in pounds$"
|
||||
echo "^[0-9]+(\.[0-9]+)? pounds in kg$"
|
||||
elif [[ "$1" == "--dry-run" ]]; then
|
||||
shift
|
||||
echo "$@"
|
||||
else
|
||||
NUM="$(echo "$@" | sed -E 's/([0-9]+) .*/\1/g')"
|
||||
if echo "$@" | grep -E "^[0-9]+(\.[0-9]+)? pounds in kg$" > /dev/null; then
|
||||
awk "BEGIN {printf \"%.2f\n\", $NUM/2.20462}"
|
||||
else
|
||||
awk "BEGIN {printf \"%.2f\n\", $NUM*2.20462}"
|
||||
fi
|
||||
fi
|
Loading…
Reference in a new issue