keymap.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """This script automates the copying of the default keymap into your own keymap.
  2. """
  3. import shutil
  4. from milc import cli
  5. from milc.questions import question
  6. from qmk.path import is_keyboard, keymaps, keymap
  7. from qmk.git import git_get_username
  8. from qmk.decorators import automagic_keyboard, automagic_keymap
  9. from qmk.keyboard import keyboard_completer, keyboard_folder
  10. def prompt_keyboard():
  11. prompt = """{fg_yellow}Select Keyboard{style_reset_all}
  12. If you`re unsure you can view a full list of supported keyboards with {fg_yellow}qmk list-keyboards{style_reset_all}.
  13. Keyboard Name? """
  14. return question(prompt)
  15. def prompt_user():
  16. prompt = """
  17. {fg_yellow}Name Your Keymap{style_reset_all}
  18. Used for maintainer, copyright, etc
  19. Your GitHub Username? """
  20. return question(prompt, default=git_get_username())
  21. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
  22. @cli.argument('-km', '--keymap', help='Specify the name for the new keymap directory')
  23. @cli.subcommand('Creates a new keymap for the keyboard of your choosing')
  24. @automagic_keyboard
  25. @automagic_keymap
  26. def new_keymap(cli):
  27. """Creates a new keymap for the keyboard of your choosing.
  28. """
  29. cli.log.info('{style_bright}Generating a new keymap{style_normal}')
  30. cli.echo('')
  31. # ask for user input if keyboard or keymap was not provided in the command line
  32. kb_name = cli.config.new_keymap.keyboard if cli.config.new_keymap.keyboard else prompt_keyboard()
  33. user_name = cli.config.new_keymap.keymap if cli.config.new_keymap.keymap else prompt_user()
  34. # check directories
  35. if not is_keyboard(kb_name):
  36. cli.log.error(f'Keyboard {{fg_cyan}}{kb_name}{{fg_reset}} does not exist! Please choose a valid name.')
  37. return False
  38. # generate keymap paths
  39. keymaps_dirs = keymaps(kb_name)
  40. keymap_path_default = keymap(kb_name, 'default')
  41. keymap_path_new = keymaps_dirs[0] / user_name
  42. if not keymap_path_default.exists():
  43. cli.log.error(f'Default keymap {{fg_cyan}}{keymap_path_default}{{fg_reset}} does not exist!')
  44. return False
  45. if keymap_path_new.exists():
  46. cli.log.error(f'Keymap {{fg_cyan}}{user_name}{{fg_reset}} already exists! Please choose a different name.')
  47. return False
  48. # create user directory with default keymap files
  49. shutil.copytree(keymap_path_default, keymap_path_new, symlinks=True)
  50. # end message to user
  51. cli.log.info(f'{{fg_green}}Created a new keymap called {{fg_cyan}}{user_name}{{fg_green}} in: {{fg_cyan}}{keymap_path_new}.{{fg_reset}}')
  52. cli.log.info(f"Compile a firmware with your new keymap by typing: {{fg_yellow}}qmk compile -kb {kb_name} -km {user_name}{{fg_reset}}.")