neofetch_util.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import annotations
  2. import os
  3. import re
  4. from pathlib import Path
  5. from subprocess import check_output
  6. from tempfile import TemporaryDirectory
  7. import pkg_resources
  8. from .color_util import AnsiMode
  9. from .presets import ColorProfile
  10. def get_command_path() -> str:
  11. """
  12. Get the absolute path of the neofetch command
  13. :return: Command path
  14. """
  15. return pkg_resources.resource_filename(__name__, 'scripts/neofetch_mod.sh')
  16. def get_distro_ascii() -> str:
  17. """
  18. Get the distro ascii
  19. :return: Distro ascii
  20. """
  21. return check_output([get_command_path(), "print_ascii"]).decode().strip()
  22. def run_neofetch(preset: ColorProfile, mode: AnsiMode):
  23. # Get existing ascii
  24. asc = get_distro_ascii()
  25. # Remove existing colors
  26. asc = re.sub('\\${.*?}', '', asc)
  27. # Add new colors
  28. lines = asc.split('\n')
  29. colors = preset.with_length(len(lines))
  30. asc = '\n'.join([colors[i].to_ansi(mode) + l for i, l in enumerate(lines)])
  31. # Write temp file
  32. with TemporaryDirectory() as tmp_dir:
  33. tmp_dir = Path(tmp_dir)
  34. path = tmp_dir / 'ascii.txt'
  35. path.write_text(asc)
  36. # Call neofetch with the temp file
  37. os.environ['ascii_len'] = str(max(len(l) for l in lines))
  38. os.environ['ascii_lines'] = str(len(lines))
  39. os.system(get_command_path() + f' --ascii --source {path.absolute()} --ascii-colors')