Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Tofi as fast calculator #172

Open
BoolmanO opened this issue Jun 1, 2024 · 6 comments
Open

[feature] Tofi as fast calculator #172

BoolmanO opened this issue Jun 1, 2024 · 6 comments

Comments

@BoolmanO
Copy link

BoolmanO commented Jun 1, 2024

I think it can be very helpful in some situations, so, maybe add some sort of option for calculator mode? If you don't want this, maybe add mode when tofi can accept output from some script or smth like that?

@partisani
Copy link

partisani commented Aug 30, 2024

I think the latter is the better option, because by implementing this, (depending on how its done) tofi will be useful for things like getting user input and allow anyone to make a tool that uses it if so desired

@DontBlameMe99
Copy link

This is how I implemented the calculator for myself. It uses tofi as the input and then just copies the result. I hope this can be of use for someone:

# Input operation
operation=$(echo "" | tofi --prompt-text="Math Operation: " --require-match=false)

# Calculate the result
result=$(echo "$operation" | bc -l)

# Copy the result to the clipboard
echo "$result" | wl-copy
notify-send "Result: $result" "The result has been copied to the clipboard."

@partisani
Copy link

like, how didn't i think of this before?

anyways, thanks for the tip

@marcelarie
Copy link

marcelarie commented Oct 2, 2024

Thanks @DontBlameMe99 for the script, I was searching for something like this.

I added some type of history of the recent operations:

#!/usr/bin/env bash

XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
tmp_dir="$XDG_CACHE_HOME/tofi-calc"
tmp_path="$tmp_dir/history"
is_history_value=false
max_age_days=10

mkdir -p "$tmp_dir"
if [ ! -f "$tmp_path" ]; then
    touch "$tmp_path"
fi

# Clean up old entries
find "$tmp_dir" -type f -mtime +$max_age_days -delete

# Input operation
operation=$(cat $tmp_path | tofi --prompt-text="" --require-match=false)

# Exit if no operation provided
if [ -z "$operation" ]; then
	notify-send "No operation provided." "Please provide a valid operation."
	exit
fi

# Remove operation when using history values
if [[ "$operation" == *"="* ]]; then
	operation=$(echo "$operation" | cut -d "=" -f 2)
	is_history_value=true
fi

# Calculate the result and delete new line or backslash characters
result=$(echo "$operation" | bc -l)

# Exit if invalid operation
if [ -z "$result" ]; then
	notify-send "Invalid operation." "Please provide a valid operation."
	exit
fi

# Save the operation and result to history 
if [ "$is_history_value" = false ]; then
	if ! grep -q "$operation = $result" "$tmp_path"; then
	    temp_file=$(mktemp)
	    echo "$operation = $result" > "$temp_file"
	    cat "$tmp_path" >> "$temp_file"
	    mv "$temp_file" "$tmp_path"
	fi
fi

# Copy the result to the clipboard
echo "$result" | wl-copy --trim-newline

notify-send "Result: $result" "The result has been copied to the clipboard."

Another improvement is creating a desktop app for the script, making it accessible via the drun Tofi menu:

[Desktop Entry]
Version=1.1
Name=Calculator
Comment=Tofi calculator
Exec=~/scripts/tofi/calculator.sh
Icon=accessories-calculator
Terminal=false
Type=Application
Categories=Utility;

Hope it is useful for someone else.

@partisani
Copy link

i can barely understand bash (i use nushell)

@flloschy
Copy link

flloschy commented Dec 30, 2024

I also made myself a little workaround to have calculator functionality but with qalc (package name is most likely libqalculate)

in="$(tofi-run --require-match=false)" && ls /usr/share/applications | grep "$in" -w && gtk-launch "$in" || (out="$(qalc -t "$in")" && (echo $out && wl-copy "$out" && notify-send -u low -t 10000 "Math Result" "$out") || gtk-launch "$in")

Basic logic is:

  1. ask for input
  2. see if .desktop file with exact match exists
    if yes: use gtk-launch to start this app and stop
  3. try calling qalc
    if success: copy result and send a notification and stop
  4. try launching the app using gtk-launch anyway

Since this is a single line you can just paste this in for example your hyprland config (and replace the old tofi binding)

bind = SUPER, SUPER_L, exec, in="$(tofi-run --require-match=false)" && ls /usr/share/applications | grep "$in" -w && gtk-launch "$in" || (out="$(qalc -t "$in")" && (echo $out && wl-copy "$out" && notify-send -u low -t 10000 "Math Result" "$out") || gtk-launch "$in")

Edit: I was annoyed that some programs cant launch this way to I changed the logic a bit. Its now:

  1. exists file in /usr/share/applications, yes: gtk-launch
  2. eval the input
  3. if eval doesnt work: calculate

Here is the updated command

in="$(tofi-run --require-match=false)" && ls /usr/share/applications | grep "$in" -w && gtk-launch "$in" || eval "$in" || (out="$(qalc -t "$in")" && (echo $out && wl-copy "$out" && notify-send -u low -t 10000 "Math Result" "$out"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants