Browse Source

[O] Optimize flag display

Azalea (on HyDEV-Daisy) 3 years ago
parent
commit
f6a25d7ea8
3 changed files with 28 additions and 10 deletions
  1. 1 1
      hyfetch/__init__.py
  2. 13 6
      hyfetch/main.py
  3. 14 3
      hyfetch/presets.py

+ 1 - 1
hyfetch/__init__.py

@@ -1,4 +1,4 @@
-__version__ = '1.0.0'
+__version__ = '1.0.1'
 
 
 from . import main
 from . import main
 
 

+ 13 - 6
hyfetch/main.py

@@ -98,13 +98,20 @@ def create_config() -> Config:
                                  ['ansi', '8bit', 'rgb'], 'rgb')
                                  ['ansi', '8bit', 'rgb'], 'rgb')
 
 
     # Print preset
     # Print preset
-    print('Available presets:')
+    print('Available presets:\n')
     spacing = max(max(len(k) for k in PRESETS.keys()), 30)
     spacing = max(max(len(k) for k in PRESETS.keys()), 30)
+    flags = []
     for name, preset in PRESETS.items():
     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()
     print()
     tmp = PRESETS['rainbow'].color_text('preset')
     tmp = PRESETS['rainbow'].color_text('preset')
@@ -139,7 +146,7 @@ def run():
 
 
     # Reset config
     # Reset config
     if args.config:
     if args.config:
-        create_config()
+        config = create_config()
 
 
     # Param overwrite config
     # Param overwrite config
     if args.preset:
     if args.preset:

+ 14 - 3
hyfetch/presets.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 from __future__ import annotations
 
 
 from dataclasses import dataclass
 from dataclasses import dataclass
-from typing import Literal
+from typing import Literal, Optional
 
 
 from .color_util import RGB
 from .color_util import RGB
 
 
@@ -60,16 +60,27 @@ class ColorProfile:
 
 
         return self.with_weights(weights)
         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
         Color a text
 
 
         :param txt: Text
         :param txt: Text
         :param foreground: Whether the foreground text show the color or the background block
         :param foreground: Whether the foreground text show the color or the background block
+        :param space_only: Whether to only color spaces
         :return: Colored text
         :return: Colored text
         """
         """
         colors = self.with_length(len(txt))
         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] = {
 PRESETS: dict[str, ColorProfile] = {