diff --git a/polybar-scripts/bspwm-node-flags/README.md b/polybar-scripts/bspwm-node-flags/README.md new file mode 100644 index 00000000..1d75d219 --- /dev/null +++ b/polybar-scripts/bspwm-node-flags/README.md @@ -0,0 +1,41 @@ +# bspwm-node-flags + +This script was originally written in [`fish`](https://fishshell.com/), so a +fish script has been included, but I have rewritten it in `python` so that it's +more shell agnostic - `python` is more common than `fish`. This module displays +a bookmark icon, alongside a single character to be used in conjunction with +[`bspwm`](https://github.com/baskerville/bspwm) to show flags of the focused +node. + +The following code can be modified if you'd prefer different characters for the +name of the flags. I have chosen `S` for sticky, `X` for lock, `M` for marked & +`P` for private. There are other flags on the node, but these are all that are +currently coded. + +```python +flag_states = { + 'S': node_tree['sticky'], + 'X': node_tree['locked'], + 'M': node_tree['marked'], + 'P': node_tree['private'] +} +``` + +`chmod +x $HOME/.config/polybar/scripts/bspwm-node-flags` + +`chmod +x $HOME/.config/polybar/scripts/bspwm-node-flags.py` + +## Preview + +![bspwm-node-flags](bspwm-node-flags.gif) + +## Example module + +```ini +[module/bspwm-node-flags] +exec = $HOME/.config/polybar/scripts/bspwm-node-flags +format-foreground = '#fabd2f' +tail = true +type = custom/script + +``` diff --git a/polybar-scripts/bspwm-node-flags/bspwm-node-flags.fish b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.fish new file mode 100755 index 00000000..0c9b298e --- /dev/null +++ b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.fish @@ -0,0 +1,30 @@ +#! /usr/bin/env fish + +bspc subscribe node | while read -a msg + # A couple of the events that are output by `bspc subscribe node` are + # `node_focus` and `node_stack` - we're only worried about the node being in + # focus, or a flag being set on the current node. + if test "$msg[1]" = 'node_focus'; or test "$msg[1]" = 'node_flag' + # We're only concerned with displaying these flags. + set flag_states (bspc query -T -n focused \ + | jq -r '.sticky,.locked,.marked,.private') + # Filter out false flags, and prepend a character to act as the name. + set flags (string match -ie true \ + 'S:'$flag_states[1] 'X:'$flag_states[2] \ + 'M:'$flag_states[3] 'P:'$flag_states[4]) + + # If we've matched the string 'true' above, $flags will not be empty. + if string length -q -- $flags + set output ' ' + for flag in $flags + # The name is the first character before the ':'. + set name (string split ':' $flag)[1] + set output "$output$name" + end + echo $output + else + # When we've got nothing, we want to clear the output. + echo '' + end + end +end diff --git a/polybar-scripts/bspwm-node-flags/bspwm-node-flags.ini b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.ini new file mode 100644 index 00000000..2a61fa5f --- /dev/null +++ b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.ini @@ -0,0 +1,5 @@ +[module/bspwm-node-flags] +exec = $HOME/.config/polybar/scripts/bspwm-node-flags +format-foreground = '#fabd2f' +tail = true +type = custom/script diff --git a/polybar-scripts/bspwm-node-flags/bspwm-node-flags.py b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.py new file mode 100755 index 00000000..afc83fb5 --- /dev/null +++ b/polybar-scripts/bspwm-node-flags/bspwm-node-flags.py @@ -0,0 +1,36 @@ +#! usr/bin/env python + +import argparse +import json + +argparser = argparse.ArgumentParser(description = 'Display `bspwm` node flags') +argparser.add_argument( + 'node_tree', + type = str, + help = 'JSON output of `bspc query -T -n focused`', + default = '' +) +args = argparser.parse_args() + +# Parse node_tree JSON output. +node_tree = json.loads(args.node_tree) + +# We're only concerned with displaying these flags. +flag_states = { + 'S': node_tree['sticky'], + 'X': node_tree['locked'], + 'M': node_tree['marked'], + 'P': node_tree['private'] +} + +# Filter out false flags. +if list(flag_states.values()).count(True) > 0: + flags = [ ' ' ] + for flag, state in flag_states.items(): + if state: + flags.append(flag) + + print(''.join(flags)) +else: + # When we've got nothing, we want to clear the output. + print('') diff --git a/polybar-scripts/bspwm-node-flags/bspwn-node-flags b/polybar-scripts/bspwm-node-flags/bspwn-node-flags new file mode 100644 index 00000000..7e8219da --- /dev/null +++ b/polybar-scripts/bspwm-node-flags/bspwn-node-flags @@ -0,0 +1,11 @@ +#! /usr/bin/env sh + +bspc subscribe node | while read -a msg; do + # A couple of the events that are output by `bspc subscribe node` are + # `node_focus` and `node_stack` - we're only worried about the node being in + # focus, or a flag being set on the current node. + if [[ "$msg" == 'node_focus' || "$msg" == 'node_flag' ]]; then + node_tree=$(bspc query -T -n focused) + python $HOME/.config/polybar/scripts/bspwm-node-flags.py "$node_tree" + fi +done diff --git a/polybar-scripts/bspwm-node-flags/bspwn-node-flags.gif b/polybar-scripts/bspwm-node-flags/bspwn-node-flags.gif new file mode 100644 index 00000000..ca6b43fe Binary files /dev/null and b/polybar-scripts/bspwm-node-flags/bspwn-node-flags.gif differ