mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-08 21:25:32 +02:00
@@ -5,8 +5,9 @@
|
||||
import os
|
||||
|
||||
from kitty.config_utils import (
|
||||
init_config, load_config as _load_config, merge_dicts, parse_config_base,
|
||||
python_string, resolve_config, to_color
|
||||
init_config, key_func, load_config as _load_config, merge_dicts,
|
||||
parse_config_base, parse_kittens_key, python_string, resolve_config,
|
||||
to_color
|
||||
)
|
||||
from kitty.constants import config_dir
|
||||
from kitty.rgb import color_as_sgr
|
||||
@@ -58,14 +59,46 @@ for name in (
|
||||
' highlight_removed_bg highlight_added_bg'
|
||||
).split():
|
||||
type_map[name] = to_color
|
||||
func_with_args, args_funcs = key_func()
|
||||
|
||||
|
||||
def special_handling(*a):
|
||||
pass
|
||||
@func_with_args('scroll_by')
|
||||
def parse_scroll_by(func, rest):
|
||||
try:
|
||||
return func, int(rest)
|
||||
except Exception:
|
||||
return func, 1
|
||||
|
||||
|
||||
@func_with_args('scroll_to')
|
||||
def parse_scroll_to(func, rest):
|
||||
rest = rest.lower()
|
||||
if rest not in {'start', 'end', 'next-change', 'prev-change'}:
|
||||
rest = 'start'
|
||||
return func, rest
|
||||
|
||||
|
||||
@func_with_args('change_context')
|
||||
def parse_change_context(func, rest):
|
||||
rest = rest.lower()
|
||||
if rest in {'all', 'default'}:
|
||||
return func, rest
|
||||
try:
|
||||
amount = int(rest)
|
||||
except Exception:
|
||||
amount = 5
|
||||
return func, amount
|
||||
|
||||
|
||||
def special_handling(key, val, ans):
|
||||
if key == 'map':
|
||||
action, *key_def = parse_kittens_key(val, args_funcs)
|
||||
ans['key_definitions'][tuple(key_def)] = action
|
||||
return True
|
||||
|
||||
|
||||
def parse_config(lines, check_keys=True):
|
||||
ans = {}
|
||||
ans = {'key_definitions': {}}
|
||||
parse_config_base(
|
||||
lines,
|
||||
defaults,
|
||||
|
||||
@@ -36,3 +36,20 @@ added_margin_bg #cdffd8
|
||||
filler_bg #fafbfc
|
||||
hunk_margin_bg #dbedff
|
||||
hunk_bg #f1f8ff
|
||||
|
||||
|
||||
# Keyboard shortcuts
|
||||
map q quit
|
||||
map esc quit
|
||||
map j scroll_by 1
|
||||
map k scroll_by -1
|
||||
map down scroll_by 1
|
||||
map up scroll_by -1
|
||||
map home scroll_to start
|
||||
map end scroll_to end
|
||||
map n scroll_to next-change
|
||||
map p scroll_to prev-change
|
||||
map a change_context all
|
||||
map = change_context default
|
||||
map + change_context 5
|
||||
map - change_context -5
|
||||
|
||||
@@ -11,9 +11,7 @@ from gettext import gettext as _
|
||||
|
||||
from kitty.cli import CONFIG_HELP, appname, parse_args
|
||||
from kitty.fast_data_types import wcswidth
|
||||
from kitty.key_encoding import (
|
||||
DOWN, END, ESCAPE, HOME, PAGE_DOWN, PAGE_UP, RELEASE, UP
|
||||
)
|
||||
from kitty.key_encoding import RELEASE
|
||||
|
||||
from ..tui.handler import Handler
|
||||
from ..tui.images import ImageManager
|
||||
@@ -63,6 +61,33 @@ class DiffHandler(Handler):
|
||||
self.current_context_count = self.original_context_count = self.opts.num_context_lines
|
||||
self.highlighting_done = False
|
||||
self.restore_position = None
|
||||
for key_def, action in self.opts.key_definitions.items():
|
||||
self.add_shortcut(action, *key_def)
|
||||
|
||||
def perform_action(self, action):
|
||||
func, args = action
|
||||
if func == 'quit':
|
||||
self.quit_loop(0)
|
||||
return
|
||||
if self.state <= DIFFED:
|
||||
if func == 'scroll_by':
|
||||
return self.scroll_lines(*args)
|
||||
if func == 'scroll_to':
|
||||
where = args[0]
|
||||
if 'change' in where:
|
||||
return self.scroll_to_next_change(backwards='prev' in where)
|
||||
amt = len(self.diff_lines) * (1 if 'end' in where else -1)
|
||||
return self.scroll_lines(amt)
|
||||
if func == 'change_context':
|
||||
new_ctx = self.current_context_count
|
||||
to = args[0]
|
||||
if to == 'all':
|
||||
new_ctx = 100000
|
||||
elif to == 'default':
|
||||
new_ctx = self.original_context_count
|
||||
else:
|
||||
new_ctx += to
|
||||
return self.change_context_count(new_ctx)
|
||||
|
||||
def create_collection(self):
|
||||
self.start_job('collect', create_collection, self.left, self.right)
|
||||
@@ -258,46 +283,16 @@ class DiffHandler(Handler):
|
||||
self.draw_screen()
|
||||
|
||||
def on_text(self, text, in_bracketed_paste=False):
|
||||
if text == 'q':
|
||||
if self.state <= DIFFED:
|
||||
self.quit_loop(0)
|
||||
return
|
||||
if self.state is DIFFED:
|
||||
if text in 'jk':
|
||||
self.scroll_lines(1 if text == 'j' else -1)
|
||||
return
|
||||
if text in 'a+-=':
|
||||
new_ctx = self.current_context_count
|
||||
if text == 'a':
|
||||
new_ctx = 100000
|
||||
elif text == '=':
|
||||
new_ctx = self.original_context_count
|
||||
else:
|
||||
new_ctx += (-1 if text == '-' else 1) * 5
|
||||
self.change_context_count(new_ctx)
|
||||
if text in 'np':
|
||||
self.scroll_to_next_change(backwards=text == 'p')
|
||||
return
|
||||
action = self.shortcut_action(text)
|
||||
if action is not None:
|
||||
return self.perform_action(action)
|
||||
|
||||
def on_key(self, key_event):
|
||||
if key_event.type is RELEASE:
|
||||
return
|
||||
if key_event.key is ESCAPE:
|
||||
if self.state <= DIFFED:
|
||||
self.quit_loop(0)
|
||||
return
|
||||
if self.state is DIFFED:
|
||||
if key_event.key is UP or key_event.key is DOWN:
|
||||
self.scroll_lines(1 if key_event.key is DOWN else -1)
|
||||
return
|
||||
if key_event.key is PAGE_UP or key_event.key is PAGE_DOWN:
|
||||
amt = self.num_lines * (1 if key_event.key is PAGE_DOWN else -1)
|
||||
self.scroll_lines(amt)
|
||||
return
|
||||
if key_event.key is HOME or key_event.key is END:
|
||||
amt = len(self.diff_lines) * (1 if key_event.key is END else -1)
|
||||
self.scroll_lines(amt)
|
||||
return
|
||||
action = self.shortcut_action(key_event)
|
||||
if action is not None:
|
||||
return self.perform_action(action)
|
||||
|
||||
def on_resize(self, screen_size):
|
||||
self.screen_size = screen_size
|
||||
|
||||
Reference in New Issue
Block a user