-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeParser.py
More file actions
90 lines (73 loc) · 3.08 KB
/
Copy paththemeParser.py
File metadata and controls
90 lines (73 loc) · 3.08 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
import os
import re
class ThemeEngine:
def __init__(self, theme_folder="themes"):
self.theme_folder = theme_folder
self.colors = {}
self.RESET = "\033[0m"
def hex_to_ansi(self, hex_code, is_background=False):
"""literally hex -> ANSI"""
hex_code = hex_code.strip().strip('"').strip("'").lstrip('#')
if len(hex_code) != 6:
return "\033[49m" if is_background else "\033[39m"
r = int(hex_code[0:2], 16)
g = int(hex_code[2:4], 16)
b = int(hex_code[4:6], 16)
layer = "48" if is_background else "38"
return f"\033[{layer};2;{r};{g};{b}m"
def get_available_themes(self):
"""returns all .theme's in themes folder"""
if not os.path.exists(self.theme_folder):
os.makedirs(self.theme_folder)
return []
return [f.replace('.theme', '') for f in os.listdir(self.theme_folder) if f.endswith('.theme')]
def load_theme(self, theme_name, tbg=False):
"""creates a dict w/ colors in .theme"""
self.tbg = tbg
file_path = os.path.join(self.theme_folder, f"{theme_name}.theme")
if not os.path.exists(file_path):
print(f"Theme '{theme_name}' not found.")
return False
self.colors = {}
pattern = re.compile(r'^theme\[([^\]]+)\]=["\']?(#[A-Fa-f0-9]{6})["\']?')
with open(file_path, 'r') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#'):
continue
match = pattern.match(line)
if match:
key = match.group(1)
hex_val = match.group(2)
is_bg = key.endswith('_bg')
self.colors[key] = self.hex_to_ansi(hex_val, is_background=is_bg)
if not tbg:
self.RESET = self.get("main_bg" + "main_fg")
return True
def get(self, key):
"""gets color for key"""
if self.tbg and key == "main_bg":
return ""
return self.colors.get(key, "") if key not in ["r",""] else {"r":self.RESET,"":""}[key]
def newP(self):
keys = [
"main_bg", "main_fg", "title", "hi_fg",
"selected_bg", "selected_fg", "inactive_fg",
"proc_misc",
"cpu_box", "mem_box", "net_box", "proc_box",
"div_line",
"temp_start", "temp_mid", "temp_end",
"cpu_start", "cpu_mid", "cpu_end",
"free_start", "free_mid", "free_end",
"cached_start", "cached_mid", "cached_end",
"available_start", "available_mid", "available_end",
"used_start", "used_mid", "used_end",
"download_start", "download_mid", "download_end",
"upload_start", "upload_mid", "upload_end",
"r", ""
]
return {k: self.get(k) for k in keys}
def ls(self):
if not os.path.exists("themes"):
return []
return [f[:-6] for f in os.listdir("themes") if f.endswith(".theme")]