q.sh/q-calc

38 lines
746 B
Text
Raw Permalink Normal View History

#!/bin/sh
2021-10-10 21:27:55 +00:00
2023-05-14 19:24:27 +00:00
ENV_bc=bc
ENV_wl_copy=wl-copy
ENV_cut=cut
2021-10-10 21:27:55 +00:00
calc() {
2023-05-14 19:24:27 +00:00
res="$(echo "$1" | $ENV_bc 2>/dev/null)"
if [ "$res" == "" ]; then
res="-"
fi
echo "$res"
2021-10-10 21:27:55 +00:00
}
if [[ -z "$@" ]]; then
echo "^= .*$"
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--hint" ]]; then
shift
echo "- = "
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--complete" ]]; then
shift
EXPR="$(echo "$@" | cut -d ' ' -f2-)"
RES="$(calc "$EXPR")"
echo "$RES = $EXPR -> $(calc "$EXPR")"
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--preview" ]]; then
shift
echo "$1"
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--run" ]]; then
shift
echo "$1"
2023-05-14 19:24:27 +00:00
echo "$1" | (nohup $ENV_wl_copy -n 2>/dev/null &)
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--cli" ]]; then
shift
2023-05-14 19:24:27 +00:00
calc "$(echo "$@" | $ENV_cut -d ' ' -f2-)"
2021-10-10 21:27:55 +00:00
else
echo "Unexpected arguments: $@"
2021-10-10 21:27:55 +00:00
fi