Skip to content

Commit 6c40c04

Browse files
authored
Merge pull request #496 from iyegoroff/mem_used
Memory used segment
2 parents 87c9014 + d98a368 commit 6c40c04

File tree

5 files changed

+135
-1
lines changed

5 files changed

+135
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Proper error logging in segments with function `tp_err_seg()`. [#486](https://github.com/erikw/tmux-powerline/pull/486) [#485](https://github.com/erikw/tmux-powerline/pull/485)
1414
- Adapter segments for [tmux-continuum](https://github.com/tmux-plugins/tmux-continuum/): [`tmux_continuum_save.sh`](segments/tmux_continuum_save.sh) and [`tmux_continuum_status.sh`](segments/tmux_continuum_status.sh) [#493](https://github.com/erikw/tmux-powerline/pull/493)
1515
- [`cpu_temp.sh`](segments/cpu_temp.sh): CPU temperature. [#495](https://github.com/erikw/tmux-powerline/pull/495)
16+
- [`mem_used.sh`](segments/mem_used.sh): Memory used. [#496](https://github.com/erikw/tmux-powerline/pull/496)
1617
### Changed
1718
- **Deprecation Warning:** functions `patched_font_in_use()`, `air_color()` and `format()` have been replaced by `tp_patched_font_in_use()`, `tp_air_color()` and `tp_format()` respectively and will be removed in future releases.
1819
Please update your custom themes and segments now. [#489](https://github.com/erikw/tmux-powerline/pull/489)

lib/headers.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ source "${TMUX_POWERLINE_DIR_LIB}/powerline.sh"
2828
source "${TMUX_POWERLINE_DIR_LIB}/config_file.sh"
2929
# shellcheck source=lib/cpu_temp_helper.sh
3030
source "${TMUX_POWERLINE_DIR_LIB}/cpu_temp_helper.sh"
31+
# shellcheck source=lib/mem_used_helper.sh
32+
source "${TMUX_POWERLINE_DIR_LIB}/mem_used_helper.sh"

lib/mem_used_helper.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# shellcheck shell=bash
2+
3+
# based on https://github.com/thewtex/tmux-mem-cpu-load
4+
# shellcheck disable=SC2001
5+
__tp_mem_used_info() {
6+
if tp_shell_is_macos; then
7+
local stats
8+
local bytes_per_page
9+
local free_pages
10+
local external_pages
11+
local mem_total_bytes
12+
local mem_used_bytes
13+
14+
stats=$(vm_stat | tr '\n' ' ')
15+
bytes_per_page=$(echo "$stats" | sed -e 's/.*page size of \([0-9]*\).*/\1/')
16+
mem_total_bytes=$(sysctl hw.memsize | sed -e 's/^hw.memsize: \([0-9*]\)/\1/')
17+
free_pages=$(echo "$stats" | sed -e 's/.*Pages free: *\([0-9]*\).*/\1/')
18+
external_pages=$(echo "$stats" | sed -e 's/.*File-backed pages: *\([0-9]*\).*/\1/')
19+
mem_used_bytes=$(echo "$mem_total_bytes - ($free_pages + $external_pages) * $bytes_per_page" | bc -l)
20+
21+
echo "$mem_used_bytes" "$mem_total_bytes"
22+
23+
elif tp_shell_is_linux; then
24+
local meminfo
25+
local mem_total
26+
local mem_total_bytes
27+
local mem_free
28+
local shmem
29+
local buffers
30+
local cached
31+
local s_reclaimable
32+
local mem_used_bytes
33+
34+
meminfo=$(tr '\n' ' ' < /proc/meminfo)
35+
mem_total=$(echo "$meminfo" | sed -e 's/^MemTotal: *\([0-9]*\).*/\1/')
36+
mem_total_bytes=$(echo "$mem_total * 1024" | bc -l)
37+
mem_free=$(echo "$meminfo" | sed -e 's/.* MemFree: *\([0-9]*\).*/\1/')
38+
shmem=$(echo "$meminfo" | sed -e 's/.* Shmem: *\([0-9]*\).*/\1/')
39+
buffers=$(echo "$meminfo" | sed -e 's/.* Buffers: *\([0-9]*\).*/\1/')
40+
cached=$(echo "$meminfo" | sed -e 's/.* Cached: *\([0-9]*\).*/\1/')
41+
s_reclaimable=$(echo "$meminfo" | sed -e 's/.* SReclaimable: *\([0-9]*\).*/\1/')
42+
mem_used_bytes=$(echo "($mem_total - $mem_free + $shmem - $buffers - $cached - $s_reclaimable) * 1024" | bc -l)
43+
44+
echo "$mem_used_bytes" "$mem_total_bytes"
45+
fi
46+
};
47+
48+
tp_mem_used_gigabytes() {
49+
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
50+
__round "$(echo "$mem_used_bytes / 1073741824" | bc -l)" 2
51+
}
52+
53+
tp_mem_used_megabytes() {
54+
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
55+
__round "$(echo "$mem_used_bytes / 1048576" | bc -l)" 0
56+
}
57+
58+
tp_mem_used_percentage_at_least() {
59+
local threshold_percentage="$1"
60+
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
61+
echo "($mem_used_bytes / $mem_total_bytes) * 100 >= $threshold_percentage" | bc -l
62+
}
63+
64+
# source https://askubuntu.com/a/179949
65+
# Rounds positive numbers up to the number of digits to the right of the decimal point.
66+
# Example: "__round 1.2345 3" -> "((1000 * 1.2345) + 0.5) / 1000" -> "1.235"
67+
__round() {
68+
local number="$1"
69+
local digits="$2"
70+
71+
env printf "%.${digits}f" "$(echo "scale=${digits};(((10^${digits})*${number})+0.5)/(10^${digits})" | bc)"
72+
};
73+

segments/mem_used.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# shellcheck shell=bash
2+
# Prints memory usage
3+
4+
TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT=""
5+
TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT="GB"
6+
7+
generate_segmentrc() {
8+
read -r -d '' rccontents <<EORC
9+
# Memory icon
10+
export TMUX_POWERLINE_SEG_MEM_USED_ICON="${TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT}"
11+
# Measure unit of memory: "GB" or "MB".
12+
# In context of this segment "1 GB" equals "2 ^ 30 bytes" and "1 MB" eqauls "2 ^ 20 bytes".
13+
export TMUX_POWERLINE_SEG_MEM_USED_UNIT="${TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT}"
14+
EORC
15+
echo "$rccontents"
16+
}
17+
18+
run_segment() {
19+
__process_settings
20+
21+
local mem_used
22+
23+
if [ "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" = "GB" ]; then
24+
mem_used="$(tp_mem_used_gigabytes) GB"
25+
elif [ "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" = "MB" ]; then
26+
mem_used="$(tp_mem_used_megabytes) MB"
27+
else
28+
tp_err_seg "Err: Invalid TMUX_POWERLINE_SEG_MEM_USED_UNIT value - $TMUX_POWERLINE_SEG_MEM_USED_UNIT"
29+
return 1
30+
fi
31+
32+
if [ -n "$mem_used" ]; then
33+
echo "${TMUX_POWERLINE_SEG_MEM_USED_ICON}${mem_used}"
34+
return 0
35+
else
36+
tp_err_seg "Err: Failed to obtain memory usage"
37+
return 1
38+
fi
39+
}
40+
41+
__process_settings() {
42+
if [ -z "$TMUX_POWERLINE_SEG_MEM_USED_ICON" ]; then
43+
export TMUX_POWERLINE_SEG_MEM_USED_ICON="${TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT}"
44+
fi
45+
if [ -z "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" ]; then
46+
export TMUX_POWERLINE_SEG_MEM_USED_UNIT="${TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT}"
47+
fi
48+
};
49+

themes/default.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,16 @@ if [ -z "$TMUX_POWERLINE_RIGHT_STATUS_SEGMENTS" ]; then
139139
# else
140140
# echo "cpu_temp #303080 136"
141141
# fi
142-
# )" \
142+
# )"
143+
# "$(
144+
# if (($(tp_mem_used_percentage_at_least 90))); then
145+
# echo "mem_used #ff2020 235"
146+
# elif (($(tp_mem_used_percentage_at_least 75))); then
147+
# echo "mem_used 136 235"
148+
# else
149+
# echo "mem_used 235 136"
150+
# fi
151+
# )"
143152
#"xkb_layout 125 117"
144153
#"tmux_continuum_save"
145154
#"tmux_continuum_status 14 7"

0 commit comments

Comments
 (0)