11#! /usr/bin/env bash
22
3- # TODO: add option to choose size unit: M|G
4-
53set -u
64set -e
75
86CURRENT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
97source " $CURRENT_DIR /helpers.sh"
108
11- mem_view_tmpl=$( get_tmux_option " @sysstat_mem_view_tmpl" ' #[fg=#{mem.color2 }]#{mem.pused}#[default]' )
9+ mem_view_tmpl=$( get_tmux_option " @sysstat_mem_view_tmpl" ' #[fg=#{mem.color }]#{mem.pused}#[default]' )
1210
1311mem_stress_threshold=$( get_tmux_option " @sysstat_mem_stress_threshold" " 80" )
1412swap_stress_threshold=$( get_tmux_option " @sysstat_swap_stress_threshold" " 80" )
@@ -18,6 +16,8 @@ mem_color_stress=$(get_tmux_option "@sysstat_mem_color_stress" "yellow")
1816swap_color_ok=$( get_tmux_option " @sysstat_swap_color_ok" " green" )
1917swap_color_stress=$( get_tmux_option " @sysstat_swap_color_stress" " yellow" )
2018
19+ size_unit=$( get_tmux_option " @sysstat_mem_size_unit" " M" )
20+
2121get_mem_color () {
2222 local mem_pused=$1
2323
@@ -40,6 +40,8 @@ get_swap_color(){
4040
4141print_mem () {
4242 local mem_usage
43+ local scale
44+ local size_format
4345
4446 if is_osx; then
4547 mem_usage=$( get_mem_usage_osx)
@@ -48,39 +50,61 @@ print_mem() {
4850 mem_usage=" TODO"
4951 fi
5052
53+ # get_mem_usage* function returns values in KiB
54+ # 1 - scale to KiB
55+ # 1024 - scale to MiB
56+ # 1048576 - scale to GiB
57+ case " $size_unit " in
58+ G) scale=1048576;;
59+ M) scale=1024;;
60+ K) scale=1;;
61+ esac
62+
63+ # Depending on scale factor, change precision
64+ # 12612325K - no digits after floating point
65+ # 1261M - no digits after floating point
66+ # 1.1G - 1 digit after floating point
67+ case " $size_unit " in
68+ G) size_format=' %.1f%s' ;;
69+ M) size_format=' %.0f%s' ;;
70+ K) size_format=' %.0f%s' ;;
71+ esac
72+
5173 # Extract free and used memory in MiB, calculate total and percentage
52- local mem_free=$( echo $mem_usage | awk ' { print $1/1024 }' )
53- local mem_used=$( echo $mem_usage | awk ' { print $2/1024 }' )
74+ local mem_free=$( echo $mem_usage | awk -v scale= " $scale " ' { print $1/scale }' )
75+ local mem_used=$( echo $mem_usage | awk -v scale= " $scale " ' { print $2/scale }' )
5476 local mem_total=$( echo " $mem_free + $mem_used " | calc)
5577 local mem_pused=$( echo " ($mem_used / $mem_total ) * 100" | calc)
5678 local mem_pfree=$( echo " ($mem_free / $mem_total ) * 100" | calc)
5779
5880 # Extract swap free and used in MiB, calculate total and percentage
59- local swap_free=$( echo $mem_usage | awk ' { print $3/1024 }' )
60- local swap_used=$( echo $mem_usage | awk ' { print $4/1024 }' )
81+ local swap_free=$( echo $mem_usage | awk -v scale= " $scale " ' { print $3/scale }' )
82+ local swap_used=$( echo $mem_usage | awk -v scale= " $scale " ' { print $4/scale }' )
6183 local swap_total=$( echo " $swap_free + $swap_used " | calc)
6284 local swap_pused=$( echo " ($swap_used / $swap_total ) * 100" | calc)
6385 local swap_pfree=$( echo " ($swap_free / $swap_total ) * 100" | calc)
6486
6587 # Calculate colors for mem and swap
6688 local mem_color=$( get_mem_color " $mem_pused " )
6789 local swap_color=$( get_swap_color " $swap_pused " )
90+
91+ echo $mem_view_tmpl ;
6892
6993 local mem_view=" $mem_view_tmpl "
70- mem_view=" ${mem_view// ' #{mem.used}' / $(printf " %.0fM " " $mem_used " )} "
94+ mem_view=" ${mem_view// ' #{mem.used}' / $(printf " $size_format " " $mem_used " " $size_unit " )} "
7195 mem_view=" ${mem_view// ' #{mem.pused}' / $(printf " %.0f%%" " $mem_pused " )} "
72- mem_view=" ${mem_view// ' #{mem.free}' / $(printf " %.0fM " " $mem_free " )} "
96+ mem_view=" ${mem_view// ' #{mem.free}' / $(printf " $size_format " " $mem_free " " $size_unit " )} "
7397 mem_view=" ${mem_view// ' #{mem.pfree}' / $(printf " %.0f%%" " $mem_pfree " )} "
74- mem_view=" ${mem_view// ' #{mem.total}' / $(printf " %.0fM " " $mem_total " )} "
98+ mem_view=" ${mem_view// ' #{mem.total}' / $(printf " $size_format " " $mem_total " " $size_unit " )} "
7599 mem_view=" ${mem_view// ' #{mem.color}' / $(echo " $mem_color " | awk ' { print $1 }' )} "
76100 mem_view=" ${mem_view// ' #{mem.color2}' / $(echo " $mem_color " | awk ' { print $2 }' )} "
77101 mem_view=" ${mem_view// ' #{mem.color3}' / $(echo " $mem_color " | awk ' { print $3 }' )} "
78102
79- mem_view=" ${mem_view// ' #{swap.used}' / $(printf " %.0fM " " $swap_used " )} "
103+ mem_view=" ${mem_view// ' #{swap.used}' / $(printf " $size_format " " $swap_used " " $size_unit " )} "
80104 mem_view=" ${mem_view// ' #{swap.pused}' / $(printf " %.0f%%" " $swap_pused " )} "
81- mem_view=" ${mem_view// ' #{swap.free}' / $(printf " %.0fM " " $swap_free " )} "
105+ mem_view=" ${mem_view// ' #{swap.free}' / $(printf " $size_format " " $swap_free " " $size_unit " )} "
82106 mem_view=" ${mem_view// ' #{swap.pfree}' / $(printf " %.0f%%" " $swap_pfree " )} "
83- mem_view=" ${mem_view// ' #{swap.total}' / $(printf " %.0fM " " $swap_total " )} "
107+ mem_view=" ${mem_view// ' #{swap.total}' / $(printf " $size_format " " $swap_total " " $size_unit " )} "
84108 mem_view=" ${mem_view// ' #{swap.color}' / $(echo " $swap_color " | awk ' { print $1 }' )} "
85109 mem_view=" ${mem_view// ' #{swap.color2}' / $(echo " $swap_color " | awk ' { print $2 }' )} "
86110 mem_view=" ${mem_view// ' #{swap.color3}' / $(echo " $swap_color " | awk ' { print $3 }' )} "
0 commit comments