cformat.py 1.0 KB

1234567891011121314151617181920212223242526272829
  1. """Format C code according to QMK's style.
  2. """
  3. import os
  4. import subprocess
  5. from milc import cli
  6. @cli.argument('files', nargs='*', help='Filename(s) to format.')
  7. @cli.entrypoint("Format C code according to QMK's style.")
  8. def main(cli):
  9. """Format C code according to QMK's style.
  10. """
  11. clang_format = ['clang-format', '-i']
  12. if not cli.args.files:
  13. for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
  14. for dirpath, dirnames, filenames in os.walk(dir):
  15. if 'tmk_core/protocol/usb_hid' in dirpath:
  16. continue
  17. for name in filenames:
  18. if name.endswith('.c') or name.endswith('.h') or name.endswith('.cpp'):
  19. cli.args.files.append(os.path.join(dirpath, name))
  20. try:
  21. subprocess.run(clang_format + cli.args.files, check=True)
  22. cli.log.info('Successfully formatted the C code.')
  23. except subprocess.CalledProcessError:
  24. cli.log.error('Error formatting C code!')
  25. return False