Skip to content

Instantly share code, notes, and snippets.

@marcelarie
Created October 28, 2024 20:01
Show Gist options
  • Save marcelarie/e77a1ee4b4a38a38756d163ae72ddcb2 to your computer and use it in GitHub Desktop.
Save marcelarie/e77a1ee4b4a38a38756d163ae72ddcb2 to your computer and use it in GitHub Desktop.
Minimal tofi calculator
#!/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."
@marcelarie
Copy link
Author

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment