[PATCH] chistedit: add basic colours to diff view

Jordi Gutiérrez Hermoso jordigh at octave.org
Thu Apr 4 03:56:28 UTC 2019


# HG changeset patch
# User Jordi Gutiérrez Hermoso <jordigh at octave.org>
# Date 1554350103 14400
#      Wed Apr 03 23:55:03 2019 -0400
# Node ID d0a4c3ae200ffdfc247f5e2f1897196512615087
# Parent  4ee906aa7b60fb6b113e4dc187fbb5a8f42e557c
chistedit: add basic colours to diff view

This isn't complete, and it would be nice to show the exact same
colours that `hg diff` would show. That goal is too lofty, so this
just shows some basic colours, on the premise that a little is better
than nothing.

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -964,6 +964,7 @@ ACTION_LABELS = {
 }
 
 COLOR_HELP, COLOR_SELECTED, COLOR_OK, COLOR_WARN, COLOR_CURRENT  = 1, 2, 3, 4, 5
+COLOR_DIFF_ADD_LINE, COLOR_DIFF_DEL_LINE, COLOR_DIFF_OFFSET = 6, 7, 8
 
 E_QUIT, E_HISTEDIT = 1, 2
 E_PAGEDOWN, E_PAGEUP, E_LINEUP, E_LINEDOWN, E_RESIZE = 3, 4, 5, 6, 7
@@ -1244,6 +1245,9 @@ def _chisteditmain(repo, rules, stdscr):
     curses.init_pair(COLOR_WARN, curses.COLOR_BLACK, curses.COLOR_YELLOW)
     curses.init_pair(COLOR_OK, curses.COLOR_BLACK, curses.COLOR_GREEN)
     curses.init_pair(COLOR_CURRENT, curses.COLOR_WHITE, curses.COLOR_MAGENTA)
+    curses.init_pair(COLOR_DIFF_ADD_LINE, curses.COLOR_GREEN, -1)
+    curses.init_pair(COLOR_DIFF_DEL_LINE, curses.COLOR_RED, -1)
+    curses.init_pair(COLOR_DIFF_OFFSET, curses.COLOR_MAGENTA, -1)
 
     # don't display the cursor
     try:
@@ -1340,16 +1344,27 @@ pgup/K: move patch up, pgdn/J: move patc
                 addln(rulesscr, y, 2, rule)
         rulesscr.noutrefresh()
 
-    def renderstring(win, state, output):
+    def renderstring(win, state, output, patchcolors=False):
         maxy, maxx = win.getmaxyx()
         length = min(maxy - 1, len(output))
         for y in range(0, length):
-            win.addstr(y, 0, output[y])
+            line = output[y]
+            if patchcolors:
+                if line and line[0] == '+':
+                    win.addstr(y, 0, line, curses.color_pair(COLOR_DIFF_ADD_LINE))
+                elif line and line[0] == '-':
+                    win.addstr(y, 0, line, curses.color_pair(COLOR_DIFF_DEL_LINE))
+                elif line.startswith('@@ '):
+                    win.addstr(y, 0, line, curses.color_pair(COLOR_DIFF_OFFSET))
+                else:
+                    win.addstr(y, 0, line)
+            else:
+                win.addstr(y, 0, line)
         win.noutrefresh()
 
     def renderpatch(win, state):
         start = state['modes'][MODE_PATCH]['line_offset']
-        renderstring(win, state, patchcontents(state)[start:])
+        renderstring(win, state, patchcontents(state)[start:], patchcolors=True)
 
     def layout(mode):
         maxy, maxx = stdscr.getmaxyx()


More information about the Mercurial-devel mailing list