Apply default colors in diff kitten

This commit is contained in:
Kovid Goyal 2018-04-24 10:33:18 +05:30
parent fe5b8f3aec
commit 5c4b14468c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 18 additions and 9 deletions

View File

@ -12,7 +12,9 @@ from kitty.key_encoding import ESCAPE
from ..tui.handler import Handler
from ..tui.loop import Loop
from ..tui.operations import clear_screen, set_line_wrapping, set_window_title
from ..tui.operations import (
clear_screen, set_default_colors, set_line_wrapping, set_window_title
)
from .collect import create_collection, data_for_path
from .config import init_config
from .git import Differ
@ -35,8 +37,9 @@ def generate_diff(collection, context):
class DiffHandler(Handler):
def __init__(self, args, left, right):
def __init__(self, args, opts, left, right):
self.state = INITIALIZING
self.opts = opts
self.left, self.right = left, right
self.report_traceback_on_exit = None
self.args = args
@ -54,6 +57,7 @@ class DiffHandler(Handler):
def init_terminal_state(self):
self.write(set_line_wrapping(False))
self.write(set_window_title('kitty +diff'))
self.write(set_default_colors(self.opts.foreground, self.opts.background))
def initialize(self):
self.init_terminal_state()
@ -61,7 +65,7 @@ class DiffHandler(Handler):
self.create_collection()
def finalize(self):
pass
self.write(set_default_colors())
def draw_screen(self):
if self.state < DIFFED:
@ -144,10 +148,10 @@ def main(args):
left, right = items
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.')
init_config(args)
opts = init_config(args)
loop = Loop()
handler = DiffHandler(args, left, right)
handler = DiffHandler(args, opts, left, right)
loop.loop(handler)
if loop.return_code != 0:
if handler.report_traceback_on_exit:

View File

@ -14,6 +14,7 @@ class Handler:
self.initialize()
def __exit__(self, *a):
del self.write_buf[:]
self.finalize()
def initialize(self):

View File

@ -376,8 +376,12 @@ class Loop:
self.return_code = 1
keep_going = False
finalize_output = b''.join(handler.write_buf).decode('utf-8')
if tb is not None:
self._report_error_loop(tb, term_manager)
self._report_error_loop(finalize_output + tb, term_manager)
if tb is None:
os.write(self.output_fd, finalize_output.encode('utf-8'))
def _report_error_loop(self, tb, term_manager):
select = self.sel.select

View File

@ -5,7 +5,7 @@
import sys
from contextlib import contextmanager
from kitty.rgb import color_as_sharp, to_color
from kitty.rgb import Color, color_as_sharp, to_color
from kitty.terminfo import string_capabilities
S7C1T = '\033 F'
@ -168,9 +168,9 @@ def set_default_colors(fg=None, bg=None):
if fg is None:
ans += '\x1b]110\x1b\\'
else:
ans += '\x1b]10;{}\x1b\\'.format(color_as_sharp(to_color(fg)))
ans += '\x1b]10;{}\x1b\\'.format(color_as_sharp(fg if isinstance(fg, Color) else to_color(fg)))
if bg is None:
ans += '\x1b]111\x1b\\'
else:
ans += '\x1b]11;{}\x1b\\'.format(color_as_sharp(to_color(bg)))
ans += '\x1b]11;{}\x1b\\'.format(color_as_sharp(bg if isinstance(bg, Color) else to_color(bg)))
return ans