Merge pull request #189 from IGLOU-EU/i165

🐛 fix(#165): is no longer based on word matching
This commit is contained in:
Ethan Edwards 2023-04-08 10:05:55 -04:00 committed by GitHub
commit 181efce96f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,35 +5,26 @@ export LC_ALL=en_US.UTF-8
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh source $current_dir/utils.sh
get_percent() get_ratio()
{ {
case $(uname -s) in case $(uname -s) in
Linux) Linux)
total_mem_gb=$(free -g | awk '/^Mem/ {print $2}') usage="$(free -h | awk 'NR==2 {print $3}')"
used_mem=$(free -g | awk '/^Mem/ {print $3}') total="$(free -h | awk 'NR==2 {print $2}')"
total_mem=$(free -h | awk '/^Mem/ {print $2}') formated="${usage}/${total}"
if (( $total_mem_gb == 0)); then
memory_usage=$(free -m | awk '/^Mem/ {print $3}') echo "${formated//i/B}"
total_mem_mb=$(free -m | awk '/^Mem/ {print $2}')
echo $memory_usage\M\B/$total_mem_mb\M\B
elif (( $used_mem == 0 )); then
memory_usage=$(free -m | awk '/^Mem/ {print $3}')
echo $memory_usage\M\B/$total_mem_gb\G\B
else
memory_usage=$(free -g | awk '/^Mem/ {print $3}')
echo $memory_usage\G\B/$total_mem_gb\G\B
fi
;; ;;
Darwin) Darwin)
# Get used memory blocks with vm_stat, multiply by page size to get size in bytes, then convert to MiB # Get used memory blocks with vm_stat, multiply by page size to get size in bytes, then convert to MiB
used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk -v pagesize=$(pagesize) '{printf "%d\n", ($1+$2) * pagesize / 1048576}') used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk -v pagesize=$(pagesize) '{printf "%d\n", ($1+$2) * pagesize / 1048576}')
total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}') total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}')
if (( $used_mem < 1024 )); then if ((used_mem < 1024 )); then
echo $used_mem\M\B/$total_mem echo "${used_mem}MB/$total_mem"
else else
memory=$(($used_mem/1024)) memory=$((used_mem/1024))
echo $memory\G\B/$total_mem echo "${memory}GB/$total_mem"
fi fi
;; ;;
@ -48,11 +39,11 @@ get_percent()
total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024)) total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024))
used_mem=$((total_mem - free_mem)) used_mem=$((total_mem - free_mem))
echo $used_mem echo $used_mem
if (( $used_mem < 1024 )); then if ((used_mem < 1024 )); then
echo $used_mem\M\B/$total_mem echo "${used_mem}MB/$total_mem"
else else
memory=$(($used_mem/1024)) memory=$((used_mem/1024))
echo $memory\G\B/$total_mem echo "${memory}GB/$total_mem"
fi fi
;; ;;
@ -64,12 +55,9 @@ get_percent()
main() main()
{ {
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
ram_label=$(get_tmux_option "@dracula-ram-usage-label" "RAM") ram_label=$(get_tmux_option "@dracula-ram-usage-label" "RAM")
ram_percent=$(get_percent) ram_ratio=$(get_ratio)
echo "$ram_label $ram_percent" echo "$ram_label $ram_ratio"
sleep $RATE
} }
#run main driver #run main driver