[O] Refactor global cfg
This commit is contained in:
parent
a1d687d3bd
commit
59f5fd5651
4 changed files with 43 additions and 23 deletions
|
@ -4,7 +4,7 @@ import colorsys
|
|||
from typing import NamedTuple
|
||||
from typing_extensions import Literal
|
||||
|
||||
from hyfetch.constants import COLOR_MODE
|
||||
from .constants import GLOBAL_CFG
|
||||
|
||||
AnsiMode = Literal['default', 'ansi', '8bit', 'rgb']
|
||||
|
||||
|
@ -38,7 +38,7 @@ def color(msg: str) -> str:
|
|||
code = code.replace(',', ' ').replace(';', ' ').replace(' ', ' ')
|
||||
rgb = tuple(int(c) for c in code.split(' '))
|
||||
|
||||
msg = msg[:i] + RGB(*rgb).to_ansi(mode=COLOR_MODE, foreground=fore) + msg[end + 1:]
|
||||
msg = msg[:i] + RGB(*rgb).to_ansi(foreground=fore) + msg[end + 1:]
|
||||
|
||||
return msg
|
||||
|
||||
|
@ -55,7 +55,8 @@ def clear_screen(title: str = ''):
|
|||
"""
|
||||
Clear screen using ANSI escape codes
|
||||
"""
|
||||
print('\033[2J\033[H', end='')
|
||||
if not GLOBAL_CFG.debug:
|
||||
print('\033[2J\033[H', end='')
|
||||
|
||||
if title:
|
||||
print()
|
||||
|
@ -155,7 +156,9 @@ class RGB(NamedTuple):
|
|||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def to_ansi(self, mode: AnsiMode = COLOR_MODE, foreground: bool = True):
|
||||
def to_ansi(self, mode: AnsiMode | None = None, foreground: bool = True):
|
||||
if not mode:
|
||||
mode = GLOBAL_CFG.color_mode
|
||||
if mode == 'rgb':
|
||||
return self.to_ansi_rgb(foreground)
|
||||
if mode == '8bit':
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
CONFIG_PATH = Path.home() / '.config/hyfetch.json'
|
||||
VERSION = '1.0.7'
|
||||
|
||||
# Global color mode default to 8-bit for compatibility
|
||||
COLOR_MODE = '8bit'
|
||||
|
||||
# Obtain terminal size
|
||||
try:
|
||||
TERM_LEN = os.get_terminal_size().columns
|
||||
|
@ -28,3 +28,13 @@ _/\_ _/_/\_
|
|||
| |(_( | |""".strip('\n')
|
||||
|
||||
TEST_ASCII_WIDTH = max(len(line) for line in TEST_ASCII.split('\n'))
|
||||
|
||||
@dataclass
|
||||
class GlobalConfig:
|
||||
# Global color mode default to 8-bit for compatibility
|
||||
color_mode: str
|
||||
override_distro: str | None
|
||||
debug: bool
|
||||
|
||||
|
||||
GLOBAL_CFG = GlobalConfig(color_mode='8bit', override_distro=None, debug=False)
|
||||
|
|
|
@ -4,15 +4,15 @@ from __future__ import annotations
|
|||
import argparse
|
||||
import json
|
||||
import random
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from itertools import permutations
|
||||
from typing import Iterable
|
||||
|
||||
from typing_extensions import Literal
|
||||
|
||||
from . import constants
|
||||
from .color_util import AnsiMode, printc, color, clear_screen
|
||||
from .constants import CONFIG_PATH, VERSION, TERM_LEN, TEST_ASCII_WIDTH, TEST_ASCII
|
||||
from .constants import CONFIG_PATH, VERSION, TERM_LEN, TEST_ASCII_WIDTH, TEST_ASCII, GLOBAL_CFG
|
||||
from .neofetch_util import run_neofetch, get_distro_ascii, ColorAlignment, ascii_size
|
||||
from .presets import PRESETS
|
||||
from .serializer import json_stringify
|
||||
|
@ -108,7 +108,7 @@ def create_config() -> Config:
|
|||
['8bit', 'rgb'], 'rgb')
|
||||
|
||||
# Override global color mode
|
||||
constants.COLOR_MODE = color_system
|
||||
GLOBAL_CFG.color_mode = color_system
|
||||
title += f'\n&e1. Selected color mode: &r{color_system}'
|
||||
|
||||
##############################
|
||||
|
@ -200,19 +200,20 @@ def create_config() -> Config:
|
|||
['Horizontal'.center(asc_width), *ColorAlignment('horizontal').recolor_ascii(asc, _prs).split('\n')],
|
||||
['Vertical'.center(asc_width), *ColorAlignment('vertical').recolor_ascii(asc, _prs).split('\n')],
|
||||
]
|
||||
ascii_per_row = TERM_LEN // (asc_width + 2)
|
||||
|
||||
# Random color schemes
|
||||
# ascii_indices =
|
||||
pis = list(range(len(_prs.unique_colors().colors)))
|
||||
while len(pis) < 6:
|
||||
while len(pis) < len(set(re.findall('(?<=\\${c)[0-9](?=})', asc))):
|
||||
pis += pis
|
||||
perm = list(permutations(pis))
|
||||
choices = random.sample(perm, 4)
|
||||
random_count = ascii_per_row - 2
|
||||
choices = random.sample(perm, random_count)
|
||||
choices = [{i: n for i, n in enumerate(c)} for c in choices]
|
||||
asciis += [[f'Random {i}'.center(asc_width), *ColorAlignment('custom', r).recolor_ascii(asc, _prs).split('\n')]
|
||||
asciis += [[f'random{i}'.center(asc_width), *ColorAlignment('custom', r).recolor_ascii(asc, _prs).split('\n')]
|
||||
for i, r in enumerate(choices)]
|
||||
|
||||
ascii_per_row = TERM_LEN // (asc_width + 2)
|
||||
while asciis:
|
||||
current = asciis[:ascii_per_row]
|
||||
asciis = asciis[ascii_per_row:]
|
||||
|
@ -223,7 +224,7 @@ def create_config() -> Config:
|
|||
|
||||
print('You can type "roll" to randomize again.')
|
||||
print()
|
||||
choice = literal_input(f'Your choice?', ['horizontal', 'vertical', 'roll', 'random1', 'random2', 'random3', 'random4'], 'horizontal')
|
||||
choice = literal_input(f'Your choice?', ['horizontal', 'vertical', 'roll'] + [f'random{i}' for i in range(random_count)], 'horizontal')
|
||||
|
||||
if choice == 'roll':
|
||||
continue
|
||||
|
@ -272,6 +273,14 @@ def run():
|
|||
print(f'Version is {VERSION}')
|
||||
return
|
||||
|
||||
# Test distro ascii art
|
||||
if args.test_distro:
|
||||
print(f'Setting distro to {args.test_distro}')
|
||||
GLOBAL_CFG.override_distro = args.test_distro
|
||||
|
||||
if args.debug:
|
||||
GLOBAL_CFG.debug = True
|
||||
|
||||
# Load config
|
||||
config = check_config()
|
||||
|
||||
|
@ -286,7 +295,7 @@ def run():
|
|||
config.mode = args.mode
|
||||
|
||||
# Override global color mode
|
||||
constants.COLOR_MODE = config.mode
|
||||
GLOBAL_CFG.color_mode = config.mode
|
||||
|
||||
# Get preset
|
||||
preset = PRESETS.get(config.preset)
|
||||
|
@ -299,12 +308,5 @@ def run():
|
|||
if config.lightness:
|
||||
preset = preset.set_light(config.lightness)
|
||||
|
||||
# Test distro ascii art
|
||||
if args.test_distro:
|
||||
asc = get_distro_ascii(args.test_distro)
|
||||
print(asc)
|
||||
print(ColorAlignment('horizontal').recolor_ascii(asc, preset))
|
||||
return
|
||||
|
||||
# Run
|
||||
run_neofetch(preset, config.color_align)
|
||||
|
|
|
@ -13,6 +13,7 @@ import pkg_resources
|
|||
from hyfetch.color_util import color
|
||||
from typing_extensions import Literal
|
||||
|
||||
from .constants import GLOBAL_CFG
|
||||
from .presets import ColorProfile
|
||||
|
||||
|
||||
|
@ -108,6 +109,10 @@ def get_distro_ascii(distro: str | None = None) -> str:
|
|||
|
||||
:return: Distro ascii
|
||||
"""
|
||||
if not distro and GLOBAL_CFG.override_distro:
|
||||
distro = GLOBAL_CFG.override_distro
|
||||
print(distro)
|
||||
print(GLOBAL_CFG)
|
||||
cmd = 'print_ascii'
|
||||
if distro:
|
||||
os.environ['CUSTOM_DISTRO'] = distro
|
||||
|
|
Loading…
Reference in a new issue