Created
May 24, 2020 09:54
-
-
Save dufferzafar/36f133dac5ca11ff3bb38d5071169463 to your computer and use it in GitHub Desktop.
Rofi modi for KDE services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| import sys | |
| import glob | |
| import itertools | |
| import subprocess | |
| from configparser import ConfigParser | |
| KS5_DIR = "/usr/share/kservices5" | |
| def launch(entry): | |
| # Any better way to "log" ? | |
| with open("/tmp/a.txt", "w") as w: | |
| w.write(repr(entry)) | |
| w.write("\n") | |
| # I once ran this, and everything FROZE! | |
| # Had to go into system shell and pkill rofi | |
| # subprocess.call([ | |
| # "xmessage", | |
| # entry | |
| # ]) | |
| def list_entries(): | |
| def read_desktop(file): | |
| cfg = ConfigParser(strict=False, interpolation=None) | |
| with open(file) as fp: | |
| # Some files are missing | |
| cfg.read_file(itertools.chain(['[global]'], fp), source=file) | |
| return cfg | |
| for file in glob.glob(f"{KS5_DIR}/*.desktop"): | |
| de = read_desktop(file)['Desktop Entry'] | |
| # For now, only focus on services | |
| if de['type'] != 'Service' or 'Exec' not in de: | |
| continue | |
| name = de['Name'] | |
| comment = '' | |
| if 'Comment' in de: | |
| comment = f"<span weight='light' size='small'><i>({de['Comment']})</i></span>" | |
| icon = de.get('Icon', '') | |
| exec = de['Exec'] | |
| # For some bizarre reason, this works correctly! | |
| # print(f"{comment}\x00markup-rows\x1ftrue") | |
| # But when I do this, it fails - as in, the comment loses its markup | |
| print(f"{name} - {comment}\x00markup-rows\x1ftrue") | |
| # Neither of these work | |
| # print(f"<span weight='normal' size='medium'>{name}</span>\x00markup-rows\x1ftrue") | |
| # print(f"<span weight='light' size='small'><i>({name})</i></span>\x00markup-rows\x1ftrue") | |
| # print(f"\0message\x1fSpecial <b>bold</b> message\n") | |
| # name = f"<span weight='light' size='small'><i>{name}</i></span>" | |
| # message\x1f<i>({de['Comment']})</i> | |
| # print(f"{name}\0message\x1f{comment}\0icon\x1f{icon}\0exec\x1f{exec}") | |
| # print(f"{name} - {exec}\0icon\x1f{icon}") | |
| # print(f"{name}\x00markup-rows\x1ftrue") | |
| if __name__ == "__main__": | |
| args = sys.argv | |
| if len(args) > 1: | |
| launch(args[1]) | |
| else: | |
| list_entries() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment