diff --git a/README.md b/README.md index e76c0bfb..6c7f10c4 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,10 @@ Currently, these distributions have existing packages for HyFetch: * ArchLinux: `yay -S hyfetch` (Thanks to @ Aleksana) * Nix (Nixpkgs): `nix-env -i hyfetch` ([In Progress](https://github.com/NixOS/nixpkgs/pull/170309)) * Nix (NUR): ([In Progress](https://github.com/nix-community/NUR/pull/467)) -* Guix: [In progress](https://issues.guix.gnu.org/54847#8-lineno27) +* Guix: `guix install hyfetch` (Thanks to @ WammKD) Currently, if you're using Nix the package manager or NixOS, you can use HyFetch with `nix-env -if https://github.com/hykilpikonna/hyfetch/tarball/master -A hyfetch` + > Now `hyfetch` is available in our NixOS-CN's flake. You can add [NixOS-CN](https://github.com/nixos-cn/flakes) in your [Nix Flake](https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html) at first, then install package `hyfetch`. > ``` > #flake.nix @@ -33,6 +34,7 @@ Currently, if you're using Nix the package manager or NixOS, you can use HyFetch > > ``` > (Thanks to @ YisuiDenghua and @ linyinfeng ) + ## Usage When you run `hyfetch` for the first time, it will prompt you to choose a color system and a preset. Just follow the prompt, and everything should work (hopefully). If something doesn't work, feel free to submit an issue! diff --git a/hyfetch/color_util.py b/hyfetch/color_util.py index 45081f8e..a5b60fcd 100644 --- a/hyfetch/color_util.py +++ b/hyfetch/color_util.py @@ -1,13 +1,14 @@ from __future__ import annotations import colorsys -from typing import NamedTuple +from typing import NamedTuple, Callable, Optional from typing_extensions import Literal from .constants import GLOBAL_CFG AnsiMode = Literal['default', 'ansi', '8bit', 'rgb'] +LightDark = Literal['light', 'dark'] MINECRAFT_COLORS = ["&0/\033[0;30m", "&1/\033[0;34m", "&2/\033[0;32m", "&3/\033[0;36m", "&4/\033[0;31m", @@ -176,16 +177,26 @@ class RGB(NamedTuple): """ return RGB(*redistribute_rgb(*[v * multiplier for v in self])) - def set_light(self, light: float) -> 'RGB': + def set_light(self, light: float, at_least: bool | None = None, at_most: bool | None = None) -> 'RGB': """ Set HSL lightness value :param light: Lightness value (0-1) + :param at_least: Set the lightness to at least this value (no change if greater) + :param at_most: Set the lightness to at most this value (no change if lesser) :return: New color (original isn't modified) """ + # Convert to HSL h, l, s = colorsys.rgb_to_hls(*[v / 255.0 for v in self]) - return RGB(*[round(v * 255.0) for v in colorsys.hls_to_rgb(h, light, s)]) - def set_min_light(self, light: float) -> 'RGB': - h, l, s = colorsys.rgb_to_hls(*[v / 255.0 for v in self]) - return RGB(*[round(v * 255.0) for v in colorsys.hls_to_rgb(h, max(l, light), s)]) + # Modify light value + if at_least is None and at_most is None: + l = light + else: + if at_most: + l = min(l, light) + if at_least: + l = max(l, light) + + # Convert back to RGB + return RGB(*[round(v * 255.0) for v in colorsys.hls_to_rgb(h, l, s)]) diff --git a/hyfetch/constants.py b/hyfetch/constants.py index e4e2e629..fa6bf2d1 100644 --- a/hyfetch/constants.py +++ b/hyfetch/constants.py @@ -4,8 +4,10 @@ import os from dataclasses import dataclass from pathlib import Path +from typing_extensions import Literal + CONFIG_PATH = Path.home() / '.config/hyfetch.json' -VERSION = '1.0.7' +VERSION = '1.1.0' # Obtain terminal size try: @@ -14,20 +16,21 @@ except Exception: TERM_LEN = 40 TEST_ASCII = r""" - |\___/| - ) ( - =\ /= - )===( - / \ - | | - / {txt} \ - \ / -_/\_ _/_/\_ -| |( ( | | -| | ) ) | | -| |(_( | |""".strip('\n') +### |\___/| ### +### ) ( ### +## =\ /= ## +#### )===( #### +### / \ ### +### | | ### +## / {txt} \ ## +## \ / ## +_/\_\_ _/_/\_ +|##| ( ( |##| +|##| ) ) |##| +|##| (_( |##|""".strip('\n') TEST_ASCII_WIDTH = max(len(line) for line in TEST_ASCII.split('\n')) +DEFAULT_DARK_L = 0. @dataclass class GlobalConfig: @@ -37,5 +40,13 @@ class GlobalConfig: debug: bool is_light: bool + def light_dark(self) -> Literal['light', 'dark']: + return 'light' if self.is_light else 'dark' + + def default_lightness(self, term: Literal['light', 'dark'] | None = None) -> float: + if term is None: + term = self.light_dark() + return 0.65 if term.lower() == 'dark' else 0.4 + GLOBAL_CFG = GlobalConfig(color_mode='8bit', override_distro=None, debug=False, is_light=False) diff --git a/hyfetch/main.py b/hyfetch/main.py index d60ac279..47d7ba13 100755 --- a/hyfetch/main.py +++ b/hyfetch/main.py @@ -10,7 +10,7 @@ from typing import Iterable from hyfetch import presets -from .color_util import printc, color, clear_screen +from .color_util import printc, color, clear_screen, LightDark from .constants import CONFIG_PATH, VERSION, TERM_LEN, TEST_ASCII_WIDTH, TEST_ASCII, GLOBAL_CFG from .models import Config from .neofetch_util import run_neofetch, get_distro_ascii, ColorAlignment, ascii_size, color_alignments @@ -27,7 +27,10 @@ def check_config() -> Config: :return: Config object """ if CONFIG_PATH.is_file(): - return Config(**json.loads(CONFIG_PATH.read_text('utf-8'))) + try: + return Config.from_dict(json.loads(CONFIG_PATH.read_text('utf-8'))) + except KeyError: + return create_config() return create_config() @@ -98,9 +101,18 @@ def create_config() -> Config: title += f'\n&e1. Selected color mode: &r{color_system}' ############################## - # 2. Choose preset + # 2. Select light/dark mode clear_screen(title) - printc('&a2. Let\'s choose a flag!') + light_dark = literal_input(f'2. Is your terminal in &gf(#85e7e9)light mode&r or &gf(#c471ed)dark mode&r?', + ['light', 'dark'], 'dark') + is_light = light_dark == 'light' + GLOBAL_CFG.is_light = is_light + title += f'\n&e2. Light/Dark: &r{light_dark}' + + ############################## + # 3. Choose preset + clear_screen(title) + printc('&a3. Let\'s choose a flag!') printc('Available flag presets:') print() @@ -122,19 +134,10 @@ def create_config() -> Config: print() print() - tmp = PRESETS['rainbow'].set_light(.7).color_text('preset') + tmp = PRESETS['rainbow'].set_light_dl_def(light_dark).color_text('preset') preset = literal_input(f'Which {tmp} do you want to use?', PRESETS.keys(), 'rainbow', show_ops=False) _prs = PRESETS[preset] - title += f'\n&e2. Selected flag: &r{_prs.color_text(preset)}' - - ############################## - # 3. Select light/dark mode - clear_screen(title) - light_dark = literal_input(f'3. Is your terminal in &gf(#85e7e9)light mode&r or &gf(#c471ed)dark mode&r?', - ['light', 'dark'], 'dark') - is_light = light_dark == 'light' - GLOBAL_CFG.is_light = is_light - title += f'\n&e3. Light/Dark: &r{light_dark}' + title += f'\n&e3. Selected flag: &r{_prs.color_text(preset)}' ############################# # 4. Dim/lighten colors @@ -146,14 +149,14 @@ def create_config() -> Config: # Print cats num_cols = TERM_LEN // (TEST_ASCII_WIDTH + 2) ratios = [col / (num_cols - 1) for col in range(num_cols)] - ratios = [r * 0.6 + 0.2 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( - '{txt}', f'{r * 100:.0f}%'.center(5)), _prs.set_light(r)).split('\n') for r in ratios] + '{txt}', f'{r * 100:.0f}%'.center(5)), _prs.set_light_dl(r, light_dark)).split('\n') for r in ratios] [printc(' '.join(line)) for line in zip(*lines)] while True: print() - printc('Which brightness level look the best? (Default: unset)') + printc(f'Which brightness level look the best? (Default: left blank = {GLOBAL_CFG.default_lightness(light_dark):.2f} for {light_dark} mode)') lightness = input('> ').strip().lower() or None # Parse lightness @@ -170,11 +173,12 @@ def create_config() -> Config: printc('&cUnable to parse lightness value, please input it as a decimal or percentage (e.g. 0.5 or 50%)') if lightness: - _prs = _prs.set_light(lightness) + _prs = _prs.set_light_dl(lightness, light_dark) title += f'\n&e4. Brightness: &r{f"{lightness:.2f}" if lightness else "unset"}' ############################# # 5. Color arrangement + color_alignment = None while True: clear_screen(title) printc(f'&a5. Let\'s choose a color arrangement!') @@ -184,8 +188,8 @@ def create_config() -> Config: asc = get_distro_ascii() asc_width = ascii_size(asc)[0] asciis = [ - ['Horizontal'.center(asc_width), *ColorAlignment('horizontal').recolor_ascii(asc, _prs).split('\n')], - ['Vertical'.center(asc_width), *ColorAlignment('vertical').recolor_ascii(asc, _prs).split('\n')], + [*ColorAlignment('horizontal').recolor_ascii(asc, _prs).split('\n'), 'Horizontal'.center(asc_width)], + [*ColorAlignment('vertical').recolor_ascii(asc, _prs).split('\n'), 'Vertical'.center(asc_width)], ] ascii_per_row = TERM_LEN // (asc_width + 2) @@ -195,10 +199,10 @@ def create_config() -> Config: while len(pis) < len(set(re.findall('(?<=\\${c)[0-9](?=})', asc))): pis += pis perm = list(permutations(pis)) - random_count = ascii_per_row - 2 + random_count = ascii_per_row * 2 - 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 += [[*ColorAlignment('custom', r).recolor_ascii(asc, _prs).split('\n'), f'random{i}'.center(asc_width)] for i, r in enumerate(choices)] while asciis: @@ -219,7 +223,7 @@ def create_config() -> Config: if choice in ['horizontal', 'vertical']: color_alignment = ColorAlignment(choice) elif choice.startswith('random'): - color_alignment = ColorAlignment('custom', choices[int(choice[6]) - 1]) + color_alignment = ColorAlignment('custom', choices[int(choice[6])]) else: raise NotImplementedError() @@ -242,7 +246,7 @@ def create_config() -> Config: def run(): # Create CLI - hyfetch = color('&b&lhy&f&lfetch&r') + hyfetch = color('&b&lhyfetch&r') parser = argparse.ArgumentParser(description=color(f'{hyfetch} - neofetch with flags <3')) parser.add_argument('-c', '--config', action='store_true', help=color(f'Configure {hyfetch}')) @@ -298,9 +302,9 @@ def run(): if args.scale: preset = preset.lighten(args.scale) if args.light: - preset = preset.set_light(args.light) + preset = preset.set_light_raw(args.light) if config.lightness: - preset = preset.set_light(config.lightness) + preset = preset.set_light_dl(config.lightness) # Debug recommendations if args.debug_list: diff --git a/hyfetch/models.py b/hyfetch/models.py index 2ad68835..b36fccfe 100644 --- a/hyfetch/models.py +++ b/hyfetch/models.py @@ -4,20 +4,25 @@ from dataclasses import dataclass from typing_extensions import Literal -from .color_util import AnsiMode +from .color_util import AnsiMode, LightDark from .constants import CONFIG_PATH from .neofetch_util import ColorAlignment -from .serializer import json_stringify +from .serializer import json_stringify, from_dict @dataclass class Config: preset: str mode: AnsiMode - light_dark: Literal['light', 'dark'] = 'dark' + light_dark: LightDark = 'dark' lightness: float | None = None color_align: ColorAlignment = ColorAlignment('horizontal') + @classmethod + def from_dict(cls, d: dict): + d['color_align'] = ColorAlignment.from_dict(d['color_align']) + return from_dict(cls, d) + def save(self): CONFIG_PATH.parent.mkdir(exist_ok=True, parents=True) CONFIG_PATH.write_text(json_stringify(self), 'utf-8') diff --git a/hyfetch/neofetch_util.py b/hyfetch/neofetch_util.py index a6b80f8b..51d87bfe 100644 --- a/hyfetch/neofetch_util.py +++ b/hyfetch/neofetch_util.py @@ -1,5 +1,6 @@ from __future__ import annotations +import inspect import os import platform import re @@ -15,6 +16,7 @@ from typing_extensions import Literal from hyfetch.color_util import color from .constants import GLOBAL_CFG from .presets import ColorProfile +from .serializer import from_dict RE_NEOFETCH_COLOR = re.compile('\\${c[0-9]}') @@ -67,6 +69,10 @@ class ColorAlignment: # Foreground/background ascii color index fore_back: tuple[int, int] = () + @classmethod + def from_dict(cls, d: dict): + return from_dict(cls, d) + def recolor_ascii(self, asc: str, preset: ColorProfile) -> str: """ Use the color alignment to recolor an ascii art @@ -177,7 +183,7 @@ def run_neofetch(preset: ColorProfile, alignment: ColorAlignment): color_alignments = { 'fedora': ColorAlignment('horizontal', fore_back=(2, 1)), 'ubuntu': ColorAlignment('horizontal', fore_back=(2, 1)), - 'nixos': ColorAlignment('custom', {1: 1, 2: 0}), + 'NixOS.*': ColorAlignment('custom', {1: 1, 2: 0}), # 'arch': ColorAlignment('horizontal'), # 'centos': ColorAlignment('horizontal'), } diff --git a/hyfetch/presets.py b/hyfetch/presets.py index c1bdc97b..0551fca3 100644 --- a/hyfetch/presets.py +++ b/hyfetch/presets.py @@ -4,7 +4,8 @@ from typing import Iterable from typing_extensions import Literal -from .color_util import RGB +from .color_util import RGB, LightDark +from .constants import GLOBAL_CFG def remove_duplicates(seq: Iterable) -> list: @@ -100,17 +101,37 @@ class ColorProfile: """ return ColorProfile([c.lighten(multiplier) for c in self.colors]) - def set_light(self, light: float): + def set_light_raw(self, light: float, at_least: bool | None = None, at_most: bool | None = None) -> 'ColorProfile': """ Set HSL lightness value :param light: Lightness value (0-1) + :param at_least: Set the lightness to at least this value (no change if greater) + :param at_most: Set the lightness to at most this value (no change if lesser) :return: New color profile (original isn't modified) """ - return ColorProfile([c.set_light(light) for c in self.colors]) + return ColorProfile([c.set_light(light, at_least, at_most) for c in self.colors]) - def set_min_light(self, light: float): - return ColorProfile([c.set_min_light(light) for c in self.colors]) + def set_light_dl(self, light: float, term: LightDark = GLOBAL_CFG.light_dark()): + """ + Set HSL lightness value with respect to dark/light terminals + + :param light: Lightness value (0-1) + :param term: Terminal color (can be "dark" or "light") + :return: New color profile (original isn't modified) + """ + assert term.lower() in ['light', 'dark'] + at_least, at_most = (True, None) if term.lower() == 'dark' else (None, True) + return self.set_light_raw(light, at_least, at_most) + + def set_light_dl_def(self, term: LightDark | None = None): + """ + Set default lightness with respect to dark/light terminals + + :param term: Terminal color (can be "dark" or "light") + :return: New color profile (original isn't modified) + """ + return self.set_light_dl(GLOBAL_CFG.default_lightness(term), term) def unique_colors(self) -> ColorProfile: """ diff --git a/hyfetch/serializer.py b/hyfetch/serializer.py index 28cf4f37..a392707c 100644 --- a/hyfetch/serializer.py +++ b/hyfetch/serializer.py @@ -1,6 +1,7 @@ from __future__ import annotations import dataclasses +import inspect import json from datetime import datetime, date @@ -41,3 +42,7 @@ def json_stringify(obj: object, indent: int | None = None) -> str: :return: Json strings """ return json.dumps(obj, indent=indent, cls=EnhancedJSONEncoder, ensure_ascii=False) + + +def from_dict(cls, d: dict): + return cls(**{k: v for k, v in d.items() if k in inspect.signature(cls).parameters}) diff --git a/neofetch b/neofetch index 576a35ff..8efe9c6a 100755 --- a/neofetch +++ b/neofetch @@ -11598,6 +11598,11 @@ main() { return 0 } +get_ascii_distro_name() { + get_distro + echo "$ascii_distro" +} + get_print_ascii() { cache_uname get_os