-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments
I think the latter is the better option, because by implementing this, (depending on how its done) |
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." |
like, how didn't i think of this before? anyways, thanks for the tip |
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. |
i can barely understand bash (i use nushell) |
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:
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:
Here is the updated command
|
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?
The text was updated successfully, but these errors were encountered: