From 58a1e5f1be575feb92344950c31cc322ab65f34b Mon Sep 17 00:00:00 2001
From: Siarhei Siniak <siarheisiniak@gmail.com>
Date: Mon, 30 Sep 2024 22:27:35 +0300
Subject: [PATCH] [~] Refactor

---
 dotfiles/.local/bin/commands | 91 ++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/dotfiles/.local/bin/commands b/dotfiles/.local/bin/commands
index a026acd..aa53b44 100755
--- a/dotfiles/.local/bin/commands
+++ b/dotfiles/.local/bin/commands
@@ -2852,6 +2852,95 @@ def suspend_timer(argv):
     print("suspend computer at %s" % t1.isoformat())
     subprocess.check_call(["systemctl", "suspend"]);
 
+def gnome_shortcuts(argv):
+    parser = optparse.OptionParser()
+    parser.add_option(
+        '-a', '--add',
+        action='store_true',
+        default=None,
+    )
+    parser.add_option(
+        '-l', '--list',
+        action='store_true',
+        default=None,
+    )
+
+    options, args = parser.parse_args(argv)
+
+    def commands_ids() -> list[str]:
+        t1 = json.loads(subprocess.check_output([
+            'gsettings', 'get', 'org.gnome.settings-daemon.plugins.media-keys',
+            'custom-keybindings',
+        ]).decode('utf-8').replace('\'', '"',))
+
+        return t1
+
+    def add_command(name, command, binding):
+        command_id = len(commands_ids())
+
+        for cmd in [
+            (
+                'gsettings', 'set', 'org.gnome.settings-daemon.plugins.media-keys',
+                'custom-keybindings', '[%s]' % ','.join([
+                    "'%s'" % \
+                        (
+                            '/org/gnome/settings-daemon/plugins/media-keys'
+                            '/custom-keybindings/custom%d/'
+                        ) % o
+                    for o in range(command_id + 1)
+                ]),
+            ),
+            (
+                'gsettings', 'set',
+                (
+                    'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding'
+                    ':/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom%d/'
+                ) % command_id,
+                'name', name,
+            ),
+            (
+                'gsettings', 'set',
+                (
+                    'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding'
+                    ':/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom%d/'
+                ) % command_id,
+                'command', command,
+            ),
+            (
+                'gsettings', 'set',
+                (
+                    'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding'
+                    ':/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/custom%d/'
+                ) % command_id,
+                'binding', binding,
+            ),
+        ]:
+            subprocess.check_call(cmd)
+
+    if options.list:
+        t1 = commands_ids()
+
+        t2 = [
+            {
+                k : json.loads(subprocess.check_output([
+                    'gsettings', 'get',
+                    (
+                        'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:%s'
+                    ) % o,
+                    k,
+                ]).decode('utf-8').replace('\'', '"',))
+                for k in ['name', 'binding', 'command']
+            }
+            for o in t1
+        ]
+
+        pprint.pprint(t2)
+    elif options.add:
+        add_command(*args)
+    else:
+        raise NotImplementedError
+
+
 def socat_ssh(argv):
     parser = optparse.OptionParser()
     parser.add_option(
@@ -3430,6 +3519,8 @@ def commands_cli():
             share_wifi(sys.argv[2:])
         elif sys.argv[1] == 'socat-ssh':
             socat_ssh(sys.argv[2:])
+        elif sys.argv[1] == 'gnome-shortcuts':
+            gnome_shortcuts(sys.argv[2:])
         elif sys.argv[1] == 'sway_sock':
             print(sway_sock())
         elif sys.argv[1] == 'suspend-timer':