From 7365ed9b05b2e512d8279d2faed72b7a1cc3996c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 8 May 2018 23:02:00 +0530 Subject: [PATCH] diff: Better title rendering --- kittens/diff/collect.py | 4 ++-- kittens/diff/main.py | 2 +- kittens/diff/render.py | 17 +++++++++++++---- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/kittens/diff/collect.py b/kittens/diff/collect.py index 8209e35ff..5a489b522 100644 --- a/kittens/diff/collect.py +++ b/kittens/diff/collect.py @@ -150,8 +150,8 @@ def create_collection(left, right): collect_files(collection, left, right) else: pl, pr = os.path.abspath(left), os.path.abspath(right) - path_name_map[left] = pl - path_name_map[right] = pr + path_name_map[pl] = left + path_name_map[pr] = right collection.add_change(pl, pr) collection.finalize() return collection diff --git a/kittens/diff/main.py b/kittens/diff/main.py index 283c006b6..e72d7ac39 100644 --- a/kittens/diff/main.py +++ b/kittens/diff/main.py @@ -46,7 +46,7 @@ class DiffHandler(Handler): def __init__(self, args, opts, left, right): self.state = INITIALIZING self.opts = opts - self.left, self.right = map(os.path.abspath, (left, right)) + self.left, self.right = left, right self.report_traceback_on_exit = None self.args = args self.scroll_pos = self.max_scroll_pos = 0 diff --git a/kittens/diff/render.py b/kittens/diff/render.py index 1dad3495b..bd0747fd0 100644 --- a/kittens/diff/render.py +++ b/kittens/diff/render.py @@ -120,9 +120,18 @@ def highlight_boundaries(ltype): return start, stop -def title_lines(left_path, args, columns, margin_size): - name = fit_in(sanitize(path_name_map[left_path]), columns - 2 * margin_size) - yield title_format(place_in(' ' + name, columns)) +def title_lines(left_path, right_path, args, columns, margin_size): + m = ' ' * margin_size + left_name, right_name = map(path_name_map.get, (left_path, right_path)) + if right_name and right_name != left_name: + n1 = fit_in(m + sanitize(left_name), columns // 2 - margin_size) + n1 = place_in(n1, columns // 2) + n2 = fit_in(m + sanitize(right_name), columns // 2 - margin_size) + n2 = place_in(n2, columns // 2) + name = n1 + n2 + else: + name = place_in(m + sanitize(left_name), columns) + yield title_format(place_in(name, columns)) yield title_format('━' * columns) @@ -354,7 +363,7 @@ def render_diff(collection, diff_map, args, columns): for path, item_type, other_path in collection: item_ref = Reference(path) is_binary = isinstance(data_for_path(path), bytes) - yield from yield_lines_from(title_lines(path, args, columns, margin_size), item_ref) + yield from yield_lines_from(title_lines(path, other_path, args, columns, margin_size), item_ref) if item_type == 'diff': if is_binary: ans = yield_lines_from(binary_lines(path, other_path, columns, margin_size), item_ref)