diff options
Diffstat (limited to '.config/fastfetch')
| -rw-r--r-- | .config/fastfetch/confgen.py | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/.config/fastfetch/confgen.py b/.config/fastfetch/confgen.py index 6d6931a..4fc2916 100644 --- a/.config/fastfetch/confgen.py +++ b/.config/fastfetch/confgen.py | |||
| @@ -24,7 +24,15 @@ class Node: | |||
| 24 | font_effect: Optional[str] | 24 | font_effect: Optional[str] |
| 25 | children: list["Node"] | 25 | children: list["Node"] |
| 26 | 26 | ||
| 27 | def __init__(self, module, key, format=None, additional_entries=None, text=None, font_effect=None): | 27 | def __init__( |
| 28 | self, | ||
| 29 | module, | ||
| 30 | key, | ||
| 31 | format=None, | ||
| 32 | additional_entries=None, | ||
| 33 | text=None, | ||
| 34 | font_effect=None, | ||
| 35 | ): | ||
| 28 | self.module = module | 36 | self.module = module |
| 29 | self.key = key | 37 | self.key = key |
| 30 | self.format = format | 38 | self.format = format |
| @@ -34,7 +42,7 @@ class Node: | |||
| 34 | self.children = [] | 42 | self.children = [] |
| 35 | 43 | ||
| 36 | def __str__(self) -> str: | 44 | def __str__(self) -> str: |
| 37 | return f'Node {self.construct_module()}, children: {self.children}' | 45 | return f"Node {self.construct_module()}, children: {self.children}" |
| 38 | 46 | ||
| 39 | def __repr__(self) -> str: | 47 | def __repr__(self) -> str: |
| 40 | return self.__str__() | 48 | return self.__str__() |
| @@ -50,7 +58,9 @@ class Node: | |||
| 50 | return module | key | format | text | additional_entries | 58 | return module | key | format | text | additional_entries |
| 51 | 59 | ||
| 52 | def collect_branch(self) -> list[dict]: | 60 | def collect_branch(self) -> list[dict]: |
| 53 | return [self.construct_module()] + [node.collect_branch() for node in self.children] | 61 | return [self.construct_module()] + [ |
| 62 | node.collect_branch() for node in self.children | ||
| 63 | ] | ||
| 54 | 64 | ||
| 55 | def add_children(self, *nodes: type["Node"]) -> type["Node"]: | 65 | def add_children(self, *nodes: type["Node"]) -> type["Node"]: |
| 56 | self.children += nodes | 66 | self.children += nodes |
| @@ -74,19 +84,21 @@ class Node: | |||
| 74 | self.key = key_prefix + f'{graphics["hbar"]} ' + self.key | 84 | self.key = key_prefix + f'{graphics["hbar"]} ' + self.key |
| 75 | 85 | ||
| 76 | if key_prefix[-1] == graphics["end"]: | 86 | if key_prefix[-1] == graphics["end"]: |
| 77 | key_prefix = key_prefix[:-1] + ' ' | 87 | key_prefix = key_prefix[:-1] + " " |
| 78 | else: | 88 | else: |
| 79 | key_prefix = key_prefix[:-1] + graphics["vbar"] + ' ' | 89 | key_prefix = key_prefix[:-1] + graphics["vbar"] + " " |
| 80 | 90 | ||
| 81 | for i, node in enumerate(self.children): | 91 | for i, node in enumerate(self.children): |
| 82 | if i == len(self.children) - 1: | 92 | if i == len(self.children) - 1: |
| 83 | node.prettify(font_effect, key_prefix + font_effect + graphics["end"]) | 93 | node.prettify(font_effect, key_prefix + font_effect + graphics["end"]) |
| 84 | else: | 94 | else: |
| 85 | node.prettify(font_effect, key_prefix + font_effect + graphics["branch"]) | 95 | node.prettify( |
| 96 | font_effect, key_prefix + font_effect + graphics["branch"] | ||
| 97 | ) | ||
| 86 | 98 | ||
| 87 | 99 | ||
| 88 | CSI = '\033[' | 100 | CSI = "\033[" |
| 89 | SGR = 'm' | 101 | SGR = "m" |
| 90 | BOLD = 1 | 102 | BOLD = 1 |
| 91 | BOLD_CODE = f"{CSI}{BOLD}{SGR}" | 103 | BOLD_CODE = f"{CSI}{BOLD}{SGR}" |
| 92 | 104 | ||
| @@ -137,12 +149,21 @@ modules = { | |||
| 137 | "cursor": Node("cursor", "Cursor"), | 149 | "cursor": Node("cursor", "Cursor"), |
| 138 | "font": Node("font", "Font"), | 150 | "font": Node("font", "Font"), |
| 139 | "theme": Node("theme", "Theme"), | 151 | "theme": Node("theme", "Theme"), |
| 140 | "development": Node("custom", "Development", font_effect=font_effects["development"]), | 152 | "development": Node( |
| 153 | "custom", "Development", font_effect=font_effects["development"] | ||
| 154 | ), | ||
| 141 | "editor": Node("editor", "Editor"), | 155 | "editor": Node("editor", "Editor"), |
| 142 | "git": Node("command", "Git", text="git --version", format="{~12}"), | 156 | "git": Node("command", "Git", text="git --version", format="{~12}"), |
| 143 | "python": Node("command", "Python", text="python --version", format="{~7}"), | 157 | "python": Node("command", "Python", text="python --version", format="{~7}"), |
| 144 | "gcc": Node("command", "GCC", text="gcc --version | head -1 | cut -d ' ' -f 3", format="{}"), | 158 | "gcc": Node( |
| 145 | "clang": Node("command", "Clang", text="clang --version | head -1 | cut -d ' ' -f 3", format="{}"), | 159 | "command", "GCC", text="gcc --version | head -1 | cut -d ' ' -f 3", format="{}" |
| 160 | ), | ||
| 161 | "clang": Node( | ||
| 162 | "command", | ||
| 163 | "Clang", | ||
| 164 | text="clang --version | head -1 | cut -d ' ' -f 3", | ||
| 165 | format="{}", | ||
| 166 | ), | ||
| 146 | "nodejs": Node("command", "NodeJS", text="node --version", format="{}"), | 167 | "nodejs": Node("command", "NodeJS", text="node --version", format="{}"), |
| 147 | "hardware_root": Node("custom", "Hardware", font_effect=font_effects["hardware"]), | 168 | "hardware_root": Node("custom", "Hardware", font_effect=font_effects["hardware"]), |
| 148 | "chassis": Node("chassis", "Chassis", font_effect=font_effects["chassis"]), | 169 | "chassis": Node("chassis", "Chassis", font_effect=font_effects["chassis"]), |
| @@ -159,11 +180,25 @@ modules = { | |||
| 159 | "battery": Node("battery", "Battery"), | 180 | "battery": Node("battery", "Battery"), |
| 160 | "power_adapter": Node("poweradapter", "Power Adapter"), | 181 | "power_adapter": Node("poweradapter", "Power Adapter"), |
| 161 | "display": Node("display", "Display"), | 182 | "display": Node("display", "Display"), |
| 162 | "misc_root": Node("custom", "Miscellaneous information", font_effect=font_effects["miscellaneous"]), | 183 | "misc_root": Node( |
| 184 | "custom", "Miscellaneous information", font_effect=font_effects["miscellaneous"] | ||
| 185 | ), | ||
| 163 | "datetime": Node("datetime", "Date & Time"), | 186 | "datetime": Node("datetime", "Date & Time"), |
| 164 | "browser": Node("command", "Browser", text="eval $(cat /usr/share/applications/$(xdg-mime query default text/html) | grep -Eo 'Exec=([A-Za-z\\/]+)' | head -1 | cut -c 6- | { cat | tr -d '\n'; echo ' --version'; })"), | 187 | "browser": Node( |
| 188 | "command", | ||
| 189 | "Browser", | ||
| 190 | text="""eval $( | ||
| 191 | cat /usr/share/applications/$(xdg-mime query default text/html) | | ||
| 192 | grep -Eo 'Exec=([A-Za-z\\/]+)' | | ||
| 193 | head -1 | cut -c 6- | { cat | tr -d '\n'; echo ' --version'; })""", | ||
| 194 | ), | ||
| 165 | "uptime": Node("uptime", "Uptime"), | 195 | "uptime": Node("uptime", "Uptime"), |
| 166 | "os_age": Node("disk", "OS age", format="{days} days (since {create-time:10})", additional_entries={"folders": "/"}), | 196 | "os_age": Node( |
| 197 | "disk", | ||
| 198 | "OS age", | ||
| 199 | format="{days} days (since {create-time:10})", | ||
| 200 | additional_entries={"folders": "/"}, | ||
| 201 | ), | ||
| 167 | "media": Node("media", "Now playing"), | 202 | "media": Node("media", "Now playing"), |
| 168 | "version": Node("version", "Fastfetch", format="{version}"), | 203 | "version": Node("version", "Fastfetch", format="{version}"), |
| 169 | "network_root": Node("custom", "Networks", font_effect=font_effects["network"]), | 204 | "network_root": Node("custom", "Networks", font_effect=font_effects["network"]), |
| @@ -214,7 +249,7 @@ roots = [ | |||
| 214 | modules["ram"].add_children(modules["swap"]), | 249 | modules["ram"].add_children(modules["swap"]), |
| 215 | modules["battery"].add_children(modules["power_adapter"]), | 250 | modules["battery"].add_children(modules["power_adapter"]), |
| 216 | ), | 251 | ), |
| 217 | modules["display"] | 252 | modules["display"], |
| 218 | ), | 253 | ), |
| 219 | modules["network_root"].add_children( | 254 | modules["network_root"].add_children( |
| 220 | modules["bluetooth"], | 255 | modules["bluetooth"], |
| @@ -246,13 +281,14 @@ def build_config(roots: list[Node]) -> dict[str, type[Any]]: | |||
| 246 | 281 | ||
| 247 | 282 | ||
| 248 | def get_test_response(config: dict[str, type[Any]]) -> list[dict]: | 283 | def get_test_response(config: dict[str, type[Any]]) -> list[dict]: |
| 249 | with NamedTemporaryFile(mode='w', delete_on_close=False, suffix=".jsonc") as tmp_config: | 284 | with NamedTemporaryFile( |
| 285 | mode="w", delete_on_close=False, suffix=".jsonc" | ||
| 286 | ) as tmp_config: | ||
| 250 | json.dump(config, tmp_config) | 287 | json.dump(config, tmp_config) |
| 251 | tmp_config.flush() | 288 | tmp_config.flush() |
| 252 | 289 | ||
| 253 | fastfetch_response = subprocess.run( | 290 | fastfetch_response = subprocess.run( |
| 254 | ["fastfetch", "--config", tmp_config.name, "--json"], | 291 | ["fastfetch", "--config", tmp_config.name, "--json"], capture_output=True |
| 255 | capture_output=True | ||
| 256 | ) | 292 | ) |
| 257 | 293 | ||
| 258 | return json.loads(fastfetch_response.stdout) | 294 | return json.loads(fastfetch_response.stdout) |
| @@ -261,7 +297,11 @@ def get_test_response(config: dict[str, type[Any]]) -> list[dict]: | |||
| 261 | def filter_roots(roots: list[Node], data: list[dict]) -> list[Node]: | 297 | def filter_roots(roots: list[Node], data: list[dict]) -> list[Node]: |
| 262 | skip_modules = ["Custom"] | 298 | skip_modules = ["Custom"] |
| 263 | for response_object in data: | 299 | for response_object in data: |
| 264 | if response_object.get("error") and response_object.get("type") not in skip_modules or response_object.get("result") == []: | 300 | if ( |
| 301 | response_object.get("error") | ||
| 302 | and response_object.get("type") not in skip_modules | ||
| 303 | or response_object.get("result") == [] | ||
| 304 | ): | ||
| 265 | for root in roots: | 305 | for root in roots: |
| 266 | root.remove_module(response_object.get("type").lower()) | 306 | root.remove_module(response_object.get("type").lower()) |
| 267 | return roots | 307 | return roots |
| @@ -275,9 +315,9 @@ def main(): | |||
| 275 | root.prettify() | 315 | root.prettify() |
| 276 | final_config = build_config(filtered_roots) | 316 | final_config = build_config(filtered_roots) |
| 277 | fastfetch_config = Path.home() / ".config" / "fastfetch" / "config_generated.jsonc" | 317 | fastfetch_config = Path.home() / ".config" / "fastfetch" / "config_generated.jsonc" |
| 278 | with open(fastfetch_config, 'w') as config_file: | 318 | with open(fastfetch_config, "w") as config_file: |
| 279 | json.dump(final_config, config_file) | 319 | json.dump(final_config, config_file) |
| 280 | 320 | ||
| 281 | 321 | ||
| 282 | if __name__ == '__main__': | 322 | if __name__ == "__main__": |
| 283 | main() | 323 | main() |
