Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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)
- 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)
- [`cpu_temp.sh`](segments/cpu_temp.sh): CPU temperature. [#495](https://github.com/erikw/tmux-powerline/pull/495)
- [`mem_used.sh`](segments/mem_used.sh): Memory used. [#496](https://github.com/erikw/tmux-powerline/pull/496)
### Changed
- **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.
Please update your custom themes and segments now. [#489](https://github.com/erikw/tmux-powerline/pull/489)
Expand Down
2 changes: 2 additions & 0 deletions lib/headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ source "${TMUX_POWERLINE_DIR_LIB}/powerline.sh"
source "${TMUX_POWERLINE_DIR_LIB}/config_file.sh"
# shellcheck source=lib/cpu_temp_helper.sh
source "${TMUX_POWERLINE_DIR_LIB}/cpu_temp_helper.sh"
# shellcheck source=lib/mem_used_helper.sh
source "${TMUX_POWERLINE_DIR_LIB}/mem_used_helper.sh"
73 changes: 73 additions & 0 deletions lib/mem_used_helper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# shellcheck shell=bash

# based on https://github.com/thewtex/tmux-mem-cpu-load
# shellcheck disable=SC2001
__tp_mem_used_info() {
if tp_shell_is_macos; then
local stats
local bytes_per_page
local free_pages
local external_pages
local mem_total_bytes
local mem_used_bytes

stats=$(vm_stat | tr '\n' ' ')
bytes_per_page=$(echo "$stats" | sed -e 's/.*page size of \([0-9]*\).*/\1/')
mem_total_bytes=$(sysctl hw.memsize | sed -e 's/^hw.memsize: \([0-9*]\)/\1/')
free_pages=$(echo "$stats" | sed -e 's/.*Pages free: *\([0-9]*\).*/\1/')
external_pages=$(echo "$stats" | sed -e 's/.*File-backed pages: *\([0-9]*\).*/\1/')
mem_used_bytes=$(echo "$mem_total_bytes - ($free_pages + $external_pages) * $bytes_per_page" | bc -l)

echo "$mem_used_bytes" "$mem_total_bytes"

elif tp_shell_is_linux; then
local meminfo
local mem_total
local mem_total_bytes
local mem_free
local shmem
local buffers
local cached
local s_reclaimable
local mem_used_bytes

meminfo=$(tr '\n' ' ' < /proc/meminfo)
mem_total=$(echo "$meminfo" | sed -e 's/^MemTotal: *\([0-9]*\).*/\1/')
mem_total_bytes=$(echo "$mem_total * 1024" | bc -l)
mem_free=$(echo "$meminfo" | sed -e 's/.* MemFree: *\([0-9]*\).*/\1/')
shmem=$(echo "$meminfo" | sed -e 's/.* Shmem: *\([0-9]*\).*/\1/')
buffers=$(echo "$meminfo" | sed -e 's/.* Buffers: *\([0-9]*\).*/\1/')
cached=$(echo "$meminfo" | sed -e 's/.* Cached: *\([0-9]*\).*/\1/')
s_reclaimable=$(echo "$meminfo" | sed -e 's/.* SReclaimable: *\([0-9]*\).*/\1/')
mem_used_bytes=$(echo "($mem_total - $mem_free + $shmem - $buffers - $cached - $s_reclaimable) * 1024" | bc -l)

echo "$mem_used_bytes" "$mem_total_bytes"
fi
};

tp_mem_used_gigabytes() {
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
__round "$(echo "$mem_used_bytes / 1073741824" | bc -l)" 2
}

tp_mem_used_megabytes() {
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
__round "$(echo "$mem_used_bytes / 1048576" | bc -l)" 0
}

tp_mem_used_percentage_at_least() {
local threshold_percentage="$1"
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest in functions to make use of named local variables for documentation purposes, and readability. Putting them at the top of a function gives bash functions something similar to an "argument list", code that documents! So e.g.

local threshold_bytes="$1"

and then use $threshold_bytes in the function.

echo "($mem_used_bytes / $mem_total_bytes) * 100 >= $threshold_percentage" | bc -l
}

# source https://askubuntu.com/a/179949
# Rounds positive numbers up to the number of digits to the right of the decimal point.
# Example: "__round 1.2345 3" -> "((1000 * 1.2345) + 0.5) / 1000" -> "1.235"
__round() {
local number="$1"
local digits="$2"

env printf "%.${digits}f" "$(echo "scale=${digits};(((10^${digits})*${number})+0.5)/(10^${digits})" | bc)"
};

49 changes: 49 additions & 0 deletions segments/mem_used.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# shellcheck shell=bash
# Prints memory usage

TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT=" "
TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT="GB"

generate_segmentrc() {
read -r -d '' rccontents <<EORC
# Memory icon
export TMUX_POWERLINE_SEG_MEM_USED_ICON="${TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT}"
# Measure unit of memory: "GB" or "MB".
# In context of this segment "1 GB" equals "2 ^ 30 bytes" and "1 MB" eqauls "2 ^ 20 bytes".
export TMUX_POWERLINE_SEG_MEM_USED_UNIT="${TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT}"
EORC
echo "$rccontents"
}

run_segment() {
__process_settings

local mem_used

if [ "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" = "GB" ]; then
mem_used="$(tp_mem_used_gigabytes) GB"
elif [ "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" = "MB" ]; then
mem_used="$(tp_mem_used_megabytes) MB"
else
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For robustness, we could check for = "MB here and in the else return an error, prompting the user that they might not have configured what they wanted.

As of recent, we have a way to report errors with tp_err_seg() e.g.

tp_err_seg "Err: Unable to auto-detect your location"
then return with a non-0 code.

tp_err_seg "Err: Invalid TMUX_POWERLINE_SEG_MEM_USED_UNIT value - $TMUX_POWERLINE_SEG_MEM_USED_UNIT"
return 1
fi

if [ -n "$mem_used" ]; then
echo "${TMUX_POWERLINE_SEG_MEM_USED_ICON}${mem_used}"
return 0
else
tp_err_seg "Err: Failed to obtain memory usage"
return 1
fi
}

__process_settings() {
if [ -z "$TMUX_POWERLINE_SEG_MEM_USED_ICON" ]; then
export TMUX_POWERLINE_SEG_MEM_USED_ICON="${TMUX_POWERLINE_SEG_MEM_USED_ICON_DEFAULT}"
fi
if [ -z "$TMUX_POWERLINE_SEG_MEM_USED_UNIT" ]; then
export TMUX_POWERLINE_SEG_MEM_USED_UNIT="${TMUX_POWERLINE_SEG_MEM_USED_UNIT_DEFAULT}"
fi
};

11 changes: 10 additions & 1 deletion themes/default.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,16 @@ if [ -z "$TMUX_POWERLINE_RIGHT_STATUS_SEGMENTS" ]; then
# else
# echo "cpu_temp #303080 136"
# fi
# )" \
# )"
# "$(
# if (($(tp_mem_used_percentage_at_least 90))); then
# echo "mem_used #ff2020 235"
# elif (($(tp_mem_used_percentage_at_least 75))); then
# echo "mem_used 136 235"
# else
# echo "mem_used 235 136"
# fi
# )"
#"xkb_layout 125 117"
#"tmux_continuum_save"
#"tmux_continuum_status 14 7"
Expand Down