Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WSL support #86

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ set -g @cpu_percentage_format "%5.1f%%" # Add left padding

Don't forget to reload the tmux environment (`$ tmux source-file ~/.tmux.conf`) after you do this.

### WSL support

If you're running on WSL you can set these options to get the CPU / RAM values from the Windows host system instead:

```shell
@cpu_wsl_host 'true' # true/false
@ram_wsl_host 'true # true/false
```

Note this only works if `$WSL_DISTRO_NAME` is also set, which is done by default inside WSL.

### Tmux Plugins

This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [battery](https://github.com/tmux-plugins/tmux-battery), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page.
Expand Down
5 changes: 4 additions & 1 deletion scripts/cpu_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ cpu_percentage_format="%3.1f%%"
print_cpu_percentage() {
cpu_percentage_format=$(get_tmux_option "@cpu_percentage_format" "$cpu_percentage_format")

if command_exists "iostat"; then
if [[ $(get_tmux_option @cpu_wsl_host) == "true" && -n "${WSL_DISTRO_NAME}" ]] ; then
Copy link
Collaborator

Choose a reason for hiding this comment

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

any reason not to just do this?

Suggested change
if [[ $(get_tmux_option @cpu_wsl_host) == "true" && -n "${WSL_DISTRO_NAME}" ]] ; then
if command_exists "wmic.exe"; then

or even better, alter the is_cygwin function & convert pre-exisiting WMIC calls to wmic.exe?

Copy link
Author

Choose a reason for hiding this comment

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

I was trying to ensure we gave users the options if they wanted to just see the guest/WSL performance or the whole system and to make sure it only ran if you actually were inside WSL (I use the same .tmux.conf on multiple systems)

or even better, alter the is_cygwin function & convert pre-exisiting WMIC calls to wmic.exe?

This might be a better option, although cygwin and WSL are different beasts - I'll install cygwin and give it a go when I've got chance.

Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to install cygwin -- I mean we can change is_cygwin to simply is_win (i.e. is_wsl_or_cygwin)

load="$(cached_eval wmic.exe cpu get LoadPercentage | grep -Eo '^[0-9]+')"
echo "$load" | awk -v format="$cpu_percentage_format" '{printf format, $1}'
elif command_exists "iostat"; then

if is_linux_iostat; then
cached_eval iostat -c 1 2 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./'
Expand Down
10 changes: 9 additions & 1 deletion scripts/ram_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ sum_macos_vm_stats() {
print_ram_percentage() {
ram_percentage_format=$(get_tmux_option "@ram_percentage_format" "$ram_percentage_format")

if command_exists "free"; then
if [[ $(get_tmux_option @ram_wsl_host) == "true" && -n "${WSL_DISTRO_NAME}" ]] ; then

# TotalPhysicalMemory is in bytes, but FreePhysicalMemory is in KiB
total=$(wmic.exe ComputerSystem get TotalPhysicalMemory | awk '/^[0-9]/{print $1/1024}')
free=$(wmic.exe OS get freevirtualmemory | awk '/^[0-9]/{print $1}')
used=$(($total - $free))
echo "$used $total" | awk -v format="$ram_percentage_format" '{printf(format, 100*$1/$2)}'

elif command_exists "free"; then
cached_eval free | awk -v format="$ram_percentage_format" '$1 ~ /Mem/ {printf(format, 100*$3/$2)}'
elif command_exists "vm_stat"; then
# page size of 4096 bytes
Expand Down