]> code.delx.au - monosys/commitdiff
set-font-xfce4-terminal
authorJames Bunton <jamesbunton@delx.net.au>
Mon, 13 Jun 2016 09:43:50 +0000 (19:43 +1000)
committerJames Bunton <jamesbunton@delx.net.au>
Mon, 13 Jun 2016 09:43:50 +0000 (19:43 +1000)
scripts/set-font-xfce4-terminal [new file with mode: 0755]

diff --git a/scripts/set-font-xfce4-terminal b/scripts/set-font-xfce4-terminal
new file mode 100755 (executable)
index 0000000..ba4b19c
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/env python3
+
+import configparser
+import math
+import os
+import sys
+
+def main():
+    action = parse_action()
+
+    if action == "+":
+        update_font_size(get_size_increment)
+    elif action == "-":
+        update_font_size(get_size_decrement)
+    else:
+        update_font_size(lambda _: action)
+
+def parse_action():
+    if len(sys.argv) != 2:
+        print_help_exit()
+
+    action = sys.argv[1]
+    if action != "+" and action != "-" and not action.isdigit():
+        print_help_exit()
+
+    if action.isdigit():
+        action = int(action)
+        if action < 1:
+            print_help_exit()
+
+    return action
+
+def print_help_exit():
+    print("Usage: %s [+|-|22]", file=sys.stderr)
+    sys.exit(1)
+
+def get_size_increment(size):
+    if size >= 40:
+        return math.floor(size / 8) * 8 + 8
+    if size >= 28:
+        return math.floor(size / 4) * 4 + 4
+    if size >= 18:
+        return math.floor(size / 2) * 2 + 2
+    return size + 1
+
+def get_size_decrement(size):
+    if size > 40:
+        return math.ceil(size / 8) * 8 - 8
+    if size > 28:
+        return math.ceil(size / 4) * 4 - 4
+    if size > 18:
+        return math.ceil(size / 2) * 2 - 2
+    return size - 1
+
+def update_font_size(fn):
+    filename = get_config_filename()
+    config = configparser.RawConfigParser()
+    config.optionxform = lambda key: key
+    config.read(filename)
+
+    font_name, old_size = config["Configuration"]["FontName"].rsplit(" ", 1)
+    new_size = fn(int(old_size))
+    config["Configuration"]["FontName"] = "%s %s" % (font_name, new_size)
+
+    print("Updating font size:", old_size, "->", new_size)
+
+    with open(filename+".new", "w") as f:
+        config.write(f)
+    os.rename(filename+".new", filename)
+
+def get_config_filename():
+    xdg_config_dir = os.environ.get("XDG_CONFIG_HOME", None)
+    if not xdg_config_dir:
+        xdg_config_dir = os.path.expanduser("~/.config")
+    return os.path.join(xdg_config_dir, "xfce4", "terminal", "terminalrc")
+
+
+if __name__ == "__main__":
+    main()