More work on the diff kitten
This commit is contained in:
@@ -107,8 +107,7 @@ class Differ:
|
|||||||
key = file1, file2
|
key = file1, file2
|
||||||
self.jobs.append(key)
|
self.jobs.append(key)
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self, context=3):
|
||||||
context = 3
|
|
||||||
ans = {}
|
ans = {}
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
|
with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
|
||||||
jobs = {executor.submit(run_diff, key[0], key[1], context): key for key in self.jobs}
|
jobs = {executor.submit(run_diff, key[0], key[1], context): key for key in self.jobs}
|
||||||
|
|||||||
@@ -4,20 +4,38 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import traceback
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from gettext import gettext as _
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
from kitty.cli import parse_args
|
from kitty.cli import parse_args
|
||||||
|
from kitty.key_encoding import ESCAPE
|
||||||
|
|
||||||
from ..tui.handler import Handler
|
from ..tui.handler import Handler
|
||||||
from ..tui.loop import Loop
|
from ..tui.loop import Loop
|
||||||
from ..tui.operations import set_line_wrapping, set_window_title
|
from ..tui.operations import clear_screen, set_line_wrapping, set_window_title
|
||||||
from .collect import create_collection
|
from .collect import create_collection
|
||||||
|
|
||||||
|
INITIALIZING, READY, RENDERED = range(3)
|
||||||
|
|
||||||
|
|
||||||
class DiffHandler(Handler):
|
class DiffHandler(Handler):
|
||||||
|
|
||||||
def __init__(self, collection):
|
def __init__(self, left, right):
|
||||||
self.collection = collection
|
self.state = INITIALIZING
|
||||||
|
self.left, self.right = left, right
|
||||||
|
self.report_traceback_on_exit = None
|
||||||
|
|
||||||
|
def create_collection(self):
|
||||||
|
try:
|
||||||
|
self.collection = create_collection(self.left, self.right)
|
||||||
|
except Exception:
|
||||||
|
self.report_traceback_on_exit = traceback.format_exc()
|
||||||
|
self.quit_loop(1)
|
||||||
|
else:
|
||||||
|
self.state = READY
|
||||||
|
self.wakeup()
|
||||||
|
|
||||||
def init_terminal_state(self):
|
def init_terminal_state(self):
|
||||||
self.write(set_line_wrapping(False))
|
self.write(set_line_wrapping(False))
|
||||||
@@ -26,6 +44,35 @@ class DiffHandler(Handler):
|
|||||||
def initialize(self, *args):
|
def initialize(self, *args):
|
||||||
Handler.initialize(self, *args)
|
Handler.initialize(self, *args)
|
||||||
self.init_terminal_state()
|
self.init_terminal_state()
|
||||||
|
self.draw_screen()
|
||||||
|
t = Thread(target=self.create_collection, name='CreatingCollection')
|
||||||
|
t.daemon = True
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
def draw_screen(self):
|
||||||
|
if self.state is INITIALIZING:
|
||||||
|
self.write(clear_screen())
|
||||||
|
self.write(_('Calculating diff, please wait...'))
|
||||||
|
return
|
||||||
|
if self.state is READY:
|
||||||
|
self.write(clear_screen())
|
||||||
|
self.state = RENDERED
|
||||||
|
return
|
||||||
|
|
||||||
|
def on_key(self, key_event):
|
||||||
|
if self.state is INITIALIZING:
|
||||||
|
if key_event.key is ESCAPE:
|
||||||
|
self.quit_loop(0)
|
||||||
|
return
|
||||||
|
|
||||||
|
def on_wakeup(self):
|
||||||
|
self.draw_screen()
|
||||||
|
|
||||||
|
def on_interrupt(self):
|
||||||
|
self.quit_loop(1)
|
||||||
|
|
||||||
|
def on_eot(self):
|
||||||
|
self.quit_loop(1)
|
||||||
|
|
||||||
|
|
||||||
OPTIONS = partial('''\
|
OPTIONS = partial('''\
|
||||||
@@ -40,12 +87,13 @@ def main(args):
|
|||||||
left, right = items
|
left, right = items
|
||||||
if os.path.isdir(left) != os.path.isdir(right):
|
if os.path.isdir(left) != os.path.isdir(right):
|
||||||
raise SystemExit('The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.')
|
raise SystemExit('The items to be diffed should both be either directories or files. Comparing a directory to a file is not valid.')
|
||||||
collection = create_collection(left, right)
|
|
||||||
|
|
||||||
loop = Loop()
|
loop = Loop()
|
||||||
handler = DiffHandler(collection)
|
handler = DiffHandler(left, right)
|
||||||
loop.loop(handler)
|
loop.loop(handler)
|
||||||
if loop.return_code != 0:
|
if loop.return_code != 0:
|
||||||
|
if handler.report_traceback_on_exit:
|
||||||
|
print(handler.report_traceback_on_exit, file=sys.stderr)
|
||||||
raise SystemExit(loop.return_code)
|
raise SystemExit(loop.return_code)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ class Handler:
|
|||||||
def on_eot(self):
|
def on_eot(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def on_wakeup(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
if isinstance(data, str):
|
if isinstance(data, str):
|
||||||
data = data.encode('utf-8')
|
data = data.encode('utf-8')
|
||||||
|
|||||||
@@ -283,6 +283,8 @@ class Loop:
|
|||||||
handler.on_term()
|
handler.on_term()
|
||||||
if b'i' in data:
|
if b'i' in data:
|
||||||
handler.on_interrupt()
|
handler.on_interrupt()
|
||||||
|
if b'1' in data:
|
||||||
|
handler.on_wakeup()
|
||||||
|
|
||||||
def _wakeup_write(self, val):
|
def _wakeup_write(self, val):
|
||||||
while not os.write(self.wakeup_write_fd, val):
|
while not os.write(self.wakeup_write_fd, val):
|
||||||
|
|||||||
Reference in New Issue
Block a user