#!/bin/bash # tcalc by Pedro Estarque # A very simple pocket calculator for your terminal # this software is licensed under GPL with no warranty at all # very little testing was done so please # do not trust the results for any serious purposes TOP () { echo -ne ' \033[3;25f\033(0lqqqqqqqqqqqqqqqqqqqqqqqqqk \033(B ' echo -ne "\033[4;25f\033(0x$result x\033(B" echo -ne '\033[5;25f\033(0xqqqqqqqqqqqqqqqqqqqqqqqqqx \033(B ' echo -ne '\033[6;25f\033(0x aa C aaaaaaaaaaaaaaaaaa x\033(B' } BOTTOM () { echo -ne ' \033[7;25f\033(0xqqqqqqqqqqqqqqqqqqqqqqqqqx\033(B ' echo -ne ' \033[8;25f\033(0x 7 8 9 / x\033(B ' echo -ne ' \033[9;25f\033(0x x\033(B ' echo -ne ' \033[10;25f\033(0x x\033(B ' echo -ne ' \033[11;25f\033(0x 4 5 6 * x\033(B ' echo -ne ' \033[12;25f\033(0x x\033(B ' echo -ne ' \033[13;25f\033(0x x\033(B ' echo -ne ' \033[14;25f\033(0x 1 2 3 - x\033(B ' echo -ne ' \033[15;25f\033(0x x\033(B ' echo -ne ' \033[16;25f\033(0x x\033(B ' echo -ne ' \033[17;25f\033(0x 0 . = + x\033(B ' echo -ne ' \033[18;25f\033(0mqqqqqqqqqqqqqqqqqqqqqqqqqj \033(B ' } CALC () { result2=`echo "scale=5; "$first" "$opr" "$second"" | bc 2> /tmp/tcalc_error.txt` ORDER=`echo -n "$result2" | wc -c` CUTNUM=`echo 24 - "$ORDER" | bc` spaces=`echo " " | cut -c 1-$CUTNUM` result="$spaces""$result2" if [ "$ORDER" -ge '20' ] then err=`echo "$result2" | cut -c 1-18` echo -ne "\033[4;25f\033(0x\033[5mERR \033[0m$err x\033(B" else echo -ne "\033[4;25f\033(0x$result x\033(B" BOTTOM echo -ne ' \033[17;25f\033(0x 0 . \033[5;7m=\033[0m + x\033(B ' fi cat /tmp/tcalc_error.txt | grep -q 'error' if [ $? -eq 0 ] then echo -ne "\033[4;25f\033(0x\033[5mERR\033[0m$err x\033(B" fi echo "$result2" | pbcopy } clear input=0 result=" 0" TOP BOTTOM while [ "$input" != q ] do read -s -n 1 "input" boldinput=`echo -e "\033[5;7m$input\033[0m"` boldbar=`echo -e "\033[5;7m\/\033[0m"` if [[ "$input" = [0-9] ]] || [ "$input" = '.' ] then if [ "$result" = " 0" ] then result=`echo "$result" | sed 's/0/\ /'` fi if [ -n "$result1" ] then result=" " first=$result1 result1="" fi if [ "$EQUAL" = 1 ] then result=" " EQUAL=0 fi result=`echo "$result" | sed 's/\ //'` result=`echo "$result" | rev` result=`echo $input"$result"` result=`echo "$result" | rev` TOP BOTTOM | sed "s/ [$input] / $boldinput /" echo -ne '\033[20;29f ' elif [ "$input" = '+' ] || [ "$input" = '*' ] then if [ "$MULTI_OPR" = 2 ] then first=$result CALC fi if [ "$MULTI_OPR" = 1 ] then second=$result CALC MULTI_OPR=2 fi result1="$result" opr="$input" BOTTOM | sed "s/ [$input] / $boldinput /" echo -ne '\033[20;29f ' EQUAL=0 MULTI_OPR=1 elif [ "$input" = '-' ] then if [ "$MULTI_OPR" = 2 ] then first=$result CALC fi if [ "$MULTI_OPR" = 1 ] then second=$result CALC MULTI_OPR=2 fi opr="$input" BOTTOM | sed "s/ [$input] / $boldinput /" echo -ne '\033[20;29f ' EQUAL=0 MULTI_OPR=1 if [ "$result" = " 0" ] then result=" -" TOP MULTI_OPR=0 else result1="$result" fi elif [ "$input" = '/' ] then if [ "$MULTI_OPR" = 2 ] then first=$result CALC fi if [ "$MULTI_OPR" = 1 ] then second=$result CALC MULTI_OPR=2 fi result1="$result" opr="$input" BOTTOM | sed "s/ \/ / $boldbar /" echo -ne '\033[20;29f ' EQUAL=0 MULTI_OPR=1 elif [ -z $input ] || [ "$input" = '=' ] then if [ "$EQUAL" = '1' ] then first=$result else second=$result fi CALC EQUAL=1 MULTI_OPR=0 elif [ "$input" = 'c' ] then result=" 0" first="" opr="" echo -ne "\033[4;25f\033(0x$result x\033(B" BOTTOM echo -ne '\033[6;25f\033(0x aa \033[5;7mC\033[0m aaaaaaaaaaaaaaaaaa x\033(B' EQUAL=0 else if [ "$input" != 'q' ] then echo -ne '\033[20;29f Unaccepted input!\007' fi fi done rm /tmp/tcalc_error.txt clear