Skip to content

Commit

Permalink
Merge pull request #220 from aaronkollasch/feat/tmux-ram-usage
Browse files Browse the repository at this point in the history
Add tmux-ram-usage plugin
  • Loading branch information
ethancedwards8 committed Jul 9, 2023
2 parents 79068c4 + 640eb4c commit 5e9c4e1
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
10 changes: 9 additions & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ To enable plugins set up the `@dracula-plugins` option in you `.tmux.conf` file,
The order that you define the plugins will be the order on the status bar left to right.

```bash
# available plugins: battery, cpu-usage, git, gpu-usage, ram-usage, network, network-bandwidth, network-ping, attached-clients, network-vpn, weather, time, spotify-tui, kubernetes-context
# available plugins: battery, cpu-usage, git, gpu-usage, ram-usage, tmux-ram-usage, network, network-bandwidth, network-ping, attached-clients, network-vpn, weather, time, spotify-tui, kubernetes-context

set -g @dracula-plugins "cpu-usage gpu-usage ram-usage"
```
Expand Down Expand Up @@ -161,6 +161,14 @@ Customize label
set -g @dracula-ram-usage-label "RAM"
```

#### tmux-ram-usage options

Customize label

```bash
set -g @dracula-tmux-ram-usage-label "MEM"
```

#### network-bandwidth

You can configure which network interface you want to view the bandwidth,
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Configuration and options can be found at [draculatheme.com/tmux](https://dracul
- Battery percentage and AC power connection status
- Refresh rate control
- CPU usage (percentage or load average)
- RAM usage
- RAM usage (system and/or tmux server)
- GPU usage
- Custom status texts from external scripts
- GPU VRAM usage
Expand Down
4 changes: 4 additions & 0 deletions scripts/dracula.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ main()
IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-ram-usage-colors" "cyan dark_gray")
script="#($current_dir/ram_info.sh)"

elif [ $plugin = "tmux-ram-usage" ]; then
IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-tmux-ram-usage-colors" "cyan dark_gray")
script="#($current_dir/tmux_ram_info.sh)"

elif [ $plugin = "network" ]; then
IFS=' ' read -r -a colors <<< $(get_tmux_option "@dracula-network-colors" "cyan dark_gray")
script="#($current_dir/network.sh)"
Expand Down
120 changes: 120 additions & 0 deletions scripts/tmux_ram_info.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/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

current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$current_dir/utils.sh"

get_cpids_linux() {
local ppid="$1"
local cpids
local cpid
echo "$ppid"
cpids="$(pgrep -P "$ppid")"
for cpid in $cpids; do
get_cpids_linux "$cpid"
done
}

get_cpids_unix() {
local ppid="$1"
local cpids
local cpid
echo "$ppid"
cpids="$(pgrep -aP "$ppid")"
for cpid in $cpids; do
get_cpids_unix "$cpid"
done
}

kb_to_mb() {
if [ $# == 0 ]; then
read -r num
else
num="$1"
fi
bc <<< "scale=3;$num/1024"
}

kb_to_gb() {
if [ $# == 0 ]; then
read -r num
else
num="$1"
fi
bc <<< "scale=6;$num/1048576"
}

round() {
if [ $# == 1 ]; then
read -r num
scale="$1"
elif [ $# == 2 ]; then
num="$1"
scale="$2"
fi
printf "%.${scale}f" "${num}"
}

get_tmux_ram_usage()
{
local pid
local pids
local total_mem_kb=0
local total_mem_mb=0
local total_mem_gb=0
pid="$(tmux display-message -pF '#{pid}')"
case $(uname -s) in
Linux)
if command -v pstree > /dev/null; then
pids="$(pstree -p "$pid" | tr -d '\n' | sed -rn -e 's/[^()]*\(([0-9]+)\)[^()]*/\1,/g' -e 's/,$//p')"
else
pids="$(get_cpids_linux "$pid" | tr '\n' ',')"
fi
total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ | bc)"
;;

Darwin)
if command -v pstree > /dev/null; then
pids="$(pstree "$pid" | sed -En 's/[^0-9]+([0-9]+) .*/\1/p' | tr '\n' ',')"
else
pids="$(get_cpids_unix "$pid" | tr '\n' ',')"
fi
total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ - | bc)"
;;

FreeBSD)
# TODO check FreeBSD compatibility
if command -v pstree > /dev/null; then
pids="$(pstree "$pid" | sed -En 's/[^0-9]+([0-9]+) .*/\1/p' | tr '\n' ',')"
else
pids="$(get_cpids_unix "$pid" | tr '\n' ',')"
fi
total_mem_kb="$(ps -o rss= -p "$pids" | paste -sd+ - | bc)"
;;

CYGWIN*|MINGW32*|MSYS*|MINGW*)
# TODO - windows compatability
;;
esac
total_mem_mb=$(kb_to_mb "$total_mem_kb" | round 0)
total_mem_gb=$(kb_to_gb "$total_mem_kb" | round 0)

if (( total_mem_gb > 0)); then
echo "${total_mem_gb}GB"
elif (( total_mem_mb > 0 )); then
echo "${total_mem_mb}MB"
else
echo "${total_mem_kb}kB"
fi
}

main()
{
ram_label=$(get_tmux_option "@dracula-tmux-ram-usage-label" "MEM")
ram_usage=$(get_tmux_ram_usage)
echo "$ram_label $ram_usage"
}

#run main driver
main

0 comments on commit 5e9c4e1

Please sign in to comment.