Skip to content

Commit d12cde5

Browse files
committed
refactor: use bytes_to_human_kb for dynamic size formatting
- Replace inline dynamic formatting with shared bytes_to_human_kb() - Apply to both clean and purge commands - Simplify movie comparison with integer arithmetic
1 parent 814b368 commit d12cde5

File tree

2 files changed

+9
-28
lines changed

2 files changed

+9
-28
lines changed

bin/clean.sh

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,24 +1022,7 @@ perform_cleanup() {
10221022

10231023
if [[ $total_size_cleaned -gt 0 ]]; then
10241024
local freed_size_human
1025-
local freed_value
1026-
local freed_unit
1027-
1028-
# ============================================
1029-
# Dynamic size formatting (KB → MB → GB)
1030-
# ============================================
1031-
if ((total_size_cleaned < 1024)); then
1032-
freed_value="$total_size_cleaned"
1033-
freed_unit="KB"
1034-
elif ((total_size_cleaned < 1024 * 1024)); then
1035-
freed_value=$(printf "%.2f" "$(echo "scale=4; $total_size_cleaned/1024" | bc)")
1036-
freed_unit="MB"
1037-
else
1038-
freed_value=$(printf "%.2f" "$(echo "scale=4; $total_size_cleaned/1024/1024" | bc)")
1039-
freed_unit="GB"
1040-
fi
1041-
1042-
freed_size_human="${freed_value}${freed_unit}"
1025+
freed_size_human=$(bytes_to_human_kb "$total_size_cleaned")
10431026

10441027
if [[ "$DRY_RUN" == "true" ]]; then
10451028
local stats="Potential space: ${GREEN}${freed_size_human}${NC}"
@@ -1072,12 +1055,10 @@ perform_cleanup() {
10721055

10731056
summary_details+=("$summary_line")
10741057

1075-
# Movie comparison only if unit is GB and >= 1GB
1076-
if [[ "$freed_unit" == "GB" ]] &&
1077-
[[ $(echo "$freed_value >= 1" | bc) -eq 1 ]]; then
1078-
1079-
local movies
1080-
movies=$(printf "%.0f" "$(echo "scale=2; $freed_value/4.5" | bc)")
1058+
# Movie comparison only if >= 1GB (1048576 KB)
1059+
if ((total_size_cleaned >= 1048576)); then
1060+
local freed_gb=$((total_size_cleaned / 1048576))
1061+
local movies=$((freed_gb * 10 / 45))
10811062

10821063
if [[ $movies -gt 0 ]]; then
10831064
if [[ $movies -eq 1 ]]; then

bin/purge.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ perform_purge() {
210210
fi
211211

212212
if [[ $total_size_cleaned -gt 0 ]]; then
213-
local freed_gb
214-
freed_gb=$(echo "$total_size_cleaned" | awk '{printf "%.2f", $1/1024/1024}')
213+
local freed_size_human
214+
freed_size_human=$(bytes_to_human_kb "$total_size_cleaned")
215215

216-
local summary_line="Space freed: ${GREEN}${freed_gb}GB${NC}"
216+
local summary_line="Space freed: ${GREEN}${freed_size_human}${NC}"
217217
if [[ "${MOLE_DRY_RUN:-0}" == "1" ]]; then
218-
summary_line="Would free: ${GREEN}${freed_gb}GB${NC}"
218+
summary_line="Would free: ${GREEN}${freed_size_human}${NC}"
219219
fi
220220
[[ $total_items_cleaned -gt 0 ]] && summary_line+=" | Items: $total_items_cleaned"
221221
summary_line+=" | Free: $(get_free_space)"

0 commit comments

Comments
 (0)