neofetch_util.py 1.3 KB

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