aboutsummaryrefslogtreecommitdiffstats
path: root/.config/herbstluftwm/calc.sh
blob: 7a1b9b97f022a968448add6e3acd5f8ae57214b8 (plain)
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
51
52
53
54
55
56
57
58
59
#!/bin/zsh

source ~/.config/init/vars

acc=0
in="first"
prompt="calc:"
pi="3.1415926535897932384626433832795028841971694"
e="2.7182818284590452353602874713526624977572471"

# check first word for special commands
command_check() {
	case "$(echo $1 | cut -d ' ' -f 1)" in
	"y")
		echo "$2" | tr -d '~' | xclip -selection clipboard
		return 1
	;;
	"yank")
		echo "$2" | tr -d '~' | xclip -selection clipboard
		return 1
	;;

	*)
		return 0
	;;
	esac
}

while [[ "$in" != "" ]]; do
	in=$(dmenu -noinput -fn "${bfont}:size=${bfont_size}" -q -h "$bheight" \
		-nb "$bar_bg" -nf "$fg_normal" \
		-sb "$bar_fg" -sf "$fg_focus" -p "$prompt" | \
		sed -e "s/ans/$acc/g")
	if [[ $(command_check "$in" "$acc") -eq 1 ]]; then
		break
	fi

	out=$(echo "pi=$pi; e=$e; $acc $in" | calc -p 2>&1)
	
	if [[ "${?#0}" != "" ]]; then
		out=$(echo "$out" | tr -d "\n")
		if [[ "$out" = "Missing operator" ]]; then
			out=$(echo "pi=$pi; e=$e; $in" | calc -p 2>&1)
			if [[ "${?#0}" != "" ]]; then
				out=$(echo "$out" | tr -d "\n")
				prompt="calc: ($acc) err: $out"
			else
				acc=$out
				prompt="calc: ($acc)"
			fi
		else
			out=$(echo "$out" | tr -d "\n")
			prompt="calc: ($acc) err: $out"
		fi
	else
		acc=$out
		prompt="calc: ($acc)"
	fi
done