|
@@ -10,13 +10,12 @@ from subprocess import check_output
|
|
from tempfile import TemporaryDirectory
|
|
from tempfile import TemporaryDirectory
|
|
|
|
|
|
import pkg_resources
|
|
import pkg_resources
|
|
-from hyfetch.color_util import color
|
|
|
|
from typing_extensions import Literal
|
|
from typing_extensions import Literal
|
|
|
|
|
|
|
|
+from hyfetch.color_util import color
|
|
from .constants import GLOBAL_CFG
|
|
from .constants import GLOBAL_CFG
|
|
from .presets import ColorProfile
|
|
from .presets import ColorProfile
|
|
|
|
|
|
-
|
|
|
|
RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
|
|
RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
|
|
|
|
|
|
|
|
|
|
@@ -38,6 +37,26 @@ def normalize_ascii(asc: str) -> str:
|
|
return '\n'.join(line + ' ' * (w - ascii_size(line)[0]) for line in asc.split('\n'))
|
|
return '\n'.join(line + ' ' * (w - ascii_size(line)[0]) for line in asc.split('\n'))
|
|
|
|
|
|
|
|
|
|
|
|
+def fill_starting(asc: str) -> str:
|
|
|
|
+ """
|
|
|
|
+ Fill the missing starting placeholders.
|
|
|
|
+
|
|
|
|
+ E.g. "${c1}...\n..." -> "${c1}...\n${c1}..."
|
|
|
|
+ """
|
|
|
|
+ new = []
|
|
|
|
+ last = ''
|
|
|
|
+ for line in asc.split('\n'):
|
|
|
|
+ new.append(last + line)
|
|
|
|
+
|
|
|
|
+ # Line has color placeholders
|
|
|
|
+ matches = RE_NEOFETCH_COLOR.findall(line)
|
|
|
|
+ if len(matches) > 0:
|
|
|
|
+ # Get the last placeholder for the next line
|
|
|
|
+ last = matches[-1]
|
|
|
|
+
|
|
|
|
+ return '\n'.join(new)
|
|
|
|
+
|
|
|
|
+
|
|
@dataclass
|
|
@dataclass
|
|
class ColorAlignment:
|
|
class ColorAlignment:
|
|
mode: Literal['horizontal', 'vertical', 'custom']
|
|
mode: Literal['horizontal', 'vertical', 'custom']
|
|
@@ -45,13 +64,35 @@ class ColorAlignment:
|
|
# custom_colors[ascii color index] = unique color index in preset
|
|
# custom_colors[ascii color index] = unique color index in preset
|
|
custom_colors: dict[int, int] = ()
|
|
custom_colors: dict[int, int] = ()
|
|
|
|
|
|
|
|
+ # Foreground/background ascii color index
|
|
|
|
+ fore_back: tuple[int, int] = ()
|
|
|
|
+
|
|
def recolor_ascii(self, asc: str, preset: ColorProfile) -> str:
|
|
def recolor_ascii(self, asc: str, preset: ColorProfile) -> str:
|
|
"""
|
|
"""
|
|
Use the color alignment to recolor an ascii art
|
|
Use the color alignment to recolor an ascii art
|
|
|
|
|
|
:return Colored ascii, Uncolored lines
|
|
:return Colored ascii, Uncolored lines
|
|
"""
|
|
"""
|
|
- if self.mode in ['horizontal', 'vertical']:
|
|
|
|
|
|
+ asc = fill_starting(asc)
|
|
|
|
+
|
|
|
|
+ if self.fore_back and self.mode in ['horizontal', 'vertical']:
|
|
|
|
+ fore, back = self.fore_back
|
|
|
|
+
|
|
|
|
+ # Replace foreground colors
|
|
|
|
+ asc = asc.replace(f'${{c{fore}}}', color('&0' if GLOBAL_CFG.is_light else '&f'))
|
|
|
|
+ lines = asc.split('\n')
|
|
|
|
+
|
|
|
|
+ # Add new colors
|
|
|
|
+ if self.mode == 'horizontal':
|
|
|
|
+ colors = preset.with_length(len(lines))
|
|
|
|
+ asc = '\n'.join([l.replace(f'${{c{back}}}', colors[i].to_ansi()) + color('&r') for i, l in enumerate(lines)])
|
|
|
|
+ else:
|
|
|
|
+ raise NotImplementedError()
|
|
|
|
+
|
|
|
|
+ # Remove existing colors
|
|
|
|
+ asc = re.sub(RE_NEOFETCH_COLOR, '', asc)
|
|
|
|
+
|
|
|
|
+ elif self.mode in ['horizontal', 'vertical']:
|
|
# Remove existing colors
|
|
# Remove existing colors
|
|
asc = re.sub(RE_NEOFETCH_COLOR, '', asc)
|
|
asc = re.sub(RE_NEOFETCH_COLOR, '', asc)
|
|
lines = asc.split('\n')
|
|
lines = asc.split('\n')
|
|
@@ -67,28 +108,9 @@ class ColorAlignment:
|
|
preset = preset.unique_colors()
|
|
preset = preset.unique_colors()
|
|
|
|
|
|
# Apply colors
|
|
# Apply colors
|
|
- new = []
|
|
|
|
- start_color = None
|
|
|
|
color_map = {ai: preset.colors[pi].to_ansi() for ai, pi in self.custom_colors.items()}
|
|
color_map = {ai: preset.colors[pi].to_ansi() for ai, pi in self.custom_colors.items()}
|
|
- for line in asc.split('\n'):
|
|
|
|
- # Line has color placeholders
|
|
|
|
- if len(RE_NEOFETCH_COLOR.findall(line)) > 0:
|
|
|
|
- # Get the last placeholder for the next line
|
|
|
|
- last = int(RE_NEOFETCH_COLOR.findall(line)[-1][3])
|
|
|
|
-
|
|
|
|
- # Replace placeholders
|
|
|
|
- for ascii_i, c in color_map.items():
|
|
|
|
- line = line.replace(f'${{c{ascii_i}}}', c)
|
|
|
|
-
|
|
|
|
- # Add to new ascii
|
|
|
|
- new.append(f'{start_color or ""}{line}')
|
|
|
|
-
|
|
|
|
- # Change next start color
|
|
|
|
- start_color = color_map[last]
|
|
|
|
- else:
|
|
|
|
- new.append(f'{start_color or ""}{line}')
|
|
|
|
-
|
|
|
|
- asc = '\n'.join(new)
|
|
|
|
|
|
+ for ascii_i, c in color_map.items():
|
|
|
|
+ asc = asc.replace(f'${{c{ascii_i}}}', c)
|
|
|
|
|
|
return asc
|
|
return asc
|
|
|
|
|
|
@@ -111,8 +133,9 @@ def get_distro_ascii(distro: str | None = None) -> str:
|
|
"""
|
|
"""
|
|
if not distro and GLOBAL_CFG.override_distro:
|
|
if not distro and GLOBAL_CFG.override_distro:
|
|
distro = GLOBAL_CFG.override_distro
|
|
distro = GLOBAL_CFG.override_distro
|
|
- print(distro)
|
|
|
|
- print(GLOBAL_CFG)
|
|
|
|
|
|
+ if GLOBAL_CFG.debug:
|
|
|
|
+ print(distro)
|
|
|
|
+ print(GLOBAL_CFG)
|
|
cmd = 'print_ascii'
|
|
cmd = 'print_ascii'
|
|
if distro:
|
|
if distro:
|
|
os.environ['CUSTOM_DISTRO'] = distro
|
|
os.environ['CUSTOM_DISTRO'] = distro
|
|
@@ -148,3 +171,14 @@ def run_neofetch(preset: ColorProfile, alignment: ColorAlignment):
|
|
# print(full_cmd)
|
|
# print(full_cmd)
|
|
|
|
|
|
subprocess.run(full_cmd)
|
|
subprocess.run(full_cmd)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# Color alignment recommendations
|
|
|
|
+color_alignments = {
|
|
|
|
+ 'fedora': ColorAlignment('horizontal', fore_back=(2, 1)),
|
|
|
|
+ 'ubuntu': ColorAlignment('horizontal', fore_back=(2, 1)),
|
|
|
|
+ 'nixos': ColorAlignment('custom', {1: 1, 2: 0}),
|
|
|
|
+ # 'arch': ColorAlignment('horizontal'),
|
|
|
|
+ # 'centos': ColorAlignment('horizontal'),
|
|
|
|
+}
|
|
|
|
+
|