Skip to content

Instantly share code, notes, and snippets.

@brisvag
Created July 13, 2020 14:56
Show Gist options
  • Save brisvag/a81cf9bfcf6646782cd652c1ac82476f to your computer and use it in GitHub Desktop.
Save brisvag/a81cf9bfcf6646782cd652c1ac82476f to your computer and use it in GitHub Desktop.
Simple script for showing used/available i3wm bindings
#!/usr/bin/env python3
import re
import sys
import os
i3conf = os.path.expandvars("$XDG_CONFIG_HOME/i3/config")
def get_bindings():
with open(i3conf, "r") as f:
lines = f.readlines()
re_var = re.compile("set\s+(\S+)\s+(\S+)")
re_bind = re.compile("bindsym\s+(?:-\S+\s+)*(\S+)\s+(.*)")
variables = {}
bindings = {}
for line in lines:
if line.startswith('#'):
continue
# variable assignments
if match := re_var.match(line):
variables[match.group(1)] = match.group(2)
# parse for bindings
if match := re_bind.match(line):
# expand variables if any
expanded = match.group(1)
for var, value in variables.items():
expanded = expanded.replace(var, value)
# tokenize bindings
tokens = tuple(expanded.split('+'))
bindings[tokens] = match.group(2)
return bindings
def get_uses(key):
bindings = get_bindings()
uses = []
for bind, cmd in bindings.items():
if key in bind:
uses.append((bind, cmd))
return uses
def is_used(key):
if uses := get_uses(key):
print(f'# Key "{key}" is used {len(uses)} times:')
for use in uses:
print(f'- {"+".join(use[0])} => {use[1]}')
print()
else:
print(f'# Key "{key}" is free!')
print()
if __name__ == '__main__':
keys = sys.argv[1:]
for key in keys:
is_used(key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment