Parcourir la source

[+] Terminal size warning

Azalea (on HyDEV-Daisy) il y a 2 ans
Parent
commit
bc7339969f
3 fichiers modifiés avec 35 ajouts et 16 suppressions
  1. 1 7
      hyfetch/constants.py
  2. 23 9
      hyfetch/main.py
  3. 11 0
      hyfetch/neofetch_util.py

+ 1 - 7
hyfetch/constants.py

@@ -9,13 +9,6 @@ from typing_extensions import Literal
 CONFIG_PATH = Path.home() / '.config/hyfetch.json'
 CONFIG_PATH = Path.home() / '.config/hyfetch.json'
 VERSION = '1.4.0'
 VERSION = '1.4.0'
 
 
-# Obtain terminal size
-try:
-    TERM_LEN = os.get_terminal_size().columns
-    TERM_LINES = os.get_terminal_size().lines
-except Exception:
-    TERM_LEN = 40
-    TERM_LINES = 10
 
 
 TEST_ASCII = r"""
 TEST_ASCII = r"""
 ### |\___/| ###
 ### |\___/| ###
@@ -34,6 +27,7 @@ _/\_\_   _/_/\_
 TEST_ASCII_WIDTH = max(len(line) for line in TEST_ASCII.split('\n'))
 TEST_ASCII_WIDTH = max(len(line) for line in TEST_ASCII.split('\n'))
 DEFAULT_DARK_L = 0.
 DEFAULT_DARK_L = 0.
 
 
+
 @dataclass
 @dataclass
 class GlobalConfig:
 class GlobalConfig:
     # Global color mode default to 8-bit for compatibility
     # Global color mode default to 8-bit for compatibility

+ 23 - 9
hyfetch/main.py

@@ -16,7 +16,7 @@ from math import ceil
 from .color_util import printc, color, clear_screen
 from .color_util import printc, color, clear_screen
 from .constants import *
 from .constants import *
 from .models import Config
 from .models import Config
-from .neofetch_util import run_neofetch, get_distro_ascii, ColorAlignment, ascii_size, get_fore_back
+from .neofetch_util import *
 from .presets import PRESETS
 from .presets import PRESETS
 
 
 
 
@@ -88,21 +88,36 @@ def create_config() -> Config:
 
 
     :return: Config object (automatically stored)
     :return: Config object (automatically stored)
     """
     """
+    asc = get_distro_ascii()
     title = 'Welcome to &b&lhy&f&lfetch&r! Let\'s set up some colors first.'
     title = 'Welcome to &b&lhy&f&lfetch&r! Let\'s set up some colors first.'
     clear_screen(title)
     clear_screen(title)
 
 
+    ##############################
+    # 0. Check term size
+    try:
+        term_len, term_lines = os.get_terminal_size().columns, os.get_terminal_size().lines
+        if term_len < 110 or term_lines < 30:
+            printc(f'&cWarning: Your terminal is too small ({term_len} * {term_lines}). \n'
+                   f'Please resize it for better experience.')
+            input('Press any key to ignore...')
+    except:
+        # print('Warning: We cannot detect your terminal size.')
+        pass
+
     ##############################
     ##############################
     # 1. Select color system
     # 1. Select color system
+    clear_screen(title)
+    term_len, term_lines = term_size()
     try:
     try:
         # Demonstrate RGB with a gradient. This requires numpy
         # Demonstrate RGB with a gradient. This requires numpy
         from .color_scale import Scale
         from .color_scale import Scale
 
 
         scale2 = Scale(['#12c2e9', '#c471ed', '#f7797d'])
         scale2 = Scale(['#12c2e9', '#c471ed', '#f7797d'])
-        _8bit = [scale2(i / TERM_LEN).to_ansi_8bit(False) for i in range(TERM_LEN)]
-        _rgb = [scale2(i / TERM_LEN).to_ansi_rgb(False) for i in range(TERM_LEN)]
+        _8bit = [scale2(i / term_len).to_ansi_8bit(False) for i in range(term_len)]
+        _rgb = [scale2(i / term_len).to_ansi_rgb(False) for i in range(term_len)]
 
 
-        printc('&f' + ''.join(c + t for c, t in zip(_8bit, '8bit Color Testing'.center(TERM_LEN))))
-        printc('&f' + ''.join(c + t for c, t in zip(_rgb, 'RGB Color Testing'.center(TERM_LEN))))
+        printc('&f' + ''.join(c + t for c, t in zip(_8bit, '8bit Color Testing'.center(term_len))))
+        printc('&f' + ''.join(c + t for c, t in zip(_rgb, 'RGB Color Testing'.center(term_len))))
 
 
         print()
         print()
         printc(f'&a1. Which &bcolor system &ado you want to use?')
         printc(f'&a1. Which &bcolor system &ado you want to use?')
@@ -138,8 +153,8 @@ def create_config() -> Config:
         flags.append([name.center(spacing), flag, flag, flag])
         flags.append([name.center(spacing), flag, flag, flag])
 
 
     # Calculate flags per row
     # Calculate flags per row
-    flags_per_row = TERM_LEN // (spacing + 2)
-    row_per_page = (TERM_LINES - 13) // 5
+    flags_per_row = term_size()[0] // (spacing + 2)
+    row_per_page = max(1, (term_size()[1] - 13) // 5)
     num_pages = ceil(len(flags) / (flags_per_row * row_per_page))
     num_pages = ceil(len(flags) / (flags_per_row * row_per_page))
 
 
     # Create pages
     # Create pages
@@ -196,7 +211,7 @@ def create_config() -> Config:
     print()
     print()
 
 
     # Print cats
     # Print cats
-    num_cols = TERM_LEN // (TEST_ASCII_WIDTH + 2)
+    num_cols = term_size()[0] // (TEST_ASCII_WIDTH + 2)
     ratios = [col / (num_cols - 1) for col in range(num_cols)]
     ratios = [col / (num_cols - 1) for col in range(num_cols)]
     ratios = [(r * 0.4 + 0.1) if is_light else (r * 0.4 + 0.5) for r in ratios]
     ratios = [(r * 0.4 + 0.1) if is_light else (r * 0.4 + 0.5) for r in ratios]
     lines = [ColorAlignment('horizontal').recolor_ascii(TEST_ASCII.replace(
     lines = [ColorAlignment('horizontal').recolor_ascii(TEST_ASCII.replace(
@@ -231,7 +246,6 @@ def create_config() -> Config:
     while True:
     while True:
         clear_screen(title)
         clear_screen(title)
 
 
-        asc = get_distro_ascii()
         asc_width = ascii_size(asc)[0]
         asc_width = ascii_size(asc)[0]
         fore_back = get_fore_back()
         fore_back = get_fore_back()
         arrangements = [
         arrangements = [

+ 11 - 0
hyfetch/neofetch_util.py

@@ -21,6 +21,17 @@ from .serializer import from_dict
 RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
 RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}')
 
 
 
 
+def term_size() -> tuple[int, int]:
+    """
+    Get terminal size
+    :return:
+    """
+    try:
+        return os.get_terminal_size().columns, os.get_terminal_size().lines
+    except Exception:
+        return 100, 20
+
+
 def ascii_size(asc: str) -> tuple[int, int]:
 def ascii_size(asc: str) -> tuple[int, int]:
     """
     """
     Get distro ascii width, height ignoring color code
     Get distro ascii width, height ignoring color code