Skip to content

Instantly share code, notes, and snippets.

@v6ak
Last active November 23, 2023 20:22
Show Gist options
  • Save v6ak/550be69f5b9af5ed02c53b80a9e437dc to your computer and use it in GitHub Desktop.
Save v6ak/550be69f5b9af5ed02c53b80a9e437dc to your computer and use it in GitHub Desktop.
dmenu-wrapper.s
#!/bin/env python3
import sys
import re
from typing import Tuple
def remove_suffix(s, suffix):
if s.endswith(suffix):
return s[0:-len(suffix)]
else:
return None
# lower number => higher priority
GLOBAL_PRIORITY_LAYOUT = -3
GLOBAL_PRIORITY_DOM0 = 0
GLOBAL_PRIORITY_DVM = 0
GLOBAL_PRIORITY_QUBE = 0
APP_PRIORITIES={
'Qube Settings': 1,
'Start qube': -1,
}
# TODO: DVM: recent qube versions on top
# TODO: DVM: fedora on top
# (global_priority, qube, is_not_dvm, app_priority, app_name)
def keyfunc(name: str) -> Tuple[int, str, bool, int, str]:
without_layout_suffix = remove_suffix(name, ' (layout)')
if without_layout_suffix is not None:
return (GLOBAL_PRIORITY_LAYOUT, 'dom0', True, 0, name)
parts = name.split(': ', 1)
if len(parts) == 1:
return (GLOBAL_PRIORITY_DOM0, 'dom0', True, 0, name)
else:
[vm_and_suffix, app] = parts
without_dvm_suffix = remove_suffix(vm_and_suffix, ' (dvm)')
if without_dvm_suffix is None:
vm = vm_and_suffix
is_dvm = False
global_priority = GLOBAL_PRIORITY_QUBE
else:
vm = without_dvm_suffix
is_dvm = True
global_priority = GLOBAL_PRIORITY_DVM
app_priority = APP_PRIORITIES.get(app, 0)
return (0, vm, not is_dvm, app_priority, app)
inp = list(map(lambda x: x[:-1], sys.stdin.readlines()))
out = sorted(inp, key=keyfunc)
print("\n".join(out))
#!/bin/bash
BASE="$(dirname "$(realpath "$0")")"
$BASE/dmenu-sort.py | dmenu "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment