From f6a25d7ea806308f8f2ed680d58b359ad314184d Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Sun, 10 Apr 2022 06:45:45 -0400 Subject: [PATCH] [O] Optimize flag display --- hyfetch/__init__.py | 2 +- hyfetch/main.py | 19 +++++++++++++------ hyfetch/presets.py | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/hyfetch/__init__.py b/hyfetch/__init__.py index 54faa856..029ad4dd 100644 --- a/hyfetch/__init__.py +++ b/hyfetch/__init__.py @@ -1,4 +1,4 @@ -__version__ = '1.0.0' +__version__ = '1.0.1' from . import main diff --git a/hyfetch/main.py b/hyfetch/main.py index a073c425..87909eb9 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -98,13 +98,20 @@ def create_config() -> Config: ['ansi', '8bit', 'rgb'], 'rgb') # Print preset - print('Available presets:') + print('Available presets:\n') spacing = max(max(len(k) for k in PRESETS.keys()), 30) + flags = [] for name, preset in PRESETS.items(): - printc(preset.color_text(center_text(name, spacing), foreground=False)) - - # preset_demo = ''.join(f'{c.to_ansi_rgb(False)} ' for c in preset.with_length(flag_length)) - # printc(name + ' ' * (spacing - len(name)) + preset_demo) + flags.append([preset.color_text(' ' * spacing, foreground=False), + '&0' + preset.color_text(center_text(name, spacing), foreground=False), + preset.color_text(' ' * spacing, foreground=False)]) + flags_per_row = 3 + while flags: + current = flags[:flags_per_row] + flags = flags[flags_per_row:] + for line in range(len(current[0])): + printc(' '.join(flag[line] for flag in current)) + print() print() tmp = PRESETS['rainbow'].color_text('preset') @@ -139,7 +146,7 @@ def run(): # Reset config if args.config: - create_config() + config = create_config() # Param overwrite config if args.preset: diff --git a/hyfetch/presets.py b/hyfetch/presets.py index bf27b74b..3b2eba6d 100644 --- a/hyfetch/presets.py +++ b/hyfetch/presets.py @@ -1,7 +1,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Literal +from typing import Literal, Optional from .color_util import RGB @@ -60,16 +60,27 @@ class ColorProfile: return self.with_weights(weights) - def color_text(self, txt: str, foreground: bool = True) -> str: + def color_text(self, txt: str, foreground: bool = True, space_only: bool = False) -> str: """ Color a text :param txt: Text :param foreground: Whether the foreground text show the color or the background block + :param space_only: Whether to only color spaces :return: Colored text """ colors = self.with_length(len(txt)) - return ''.join([colors[i].to_ansi_rgb(foreground) + c for i, c in enumerate(txt)]) + '\033[0m' + result = '' + for i, t in enumerate(txt): + if space_only and t != ' ': + if i > 0 and txt[i - 1] == ' ': + result += '\033[0m' + result += t + else: + result += colors[i].to_ansi_rgb(foreground) + t + + result += '\033[0m' + return result PRESETS: dict[str, ColorProfile] = {