forked from davatorium/rofi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rofi-calc.sh
executable file
·50 lines (47 loc) · 1.98 KB
/
rofi-calc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
# A rofi mode script that allows calculations to be done
# by Adnan Shameem (adnan360). License: MIT (Expat)
#
# Usage:
# - Download this into a dir
# - Make sure it's executable: chmod +x rofi-calc.sh
# - Make sure you have at least one of these:
# - bc
# - python (v2 or v3)
# - bcalc (curl -LO https://github.com/Phate6660/bcalc/blob/master/bcalc && chmod +x bcalc)
# - Run: rofi -modi "calc:./rofi-calc.sh" -show calc
# or, use this to have both run and calc options: rofi -combi-modi "calc:./rofi-calc.sh",run -show combi
# - Enter expressions like "2+2", "15-5", "10*2", "3^2" to get results
# Handle input
# Important! It has to be before anything else is done in the script.
# Otherwise it will keep reopening the menu indefinitely!
if [ ! -z "$@" ]; then
# To strip spaces
input=$(echo "$@" | sed 's/[[:space:]]*//g')
# If input has numbers and symbols, process it
if grep -q '+\|-[[:digit:]]\|\*\|\/\|\^' <<<"$input"; then
if [ ! -z "$(command -v bc)" ]; then # has bc package on system
# If there is an equal sign (=) at the end, get rid of it.
input=$(echo "$input" | sed 's/\=//g')
# Show the result in a notification.
echo "$input = "$( echo $input | bc )
elif [ ! -z "$(command -v python)" ]; then # has python package on system
# If there is an equal sign (=) at the end, get rid of it.
# Replace "^" with "**" so that it can calculate powers.
input=$(echo "$input" | sed 's/\=//g;s/\^/**/g')
# Show the result in a notification.
# The "float(1)*" part is so that Python 2 returns decimal places.
echo "$input = "$( python -c "print( float(1)*$input )" )
elif [ -f "$(dirname $0)/bcalc" ]; then # has bcalc
# If there is an equal sign (=) at the end, get rid of it.
input=$(echo "$input" | sed 's/\=//g')
echo "$input = "$($(dirname $0)/bcalc "$input")
else
echo 'Error: No calculation utility found.'
echo 'Please install bc, python or bcalc.'
fi
fi
exit 0
fi
# Change prompt
echo -en "\0prompt\x1fcalc\n"