summaryrefslogtreecommitdiff
path: root/dotfiles/system/.bashrc.d/utilities.sh
blob: 431cac048753f8608c1975dc85df2d9845003fec (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# utilities.sh
# Craig Jennings <c@cjennings.net>
# General utility functions

# =============================================================================
# Archive Extraction
# =============================================================================
extract() {
    if [ -f "$1" ]; then
        case "$1" in
            *.tar.bz2)   tar xjf "$1"    ;;
            *.tar.gz)    tar xzf "$1"    ;;
            *.bz2)       bunzip2 "$1"    ;;
            *.rar)       rar x "$1"      ;;
            *.gz)        gunzip "$1"     ;;
            *.tar)       tar xf "$1"     ;;
            *.tbz2)      tar xjf "$1"    ;;
            *.tgz)       tar xzf "$1"    ;;
            *.zip)       unzip "$1"      ;;
            *.Z)         uncompress "$1" ;;
            *)           echo "$1 cannot be extracted via extract()" ;;
        esac
    else
        echo "$1 is not a valid file"
    fi
}

# =============================================================================
# Archive Compression
# =============================================================================
compress() {
    if [ $# -ne 2 ]; then
        echo "Usage: compress <format> <file_or_directory>"
        echo "Formats: tar.bz2, tar.gz, bz2, tar, tbz2, tgz, zip, gz, Z"
        return 1
    fi

    format="$1"
    target="$2"

    if [ ! -e "$target" ]; then
        echo "Error: '$target' does not exist"
        return 1
    fi

    basename=$(basename "$target")

    case "$format" in
        tar.bz2|tbz2) output="${basename}.tar.bz2" ;;
        tar.gz|tgz)   output="${basename}.tar.gz" ;;
        bz2)          output="${target}.bz2" ;;
        gz)           output="${target}.gz" ;;
        tar)          output="${basename}.tar" ;;
        zip)          output="${basename}.zip" ;;
        Z)            output="${target}.Z" ;;
        *)
            echo "Error: Unknown format '$format'"
            return 1
            ;;
    esac

    if [ -e "$output" ]; then
        printf "Warning: '%s' already exists. Overwrite? (y/N): " "$output"
        read -r response
        case "$response" in
            [yY]|[yY][eE][sS]) rm -f "$output" ;;
            *) echo "Aborted." && return 1 ;;
        esac
    fi

    case "$format" in
        tar.bz2|tbz2) tar -cjf "$output" "$target" ;;
        tar.gz|tgz)   tar -czf "$output" "$target" ;;
        bz2)
            [ -d "$target" ] && echo "Error: bz2 only works on files" && return 1
            bzip2 -k "$target"
            ;;
        gz)
            [ -d "$target" ] && echo "Error: gz only works on files" && return 1
            gzip -k "$target"
            ;;
        tar)          tar -cf "$output" "$target" ;;
        zip)
            [ -d "$target" ] && zip -r "$output" "$target" || zip "$output" "$target"
            ;;
        Z)
            [ -d "$target" ] && echo "Error: Z only works on files" && return 1
            command compress -c "$target" > "$output"
            ;;
    esac

    [ $? -eq 0 ] && echo "Created $output" || echo "Compression failed"
}

# =============================================================================
# DD Helper
# =============================================================================
dd_with_bs() {
    OUT_DIR=$(dirname "$2")
    if [ ! -e "$1" ] || [ ! -e "$OUT_DIR" ]; then
        echo "$1 or $OUT_DIR doesn't exist"
        return 1
    fi
    IN_BS=$(stat -c "%o" "$1")
    OUT_BS=$(stat -c "%o" "$OUT_DIR")
    echo dd \"if=$1\" \"of=$2\" \"ibs=$IN_BS\" \"obs=$OUT_BS\"
}

# =============================================================================
# Clock, Timer, Alarm, Stopwatch
# =============================================================================
export BEEP="/usr/share/sounds/freedesktop/stereo/bell.oga"
alias beep='paplay $BEEP'

clock() {
    while true; do
        tput clear
        echo ""
        date +" %l : %M %p" | figlet -f roman -w 200
        sleep 1
    done
}

timer() {
    if [ "$#" -lt 2 ]; then
        echo "Pass the time and a notification. Example: timer 1h30m Parking expiring"
        return 1
    fi
    message="${*:2}"
    start_time=$(date +"%H:%M:%S %p")
    printf "\nStarting %s timer at %s\n" "$1" "$start_time"
    snore "$1" && paplay "$BEEP" && notify-send "Timer" "$message"
}

alarm() {
    if [ "$#" -lt 2 ]; then
        echo "Pass both the time and a message. Example: alarm 1:45pm Time to eat!"
        return 1
    fi

    if ! date -d "$1" >/dev/null 2>&1; then
        echo "Invalid time: $1"
        return 1
    fi

    echo "paplay \$BEEP && notify-send \"Alarm\" \"$*\"" | at "$1" >/dev/null 2>&1
    echo ""
    echo "Alarm '${*:2}' is queued for $1."
    echo "To see all alarms: atq"
    echo "To remove an alarm: atrm <number>"
    echo ""
}

# Stopwatch
sw_start_time=0
sw_started=0

swreset() {
    sw_start_time=0
    sw_started=0
    echo "Stopwatch reset"
}

swshow() {
    if [ "$sw_started" = 0 ]; then
        echo "Error: Stopwatch not started" >&2
        return 1
    fi

    current_time=$(date +%s)
    elapsed_time=$((current_time - sw_start_time))

    if [ "$elapsed_time" -lt 60 ]; then
        echo "Elapsed time: $elapsed_time seconds"
    elif [ "$elapsed_time" -lt 3600 ]; then
        minutes=$((elapsed_time / 60))
        seconds=$((elapsed_time % 60))
        echo "Elapsed time: $minutes minutes, $seconds seconds"
    else
        hours=$((elapsed_time / 3600))
        minutes=$(((elapsed_time / 60) % 60))
        seconds=$((elapsed_time % 60))
        echo "Elapsed time: $hours hours, $minutes minutes, $seconds seconds"
    fi
}

swstop() {
    swshow
    swreset
}

swstart() {
    if [ "$sw_started" = 1 ]; then
        printf "Stopwatch is already started. Reset? (y/n): "
        read -r answer
        case "$answer" in
            [yY]) swreset ;;
            [nN]) echo "Stopwatch not reset." && swshow && return ;;
            *) echo "Error: Invalid input." >&2 && return 1 ;;
        esac
    fi

    sw_started=1
    sw_start_time=$(date +%s)
    printf "Stopwatch started at %s\n\n" "$(date +"%H:%M:%S %p")"
}