aboutsummaryrefslogtreecommitdiffstats
path: root/config/hypr/UserScripts/RofiCalc.sh
blob: 48f150d8cb8b432872faef2263af23d5356bae88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# /* Calculator (using qalculate) and rofi */
# /* Submitted by: https://github.com/JosephArmas */

rofi_theme="${XDG_CONFIG_HOME:-$HOME/.config}/rofi/config-calc.rasi"

# Dependency checks
for cmd in rofi qalc wl-copy; do
    if ! command -v "$cmd" >/dev/null 2>&1; then
        if command -v notify-send >/dev/null 2>&1; then
            notify-send -u critical "RofiCalc" "Missing required dependency: $cmd"
        else
            echo "Error: Missing required dependency: $cmd" >&2
        fi
        exit 1
    fi
done

CALC_LOCKFILE="/tmp/roficalc-${UID:-0}.pid"
if [[ -f "$CALC_LOCKFILE" ]]; then
    oldpid="$(cat "$CALC_LOCKFILE" 2>/dev/null || true)"
    if [[ -n "$oldpid" ]] && kill -0 "$oldpid" 2>/dev/null; then
        kill "$oldpid" 2>/dev/null || true
    fi
    rm -f "$CALC_LOCKFILE" 2>/dev/null || true
fi
printf '%d' "$$" > "$CALC_LOCKFILE" 2>/dev/null || true
trap 'rm -f "$CALC_LOCKFILE" 2>/dev/null' EXIT INT TERM

# main function
calc_result=""
result=""

while true; do
    mesg_args=()
    if [[ -n "$result" || -n "$calc_result" ]]; then
        mesg_args=(-mesg "$result      =    $calc_result")
    fi

    result=$(
        rofi -i -dmenu \
            -config "$rofi_theme" \
            "${mesg_args[@]}"
    )

    if [ $? -ne 0 ]; then
        exit 0
    fi

    if [ -n "$result" ]; then
        calc_result=$(qalc -t "$result")
        echo "$calc_result" | wl-copy
    fi
done
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage