72 lines
1.6 KiB
Bash
Executable File
72 lines
1.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# https://github.com/onespaceman/menu-calc
|
|
# https://github.com/BarbUk/menu-qalc
|
|
# https://github.com/ClemaX/menu-qalc-wayland
|
|
#
|
|
# Calculator for use with rofi
|
|
# Copying to the clipboard requires wl-copy
|
|
|
|
usage() {
|
|
echo "$(tput bold)menu-calc$(tput sgr0)"
|
|
echo "A calculator for Rofi"
|
|
echo
|
|
echo "$(tput bold)Usage:$(tput sgr0)"
|
|
echo " = 4+2"
|
|
echo " = (4+2)/(4+3)"
|
|
echo " = 4^2"
|
|
echo " = sqrt(4)"
|
|
echo " = c(2)"
|
|
echo
|
|
echo "$(tput bold)Passing arguments to Rofi:$(tput sgr0)"
|
|
echo "Any parameters after ' -- ' will be passed to Rofi."
|
|
echo
|
|
echo " = -- <Rofi args>"
|
|
echo
|
|
echo "The answer can be copied to the clipboard and used for further calculations inside (or outside) Rofi."
|
|
echo
|
|
echo "If launched outside of Rofi the expression may need quotation marks."
|
|
exit
|
|
}
|
|
|
|
process_query() { # query
|
|
local query="$1"
|
|
|
|
qalc +u8 -color=never -terse <<<"$query" |
|
|
awk '!/^>/ && !/^$/ {gsub(/^[ \t]+|[ \t]+$/, "", $0); print}'
|
|
}
|
|
|
|
# Process CLI parameters
|
|
for var in "$@"; do
|
|
case $var in
|
|
-h | --help) usage ;;
|
|
--) break ;;
|
|
esac
|
|
done
|
|
|
|
# Path to menu application
|
|
if [[ -n $(command -v rofi) ]]; then
|
|
menu="$(command -v rofi)"
|
|
menu="$menu -dmenu -lines 3"
|
|
else
|
|
>&2 echo "Rofi not found"
|
|
exit
|
|
fi
|
|
|
|
# Determine args to pass to rofi
|
|
while [[ $# -gt 0 && $1 != "--" ]]; do
|
|
shift
|
|
done
|
|
[[ $1 == "--" ]] && shift
|
|
|
|
while true; do
|
|
action=$(echo -e "Copy\nClear\nClose" | ${menu} "$@" -p "= $answer")
|
|
|
|
case "$action" in
|
|
"Clear") answer= ;;
|
|
"Copy") wl-copy -- "$answer" ;;
|
|
"Close" | "") exit ;;
|
|
*) answer=$(process_query "$answer $action") ;;
|
|
esac
|
|
done
|