build-terminfo 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. # vim:fileencoding=utf-8
  3. # License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
  4. import glob
  5. import os
  6. import shutil
  7. import subprocess
  8. import sys
  9. import tempfile
  10. def compile_terminfo(base):
  11. with tempfile.TemporaryDirectory() as tdir:
  12. proc = subprocess.run(['tic', '-x', f'-o{tdir}', 'terminfo/kitty.terminfo'], capture_output=True)
  13. if proc.returncode != 0:
  14. sys.stderr.buffer.write(proc.stderr)
  15. raise SystemExit(proc.returncode)
  16. tfiles = glob.glob(os.path.join(tdir, '*', 'xterm-kitty'))
  17. if not tfiles:
  18. raise SystemExit('tic failed to output the compiled kitty terminfo file')
  19. tfile = tfiles[0]
  20. directory, xterm_kitty = os.path.split(tfile)
  21. _, directory = os.path.split(directory)
  22. odir = os.path.join(base, directory)
  23. os.makedirs(odir, exist_ok=True)
  24. ofile = os.path.join(odir, xterm_kitty)
  25. shutil.move(tfile, ofile)
  26. return ofile
  27. def generate_terminfo():
  28. base = os.path.dirname(os.path.abspath(__file__))
  29. os.chdir(base)
  30. sys.path.insert(0, base)
  31. from kitty.terminfo import generate_terminfo
  32. with open('terminfo/kitty.terminfo', 'w') as f:
  33. f.write(generate_terminfo())
  34. proc = subprocess.run(['tic', '-CrT0', 'terminfo/kitty.terminfo'], capture_output=True)
  35. if proc.returncode != 0:
  36. sys.stderr.buffer.write(proc.stderr)
  37. raise SystemExit(proc.returncode)
  38. tcap = proc.stdout.decode('utf-8').splitlines()[-1]
  39. with open('terminfo/kitty.termcap', 'w') as f:
  40. f.write(tcap)
  41. dbfile = compile_terminfo(os.path.join(base, 'terminfo'))
  42. with open(dbfile, 'rb') as f:
  43. data = f.read()
  44. with open('kitty/terminfo.h', 'w') as f:
  45. print(f'static const uint8_t terminfo_data[{len(data)}] = ''{', file=f)
  46. for b in data:
  47. print(b, end=', ', file=f)
  48. print('};', file=f)
  49. if __name__ == '__main__':
  50. generate_terminfo()