Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions =
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Calculator for use with rofi/dmenu(2)
# Copying to the clipboard requires xclip


copy_to_clipboard() {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
wl-copy "$1"
Expand All @@ -23,6 +24,13 @@ usage() {
echo " = sqrt(4)"
echo " = c(2)"
echo
echo "$(tput bold)change calculator:$(tput sgr0)"
echo
echo "By default it will be bc. To change it to another one use"
echo " --calculator argument"
echo
echo " = --calculator=<calculator_command>"
echo
echo "$(tput bold)Force Rofi/dmenu(2):$(tput sgr0)"
echo "By default, if rofi exists, it will be used. To force menu-calc to"
echo "use one or the other, use the --dmenu argument"
Expand All @@ -48,12 +56,20 @@ do
-d=*|--dmenu=*)
menu=$(echo "$var" | cut -d'=' -f 2);
;;
--calculator=*)
calc=$(echo "$var" | cut -d'=' -f 2);
;;
--) break ;;
esac
done


#set bc as default
if [ -z "${calc}" ]; then
calc="bc -l"
fi
# Grab the answer
answer=$(echo "$1" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')
answer=$(echo "$1" | $calc | sed '/\./ s/\.\{0,1\}0\{1,\}$//')

# Path to menu application
if [ -z "${menu+1}" ]; then
Expand Down Expand Up @@ -83,9 +99,9 @@ done
action=$(echo -e "Copy to clipboard\nClear\nClose" | $menu "$@" -p "= $answer")

case $action in
"Clear") $0 "--dmenu=$menu" "--" "$@" ;;
"Clear") $0 "--dmenu=$menu" "--calculator=$calc" "--" "$@" ;;
"Copy to clipboard") copy_to_clipboard "$answer" ;;
"Close") ;;
"") ;;
*) $0 "$answer $action" "--dmenu=$menu" "--" "$@" ;;
*) $0 "$answer $action" "--dmenu=$menu" "--calculator=$calc" "--" "$@" ;;
esac
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ outputs = { ..., menucalc }: {
};
```

If you want to use fend as an backend just use the package menucalc-fend

### Arch Linux AUR

[menu-calc](https://aur.archlinux.org/packages/menu-calc/)
Expand Down Expand Up @@ -86,3 +88,11 @@ By default, if `rofi` is installed, it will be used. You can force `menu-calc`
to use `dmenu` or any other `dmenu`-like application:

= --dmenu=dmenu

### Change calculator backend

By default [bc](https://www.gnu.org/software/bc/manual/html_mono/bc.html)
is used, alternatively a custom command (e.g. [fend](https://github.com/printfn/fend))
can be used instead

= --calculator=fend
13 changes: 8 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
outputs = { self, nixpkgs, flake-utils }:
(flake-utils.lib.eachDefaultSystem (system:
let pkgs = import nixpkgs { inherit system; };
in rec {
packages.menucalc = pkgs.stdenv.mkDerivation rec {
derivation-attrs = backend_pkg: backend_cmd:
rec {
pname = "menucalc";
version = "v1.3.0";

Expand All @@ -29,11 +29,11 @@
'';

wrapperPath =
pkgs.lib.makeBinPath (with pkgs; [ bc rofi xclip wl-clipboard ]);
pkgs.lib.makeBinPath (with pkgs; [ backend_pkg rofi xclip wl-clipboard ]);

fixupPhase = ''
patchShebangs $out/bin
wrapProgram $out/bin/= --prefix PATH : "${wrapperPath}"
wrapProgram $out/bin/= --prefix PATH : "${wrapperPath}" --add-flags "--calculator=${backend_cmd}"
'';

meta = {
Expand All @@ -44,9 +44,12 @@
platforms = with pkgs.lib.platforms; linux;
};
};
in rec {
packages.menucalc = pkgs.stdenv.mkDerivation (derivation-attrs pkgs.bc "bc -l");
packages.menucalc-fend = pkgs.stdenv.mkDerivation (derivation-attrs pkgs.fend "fend");
packages.default = packages.menucalc;

devShells.default =
pkgs.mkShell { packages = with pkgs; [ bc pre-commit shellcheck ]; };
pkgs.mkShell { packages = with pkgs; [ bc fend pre-commit shellcheck ]; };
}));
}
14 changes: 9 additions & 5 deletions menu-calc.1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ menu-calc \- A calculator for
.BR menu-calc
uses
.BR bc (1)
as the backend and will accept any operations
(by default) as the backend and will accept any operations
.BR bc (1)
is able to do.
The answer can be copied to the clipboard and used for further calculations
Expand Down Expand Up @@ -49,15 +49,16 @@ Examples of performing math operations in menu-calc:
.TP
Bind \fBdmenu\fR(1) with a variety of parameters to F4 in \fBi3wm\fR(1).
.B bindsym F4 exec = --dmenu=dmenu -- -i -fn 'Isoveka Term 10' -dim 0.5 -l 3

.TP
use \fBfend\fR(1) instead of bc
.B = --calculator=fend
.SH OPTIONS
.TP
.B \-h
show help
.TP
.B <bc-math-expression>
Parameters to be passed to
.BR bc (1) .
.B <math-expression>
Parameters to be passed to the configured calculator
.TP
.B --dmenu=<dmenu_executable>
Set the dmenu-compatible executable. This can be used to force menu-calc to use
Expand All @@ -68,6 +69,9 @@ Parameters to be passed to
.BR rofi (1)
/
.BR dmenu (1) .
.TP
.B --calculator=<calculator_command>
Set the calculator command. Use this to change to another calculator backend.

.SH AUTHOR
Jonathan Sumner Evans <sumner.evans98 at gmail dot com>
Expand Down