Skip to content

Instantly share code, notes, and snippets.

@emanuele6
Last active November 21, 2024 06:00
Show Gist options
  • Select an option

  • Save emanuele6/31bec2a0e1b61d0ba8b0434409f13e38 to your computer and use it in GitHub Desktop.

Select an option

Save emanuele6/31bec2a0e1b61d0ba8b0434409f13e38 to your computer and use it in GitHub Desktop.
bspwm tree visualiser
#!/bin/sh --
# \
exec jq -Crf "$0" -- "$@"
def children:
objects |
.path as $p |
(.firstChild | objects | .path = $p + "/1"),
(.secondChild | objects | .path = $p + "/2") |
select(has("id"));
def isInnerNode:
.firstChild != null;
def isNonVacantLeaf:
isInnerNode or .vacant | not;
def pathString:
"@" + (.path // "/");
def node:
"\t\(.id) \(
"[color=\(
. as $node |
[ limit(2; recurse(children) | select(isNonVacantLeaf)) ] |
length |
if . == 0 then
if $node.client != null then
"red" # vacant window
else
"grey" # fully vacant inner node
end
elif . == 1 then
if $node.client != null then
"green" # window
elif isempty($node | children) then
"blue" # receptacle
else
"yellow" # inner node with single non-vacant
end
else
empty # normal inner node
end
)]",
"[label=\"\(
[
pathString,
(
select(
[ children ] |
length != 0 and
all(any(recurse(children); isNonVacantLeaf))
) |
"\(
if .splitType == "vertical" then
"V"
else
"H"
end
):\(.splitRatio)"
),
.id,
(
select(.client != null) |
(
select(.vacant | not) |
.client.tiledRectangle |
"\(.width)x\(.height)+\(.x)+\(.y)"
),
(
.client |
"\(.className):\(.instanceName)"
)
) |
tostring |
gsub("[\\\\\"]"; "\\" + .)
] |
join("\\n")
)\"]",
"-> \(children.id | values)"
);";
"digraph BSPTree {",
(
objects.root |
objects |
recurse(children) |
node
),
"}"
#!/bin/sh --
if [ "$#" -ge 2 ]; then
printf 'Too many arguments.\n' >&2
exit 2
fi
bspc query -T -d ${1+"$1"} |
awk '
NR == 1 && "BSPSHOWTREE_FLOAT" in ENVIRON {
system("bspc rule -a \\* -o state=floating")
}
{ print | "bspdeskjson2dot | dot -Tx11" }
END { exit NR == 0 }
'
#!/bin/sh --
case $# in
0)
: "${BSPTREEUPDATEPNG_OUTFILE:=/tmp/bsptree_$$.png}"
;;
1)
if [ -z "$1" ]; then
printf 'Invalid empty argument.\n' >&2
exit 2
fi
BSPTREEUPDATEPNG_OUTFILE=$1
;;
*)
printf 'Too many arguments.\n' >&2
exit 2
esac
while
tree=$(bspc query -T -d ) &&
printf '%s\n' "$tree" |
bspdeskjson2dot |
dot -Tpng -o "$BSPTREEUPDATEPNG_OUTFILE" &&
bspc subscribe -c 1 desktop node > /dev/null
do
:
done
# Now, you can open the output file in an image viewer and see the tree
# update live! :D
# Visualise the bspwm tree of the focused desktop in the floating window
super + ctrl + t
BSPSHOWTREE_FLOAT= bspshowtree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment