tmux-kanagawa/scripts/ram_info.sh

87 lines
2.5 KiB
Bash
Raw Normal View History

2020-07-23 16:47:18 +02:00
#!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
2020-06-04 05:44:17 +02:00
2020-08-10 14:04:51 +02:00
# function for getting the refresh rate
get_tmux_option() {
local option=$1
local default_value=$2
local option_value=$(tmux show-option -gqv "$option")
if [ -z $option_value ]; then
echo $default_value
else
echo $option_value
fi
}
2020-06-04 05:44:17 +02:00
get_percent()
{
case $(uname -s) in
Linux)
2020-09-12 07:25:48 +02:00
# percent=$(free -m | awk 'NR==2{printf "%.1f%%\n", $3*100/$2}')
2020-09-12 07:27:32 +02:00
total_mem_gb=$(free -g | awk '/^Mem/ {print $2}')
2020-09-12 07:25:48 +02:00
used_mem=$(free -g | awk '/^Mem/ {print $3}')
total_mem=$(free -h | awk '/^Mem/ {print $2}')
2020-09-12 07:27:32 +02:00
if (( $total_mem_gb == 0)); then
memory_usage=$(free -m | awk '/^Mem/ {print $3}')
total_mem_mb=$(free -m | awk '/^Mem/ {print $2}')
echo $memory_usage\M\B/$total_mem_mb\M\B
elif (( $used_mem == 0 )); then
2020-09-12 07:25:48 +02:00
memory_usage=$(free -m | awk '/^Mem/ {print $3}')
echo $memory_usage\M\B/$total_mem\G\B
else
memory_usage=$(free -g | awk '/^Mem/ {print $3}')
echo $memory_usage\G\B/$total_mem\G\B
fi
2020-06-04 05:44:17 +02:00
;;
Darwin)
2020-06-07 04:41:30 +02:00
# percent=$(ps -A -o %mem | awk '{mem += $1} END {print mem}')
# Get used memory blocks with vm_stat, multiply by 4096 to get size in bytes, then convert to MiB
2020-07-24 05:12:23 +02:00
used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk '{printf "%d\n", ($1+$2) * 4096 / 1048576}')
2020-06-14 03:42:27 +02:00
total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}')
if (( $used_mem < 1024 )); then
echo $used_mem\M\B/$total_mem
else
2020-06-28 19:34:08 +02:00
memory=$(($used_mem/1024))
2020-06-14 03:42:27 +02:00
echo $memory\G\B/$total_mem
fi
2020-06-04 05:44:17 +02:00
;;
2020-09-02 03:24:39 +02:00
FreeBSD)
# Looked at the code from neofetch
hw_pagesize="$(sysctl -n hw.pagesize)"
mem_inactive="$(($(sysctl -n vm.stats.vm.v_inactive_count) * hw_pagesize))"
mem_unused="$(($(sysctl -n vm.stats.vm.v_free_count) * hw_pagesize))"
mem_cache="$(($(sysctl -n vm.stats.vm.v_cache_count) * hw_pagesize))"
free_mem=$(((mem_inactive + mem_unused + mem_cache) / 1024 / 1024))
total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024))
used_mem=$((total_mem - free_mem))
echo $used_mem
if (( $used_mem < 1024 )); then
echo $used_mem\M\B/$total_mem
else
memory=$(($used_mem/1024))
echo $memory\G\B/$total_mem
fi
;;
2020-06-04 05:44:17 +02:00
CYGWIN*|MINGW32*|MSYS*|MINGW*)
# TODO - windows compatability
;;
esac
}
main()
{
2020-08-10 14:04:51 +02:00
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
2020-06-04 05:44:17 +02:00
ram_percent=$(get_percent)
echo "RAM $ram_percent"
2020-08-09 07:50:07 +02:00
sleep $RATE
2020-06-04 05:44:17 +02:00
}
2020-06-04 05:44:17 +02:00
#run main driver
main