q.sh/q-calc

34 lines
612 B
Text
Raw Normal View History

#!/bin/sh
2021-10-10 21:27:55 +00:00
calc() {
2024-11-03 23:36:22 +00:00
res="$(echo "$1" | 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 23:36:22 +00:00
echo "$1" | (setsid -f wl-copy)
2021-10-10 21:27:55 +00:00
elif [[ "$1" == "--cli" ]]; then
shift
2024-11-03 23:36:22 +00:00
calc "$(echo "$@" | cut -d ' ' -f2-)"
2021-10-10 21:27:55 +00:00
else
echo "Unexpected arguments: $@"
2021-10-10 21:27:55 +00:00
fi