Browse Source

rofi_multicmd: multicmd - rofi version

master
Von Random 1 month ago
parent
commit
ce5e581b14
1 changed files with 59 additions and 0 deletions
  1. 59
    0
      rofi_multicmd

+ 59
- 0
rofi_multicmd View File

@@ -0,0 +1,59 @@
1
+#!/usr/bin/env python3
2
+#pylint: disable=C0111
3
+# config example (cmd_pre and cmd_post are optional):
4
+# ---
5
+# name: test
6
+# cmd_pre:
7
+#   - [/usr/bin/notify_send, 'I am precmd1!']
8
+#   - [/usr/bin/notify_send, 'I am precmd2!']
9
+# multicmd:
10
+#   selection1:
11
+#     - [/usr/bin/notify-send, 'I am command1 for selection1!']
12
+#     - [/usr/bin/notify-send, 'I am command2 for selection1!']
13
+#   selection2:
14
+#     - [/usr/bin/notify-send, 'I am command1 for selection2!']
15
+# cmd_post:
16
+#   - [/usr/bin/notify_send, 'I am postcmd1!']
17
+#   - [/usr/bin/notify_send, 'I am postcmd2!']
18
+# ---
19
+import subprocess
20
+import sys
21
+import yaml
22
+
23
+
24
+def run_cmd(cmd, stdin=subprocess.PIPE, data=None):
25
+    proc = subprocess.Popen(
26
+        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=stdin
27
+    )
28
+    stdout, stderr = (i.strip() for i in proc.communicate(data))
29
+    if proc.returncode:
30
+        err_msg = ['ERROR (' + str(proc.returncode) + '): ' + ' '.join(cmd)]
31
+        if stdout:
32
+            err_msg.append('out:\n' + stdout.decode('UTF-8'))
33
+        if stderr:
34
+            err_msg.append('err:\n' + stderr.decode('UTF-8'))
35
+        sys.stderr.write('\n'.join(err_msg) + '\n')
36
+    return stdout
37
+
38
+
39
+def run_multicmd(cmds):
40
+    for cmd in cmds:
41
+        run_cmd(cmd)
42
+
43
+
44
+def main():
45
+    config_path = sys.argv[1]
46
+    with open(config_path, mode='r') as config:
47
+        conf = yaml.load(config.read())
48
+    if len(sys.argv) < 3:
49
+        print('\n'.join(conf['multicmd']))
50
+    else:
51
+        selection = sys.argv[2]
52
+        if not selection in conf['multicmd']:
53
+            sys.exit(1)
54
+        run_multicmd(conf.get('cmd_pre', tuple()))
55
+        run_multicmd(conf['multicmd'][selection])
56
+        run_multicmd(conf.get('cmd_post', tuple()))
57
+
58
+if __name__ == '__main__':
59
+    main()

Loading…
Cancel
Save