The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Scripting an arithmeticker

By Bob Mesibov, published 12/03/2017 in Tutorials


I've just built my own GUI calculator (script below).

OK, you're probably thinking "WHY? There are plenty of perfectly good GUI calculators for Linux. Why build another one?" And my answer is "Because the available calculators are way too powerful and don't do what I want on-screen."

I manage my screen's real estate pretty carefully. I run Debian Xfce, and the screenshot below shows a typical workspace on my 1920 x 1080 px monitor. Applications (like the GIMP screen pictured) open maximised, but are restricted in size and position because I've set my workspace margins to left 10 px, bottom 10 px, top 60 px and right 400 px. The top margin leaves room for the 50 px panel at top right, and the right margin leaves plenty of room for me to read the keyboard shortcuts I've written on the desktop background image.

I often do simple arithmetic while working with an application. For example, I might want to scale an image in GIMP and I need to calculate a new image width. What I used to do was open gnome-calculator in basic mode. It appeared in the centre of the application window:

I would then drag the gnome-calculator window into that space to the right of the GIMP window, do the calculation there and return to GIMP with the result. Focus follows mouse on my desktop, so that was easy enough. It would have been more convenient, though, if I could have launched gnome-calculator in that right-hand space automatically, using the --geometry=(number)x(number)+(number)+(number) option that many GTK applications respect.

But not gnome-calculator. And its highly praised alternatives, like qalculate and extcalc and kcalc, offer way too much calculating power and way too many options. All I want to do is divide 400 by 21, not use pi and Planck's constant and a mess of variables in some long, complicated polynomial.

Going simple

So... It was back to the ever-reliable bc, with YAD dialogs supplying a GUI front-end. I launch the script shown below with the keyboard shortcut super + q. A little window pops up in the right place and grabs focus

and I enter an arithmetical expression:

I press Enter and the entry window is replaced by a second one a little further down in that right-hand space. It prints the result to 5 decimal places, which is a default I've hard-coded in the script:

If I press Enter again, the Result window disappears and the entry one re-appears, ready for a new calculation. Pressing Esc closes the script (with either window).

The YAD Result dialog includes the --selectable-labels option. This means I can, if I want, highlight the result with my mouse, and middle-click paste the result into the main application window, using Alt+Tab to switch between windows.

The script

#!/bin/bash

while true; do

        var1=$(yad --entry --geometry=+1600+300 --no-buttons --text="Enter calculation below: ")

        if [ ! -z "$var1" ]; then
                var2=$(echo "scale=5; $var1" | bc)
                yad --geometry=+1600+500 --selectable-labels --title="Result" --text="<b>$var2</b>"  --text-align=center
        else
                exit
        fi

if [ $? == 0 ]; then
        continue
else
        break
fi

done

exit

Top illustration: detail from "Allegory of Arithmetic" (ca 1650) by Laurent de La Hyre; from Wikimedia Commons by Ad Meskens.



About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.

Tags: tutorial scripts calculator bc console bash mathematics YAD
blog comments powered by Disqus