]> code.delx.au - monosys/blob - set-font-xfce4-terminal
xfce4-genmon-script: use long to parse zfs arc_used
[monosys] / set-font-xfce4-terminal
1 #!/usr/bin/env python3
2
3 import configparser
4 import math
5 import os
6 import sys
7
8 def main():
9 action = parse_action()
10
11 if action == "+":
12 update_font_size(get_size_increment)
13 elif action == "-":
14 update_font_size(get_size_decrement)
15 else:
16 update_font_size(lambda _: action)
17
18 def parse_action():
19 if len(sys.argv) != 2:
20 print_help_exit()
21
22 action = sys.argv[1]
23 if action != "+" and action != "-" and not action.isdigit():
24 print_help_exit()
25
26 if action.isdigit():
27 action = int(action)
28 if action < 1:
29 print_help_exit()
30
31 return action
32
33 def print_help_exit():
34 print("Usage: %s [+|-|22]", file=sys.stderr)
35 sys.exit(1)
36
37 def get_size_increment(size):
38 if size >= 40:
39 return math.floor(size / 8) * 8 + 8
40 if size >= 28:
41 return math.floor(size / 4) * 4 + 4
42 if size >= 18:
43 return math.floor(size / 2) * 2 + 2
44 return size + 1
45
46 def get_size_decrement(size):
47 if size > 40:
48 return math.ceil(size / 8) * 8 - 8
49 if size > 28:
50 return math.ceil(size / 4) * 4 - 4
51 if size > 18:
52 return math.ceil(size / 2) * 2 - 2
53 return size - 1
54
55 def update_font_size(fn):
56 filename = get_config_filename()
57 config = configparser.RawConfigParser()
58 config.optionxform = lambda key: key
59 config.read(filename)
60
61 font_name, old_size = config["Configuration"]["FontName"].rsplit(" ", 1)
62 new_size = fn(int(old_size))
63 config["Configuration"]["FontName"] = "%s %s" % (font_name, new_size)
64
65 print("Updating font size:", old_size, "->", new_size)
66
67 with open(filename+".new", "w") as f:
68 config.write(f)
69 os.rename(filename+".new", filename)
70
71 def get_config_filename():
72 xdg_config_dir = os.environ.get("XDG_CONFIG_HOME", None)
73 if not xdg_config_dir:
74 xdg_config_dir = os.path.expanduser("~/.config")
75 return os.path.join(xdg_config_dir, "xfce4", "terminal", "terminalrc")
76
77
78 if __name__ == "__main__":
79 main()