main.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/env python
  2. # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
  3. import sys
  4. from functools import partial
  5. from kitty.cli import CONFIG_HELP, CompletionSpec
  6. from kitty.conf.types import Definition
  7. from kitty.constants import appname
  8. def main(args: list[str]) -> None:
  9. raise SystemExit('Must be run as kitten diff')
  10. definition = Definition(
  11. '!kittens.diff',
  12. )
  13. agr = definition.add_group
  14. egr = definition.end_group
  15. opt = definition.add_option
  16. map = definition.add_map
  17. mma = definition.add_mouse_map
  18. # diff {{{
  19. agr('diff', 'Diffing')
  20. opt('syntax_aliases', 'pyj:py pyi:py recipe:py', ctype='strdict_ _:', option_type='syntax_aliases',
  21. long_text='''
  22. File extension aliases for syntax highlight. For example, to syntax highlight
  23. :file:`file.xyz` as :file:`file.abc` use a setting of :code:`xyz:abc`.
  24. Multiple aliases must be separated by spaces.
  25. '''
  26. )
  27. opt('num_context_lines', '3', option_type='positive_int',
  28. long_text='The number of lines of context to show around each change.'
  29. )
  30. opt('diff_cmd', 'auto',
  31. long_text='''
  32. The diff command to use. Must contain the placeholder :code:`_CONTEXT_` which
  33. will be replaced by the number of lines of context. A few special values are allowed:
  34. :code:`auto` will automatically pick an available diff implementation. :code:`builtin`
  35. will use the anchored diff algorithm from the Go standard library. :code:`git` will
  36. use the git command to do the diffing. :code:`diff` will use the diff command to
  37. do the diffing.
  38. '''
  39. )
  40. opt('replace_tab_by', '\\x20\\x20\\x20\\x20', option_type='python_string',
  41. long_text='The string to replace tabs with. Default is to use four spaces.'
  42. )
  43. opt('+ignore_name', '', ctype='string',
  44. add_to_default=False,
  45. long_text='''
  46. A glob pattern that is matched against only the filename of files and directories. Matching
  47. files and directories are ignored when scanning the filesystem to look for files to diff.
  48. Can be specified multiple times to use multiple patterns. For example::
  49. ignore_name .git
  50. ignore_name *~
  51. ignore_name *.pyc
  52. ''',
  53. )
  54. egr() # }}}
  55. # colors {{{
  56. agr('colors', 'Colors')
  57. opt('color_scheme', 'auto', choices=('auto', 'light', 'dark'), long_text='''
  58. Whether to use the light or dark colors. The default of :code:`auto` means
  59. to follow the parent terminal color scheme. Note that the actual colors used
  60. for dark schemes are set by the :code:`dark_*` settings below and the non-prefixed
  61. settings are used for light colors.
  62. ''')
  63. opt('pygments_style', 'default', long_text='''
  64. The pygments color scheme to use for syntax highlighting. See :link:`pygments
  65. builtin styles <https://pygments.org/styles/>` for a list of schemes. Note that
  66. this **does not** change the colors used for diffing,
  67. only the colors used for syntax highlighting. To change the general colors use the settings below.
  68. This sets the colors used for light color schemes, use :opt:`dark_pygments_style` to change the
  69. colors for dark color schemes.
  70. '''
  71. )
  72. opt('dark_pygments_style', 'github-dark', long_text='''
  73. The pygments color scheme to use for syntax highlighting with dark colors. See :link:`pygments
  74. builtin styles <https://pygments.org/styles/>` for a list of schemes. Note that
  75. this **does not** change the colors used for diffing,
  76. only the colors used for syntax highlighting. To change the general colors use the settings below.
  77. This sets the colors used for dark color schemes, use :opt:`pygments_style` to change the
  78. colors for light color schemes.''')
  79. opt('foreground', 'black', option_type='to_color', long_text='Basic colors')
  80. opt('dark_foreground', '#f8f8f2', option_type='to_color')
  81. dark_bg = '#212830'
  82. opt('background', 'white', option_type='to_color',)
  83. opt('dark_background', dark_bg, option_type='to_color',)
  84. opt('title_fg', 'black', option_type='to_color', long_text='Title colors')
  85. opt('dark_title_fg', 'white', option_type='to_color')
  86. opt('title_bg', 'white', option_type='to_color',)
  87. opt('dark_title_bg', dark_bg, option_type='to_color',)
  88. opt('margin_bg', '#fafbfc', option_type='to_color', long_text='Margin colors')
  89. opt('dark_margin_bg', dark_bg, option_type='to_color')
  90. opt('margin_fg', '#aaaaaa', option_type='to_color')
  91. opt('dark_margin_fg', '#aaaaaa', option_type='to_color')
  92. opt('removed_bg', '#ffeef0', option_type='to_color', long_text='Removed text backgrounds')
  93. opt('dark_removed_bg', '#352c33', option_type='to_color')
  94. opt('highlight_removed_bg', '#fdb8c0', option_type='to_color')
  95. opt('dark_highlight_removed_bg', '#5c3539', option_type='to_color')
  96. opt('removed_margin_bg', '#ffdce0', option_type='to_color')
  97. opt('dark_removed_margin_bg', '#5c3539', option_type='to_color')
  98. opt('added_bg', '#e6ffed', option_type='to_color', long_text='Added text backgrounds')
  99. opt('dark_added_bg', '#263834', option_type='to_color')
  100. opt('highlight_added_bg', '#acf2bd', option_type='to_color')
  101. opt('dark_highlight_added_bg', '#31503d', option_type='to_color')
  102. opt('added_margin_bg', '#cdffd8', option_type='to_color')
  103. opt('dark_added_margin_bg', '#31503d', option_type='to_color')
  104. opt('filler_bg', '#fafbfc', option_type='to_color', long_text='Filler (empty) line background')
  105. opt('dark_filler_bg', '#262c36', option_type='to_color')
  106. opt('margin_filler_bg', 'none', option_type='to_color_or_none', long_text='Filler (empty) line background in margins, defaults to the filler background')
  107. opt('dark_margin_filler_bg', 'none', option_type='to_color_or_none')
  108. opt('hunk_margin_bg', '#dbedff', option_type='to_color', long_text='Hunk header colors')
  109. opt('dark_hunk_margin_bg', '#0c2d6b', option_type='to_color')
  110. opt('hunk_bg', '#f1f8ff', option_type='to_color')
  111. opt('dark_hunk_bg', '#253142', option_type='to_color')
  112. opt('search_bg', '#444', option_type='to_color', long_text='Highlighting')
  113. opt('dark_search_bg', '#2c599c', option_type='to_color')
  114. opt('search_fg', 'white', option_type='to_color')
  115. opt('dark_search_fg', 'white', option_type='to_color')
  116. opt('select_bg', '#b4d5fe', option_type='to_color')
  117. opt('dark_select_bg', '#2c599c', option_type='to_color')
  118. opt('select_fg', 'black', option_type='to_color_or_none')
  119. opt('dark_select_fg', 'white', option_type='to_color_or_none')
  120. egr() # }}}
  121. # shortcuts {{{
  122. agr('shortcuts', 'Keyboard shortcuts')
  123. map('Quit',
  124. 'quit q quit',
  125. )
  126. map('Quit',
  127. 'quit esc quit',
  128. )
  129. map('Scroll down',
  130. 'scroll_down j scroll_by 1',
  131. )
  132. map('Scroll down',
  133. 'scroll_down down scroll_by 1',
  134. )
  135. map('Scroll up',
  136. 'scroll_up k scroll_by -1',
  137. )
  138. map('Scroll up',
  139. 'scroll_up up scroll_by -1',
  140. )
  141. map('Scroll to top',
  142. 'scroll_top home scroll_to start',
  143. )
  144. map('Scroll to bottom',
  145. 'scroll_bottom end scroll_to end',
  146. )
  147. map('Scroll to next page',
  148. 'scroll_page_down page_down scroll_to next-page',
  149. )
  150. map('Scroll to next page',
  151. 'scroll_page_down space scroll_to next-page',
  152. )
  153. map('Scroll to previous page',
  154. 'scroll_page_up page_up scroll_to prev-page',
  155. )
  156. map('Scroll to next change',
  157. 'next_change n scroll_to next-change',
  158. )
  159. map('Scroll to previous change',
  160. 'prev_change p scroll_to prev-change',
  161. )
  162. map('Scroll to next file',
  163. 'next_file shift+j scroll_to next-file',
  164. )
  165. map('Scroll to previous file',
  166. 'prev_file shift+k scroll_to prev-file',
  167. )
  168. map('Show all context',
  169. 'all_context a change_context all',
  170. )
  171. map('Show default context',
  172. 'default_context = change_context default',
  173. )
  174. map('Increase context',
  175. 'increase_context + change_context 5',
  176. )
  177. map('Decrease context',
  178. 'decrease_context - change_context -5',
  179. )
  180. map('Search forward',
  181. 'search_forward / start_search regex forward',
  182. )
  183. map('Search backward',
  184. 'search_backward ? start_search regex backward',
  185. )
  186. map('Scroll to next search match',
  187. 'next_match . scroll_to next-match',
  188. )
  189. map('Scroll to next search match',
  190. 'next_match > scroll_to next-match',
  191. )
  192. map('Scroll to previous search match',
  193. 'prev_match , scroll_to prev-match',
  194. )
  195. map('Scroll to previous search match',
  196. 'prev_match < scroll_to prev-match',
  197. )
  198. map('Search forward (no regex)',
  199. 'search_forward_simple f start_search substring forward',
  200. )
  201. map('Search backward (no regex)',
  202. 'search_backward_simple b start_search substring backward',
  203. )
  204. map('Copy selection to clipboard', 'copy_to_clipboard y copy_to_clipboard')
  205. map('Copy selection to clipboard or exit if no selection is present', 'copy_to_clipboard_or_exit ctrl+c copy_to_clipboard_or_exit')
  206. egr() # }}}
  207. OPTIONS = partial('''\
  208. --context
  209. type=int
  210. default=-1
  211. Number of lines of context to show between changes. Negative values use the
  212. number set in :file:`diff.conf`.
  213. --config
  214. type=list
  215. completion=type:file ext:conf group:"Config files" kwds:none,NONE
  216. {config_help}
  217. --override -o
  218. type=list
  219. Override individual configuration options, can be specified multiple times.
  220. Syntax: :italic:`name=value`. For example: :italic:`-o background=gray`
  221. '''.format, config_help=CONFIG_HELP.format(conf_name='diff', appname=appname))
  222. help_text = 'Show a side-by-side diff of the specified files/directories. You can also use :italic:`ssh:hostname:remote-file-path` to diff remote files.'
  223. usage = 'file_or_directory_left file_or_directory_right'
  224. if __name__ == '__main__':
  225. main(sys.argv)
  226. elif __name__ == '__doc__':
  227. cd = sys.cli_docs # type: ignore
  228. cd['usage'] = usage
  229. cd['options'] = OPTIONS
  230. cd['help_text'] = help_text
  231. cd['short_desc'] = 'Pretty, side-by-side diffing of files and images'
  232. cd['args_completion'] = CompletionSpec.from_string('type:file mime:text/* mime:image/* group:"Text and image files"')
  233. elif __name__ == '__conf__':
  234. sys.options_definition = definition # type: ignore