q.sh/q-calc

38 lines
670 B
Text
Raw 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() {
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"
2024-11-03 02:17:28 +00:00
echo "$1" | (setsid -f $ENV_wl_copy)
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--cli" ]]; then
shift
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