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

bspwm-node-flags: script, module & preview #361

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
41 changes: 41 additions & 0 deletions polybar-scripts/bspwm-node-flags/README.md
Original file line number Diff line number Diff line change
@@ -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

```
30 changes: 30 additions & 0 deletions polybar-scripts/bspwm-node-flags/bspwm-node-flags.fish
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions polybar-scripts/bspwm-node-flags/bspwm-node-flags.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[module/bspwm-node-flags]
exec = $HOME/.config/polybar/scripts/bspwm-node-flags
format-foreground = '#fabd2f'
tail = true
type = custom/script
36 changes: 36 additions & 0 deletions polybar-scripts/bspwm-node-flags/bspwm-node-flags.py
Original file line number Diff line number Diff line change
@@ -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('')
11 changes: 11 additions & 0 deletions polybar-scripts/bspwm-node-flags/bspwn-node-flags
Original file line number Diff line number Diff line change
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.