-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpokefetch.py
More file actions
185 lines (167 loc) · 5.64 KB
/
pokefetch.py
File metadata and controls
185 lines (167 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env python3
import re
from collections import Counter
import json
import os
class PokeFastFetch:
def __init__(self, offset: int, cached_path: str, ff_config_path: str) -> None:
self._offset = offset
self._cached_path = cached_path
self._ff_config_path = ff_config_path
self._pokemon = None
self._pokemon_lines = None
self._ff_lines = 0
self._results_map = None
self._color_fmt = None
self._extract_colors()
self._ff_config = {}
self._read_ff_config()
self._write_ff_config()
@staticmethod
def _quantize_color(rgb: tuple, step: int = 8) -> tuple:
return tuple((c // step) * step for c in rgb)
def _extract_colors(self) -> None:
with open(self._cached_path) as f:
pokemon = f.read().strip()
pokemon_lines = len(pokemon.split("\n"))
ff_lines = pokemon_lines + OFFSET
pattern = r"(?:38|48);2;(\d{1,3});(\d{1,3});(\d{1,3})"
results = re.findall(pattern, pokemon)
results = [tuple(map(int, c)) for c in results]
results = [
(r, g, b)
for (r, g, b) in results
if not (all(c < 90 for c in (r, g, b)) or all(c > 180 for c in (r, g, b)))
]
if len(results) == 0:
raise ValueError("ANSII parsing failed, zero results found")
binned = [self._quantize_color(c) for c in results]
results_map = Counter(binned)
r, g, b = results_map.most_common(1)[0][0]
color_fmt = f"38;2;{r};{g};{b}"
self._pokemon = pokemon
self._pokemon_lines = pokemon_lines
self._ff_lines = ff_lines
self._results_map = results_map
self._color_fmt = color_fmt
def _read_ff_config(self) -> None:
with open(self._ff_config_path) as f:
self._ff_config = json.load(f)
self._ff_config["modules"] = ["break", "title"]
def _write_ff_config(self) -> None:
modules = [
{
"type": "users",
"key": "users ",
"keyColor": self._color_fmt,
},
{
"type": "de",
"key": "de ",
"keyColor": self._color_fmt,
},
{
"type": "shell",
"key": "shell ",
"keyColor": self._color_fmt,
},
{
"type": "terminal",
"key": "term ",
"keyColor": self._color_fmt,
},
"break",
{
"type": "dns",
"key": "dns ",
"keyColor": self._color_fmt,
},
{
"type": "localip",
"key": "ipv4 ",
"keyColor": self._color_fmt,
"format": "{ifname}: {ipv4}",
},
{
"type": "wifi",
"key": "wifi ",
"keyColor": self._color_fmt,
"format": "{ssid} ({signal-quality}) - {protocol}",
},
"break",
{
"type": "battery",
"key": "bat ",
"keyColor": self._color_fmt,
"format": "{capacity} [{status}] ({cycle-count})",
},
{
"type": "disk",
"key": "disk ",
"keyColor": self._color_fmt,
},
{
"type": "memory",
"key": "memory",
"keyColor": self._color_fmt,
},
{
"type": "gpu",
"key": "gpu ",
"keyColor": self._color_fmt,
},
{
"type": "cpu",
"key": "cpu ",
"keyColor": self._color_fmt,
},
"break",
{
"type": "uptime",
"format": "{?days}{days}d {?}{hours}h {minutes}m",
"key": "uptime",
"keyColor": self._color_fmt,
},
{
"type": "packages",
"key": "pkgs ",
"keyColor": self._color_fmt,
},
{
"type": "host",
"key": "host ",
"format": "{vendor} {family}",
"keyColor": self._color_fmt,
},
{
"type": "kernel",
"key": "kernel",
"keyColor": self._color_fmt,
},
{
"type": "os",
"key": "os ",
"keyColor": self._color_fmt,
},
]
if self._ff_config.get("display", {}).get("color", {}):
self._ff_config["display"]["color"]["title"] = self._color_fmt
self._ff_config["display"]["color"]["title"]
for i in range(self._ff_lines):
try:
module = modules.pop()
if self._ff_lines - i <= 2 and module == "break":
while module == "break":
module = modules.pop()
self._ff_config["modules"].append(module)
except IndexError:
break
self._ff_config["modules"].append("break")
with open(self._ff_config_path, "w") as f:
json.dump(self._ff_config, f, indent=1)
if __name__ == "__main__":
HOMEDIR = os.getenv("HOME")
OFFSET = -3 # offset from fastfetch config preamble to module entries
CACHED_PATH = rf"{HOMEDIR}/.cache/pokemon.txt"
FF_CONFIG_PATH = rf"{HOMEDIR}/.config/fastfetch/config.jsonc"
PokeFastFetch(OFFSET, CACHED_PATH, FF_CONFIG_PATH)