Skip to content

Instantly share code, notes, and snippets.

@edoz90 edoz90/windows_list.zsh
Last active Sep 13, 2019

Embed
What would you like to do?
Prints windows list on focused desktop (bspwm) with dedicated icons
#!/usr/bin/env zsh
#Get id of all non-floating windows on current desktop
WINDOWS=(${(f)"$(bspc query -N -d focused -n .window)"})
#Get id of currently focused window
FOCUSED="$(bspc query -N -d focused -n .focused.window)"
FORMAT="-f3-"
isVim () {
if [[ "${wname_complete:l}" == *"vim"* ]]; then
echo "$(echo -n "${wname}" | cut -d '-' -f1)"
else
echo "${wname}"
fi
}
parse_windows() {
#Get WM_CLASS string from all IDs
windowlist=""
private="(Private Browsing)"
for wid in ${WINDOWS}; do
local max_length="${1}"
#Get window's class, title/name and PID
local info=$(xprop -id "${wid}" WM_NAME WM_CLASS _NET_WM_PID)
wclass="$(echo "${info}" | rg WM_CLASS | cut -d "=" -f2 | sed -e 's/\"//g')"
wclass=($(awk -F ',' '{$1=$1} 1' <<< "${wclass}" ))
# Get window's pid
local wpid=$(echo "${info}" | rg _NET_WM_PID | cut -d " " -f3)
#Get window's name
local wname_complete="$(echo "$info" | rg WM_NAME)"
local wname="$(echo -n "${wname_complete//\"}" | cut -d " " $FORMAT)"
case "${wclass[2]}" in
"Autopsy" )
wname="${wname}" ;;
"burp-StartBurp" )
wname="${wname}" ;;
"Chromium"*|"Google-chrome")
wname="${wname}" ;;
"firefox" )
if test "${wname_complete#*$private}" != "${wname_complete}"; then
wname="${wname// - Mozilla Firefox/}"
else
wname="${wname// - Mozilla Firefox/}"
fi ;;
"File-roller" )
wname="${wname}" ;;
"Gufw.py" )
wname="${wname}" ;;
"java" )
wname="${wname}" ;;
"libreoffice-calc" )
wname="${wname// - LibreOffice Calc/}" ;;
"libreoffice-impress" )
wname="${wname// - LibreOffice Impress/}" ;;
"Nm-connection-editor" )
wname="${wname}" ;;
"Mist" )
wname="${wname}" ;;
"Mumble" )
wname="${wname}" ;;
"Nemo"|"Pcmanfm"|"Thunar" )
wname="${wname// - File Manager/}" ;;
"Popcorn-Time" )
wname="${wname}" ;;
"rdesktop"|"org.remmina.Remmina" )
wname="${wname}";;
"Spotify" )
wname="${wname}" ;;
"Steam" )
wname="${wname}" ;;
"Sublime_text"|"processing-app-Base" )
wname="${wname}" ;;
"TelegramDesktop" )
wname="${wname}" ;;
"Terminator"|"XTerm" )
wname="$(isVim "${wname_complete}" "${wname}")" ;;
"Thunderbird" )
wname="${wname// - Mozilla Thunderbird/}" ;;
"Transmission-gtk" )
wname="${wname}" ;;
"vlc"|"mpv" )
wname="${wname}" ;;
"Viewnior" )
wname="${wname}" ;;
"VirtualBox" )
wname="${wname}" ;;
"Zathura" )
wname="$(basename ${wname})" ;;
"zoom" )
wname="${wname}" ;;
*)
case "${wclass[1]}" in
"chromium" )
wname="${wname}" ;;
"guake" )
wname="$(isVim "${wname_complete}" "${wname}")" ;;
"VirtualBox" )
wname="${wname}" ;;
# Print the class, helps to make a new entry
*)
wname="$wname ${wclass[2]} ${wclass[1]}" ;;
esac
esac
# Default: click with middle click
wname="%{A2:kill -3 ${wpid}:}${wname:0:${max_length}}%{A}"
if [[ "${wid}" == "${FOCUSED}" ]]; then
win="$(printf "%s" "%{F#2ECC71} ${wname} %{F#ffffff}")"
else
win="%{A1:bspc node -f $wid:} ${wname} %{A}"
fi
windowlist="${windowlist} ${win}"
done
}
parse_windows 60
#Result
if [[ ${#windowlist} -gt 300 ]]; then
parse_windows 30
fi
if [[ ${#windowlist} -gt 400 ]]; then
parse_windows 20
fi
echo ${windowlist}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.