neofetch_util.py 1.3 KB

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