comparison hgext/histedit.py @ 42089:16692aa3472b

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.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 03 Apr 2019 23:55:03 -0400
parents 770e87999701
children 9e40c5892714
comparison
equal deleted inserted replaced
42088:770e87999701 42089:16692aa3472b
961 'fold': '^fold', 961 'fold': '^fold',
962 'roll': '^roll', 962 'roll': '^roll',
963 } 963 }
964 964
965 COLOR_HELP, COLOR_SELECTED, COLOR_OK, COLOR_WARN, COLOR_CURRENT = 1, 2, 3, 4, 5 965 COLOR_HELP, COLOR_SELECTED, COLOR_OK, COLOR_WARN, COLOR_CURRENT = 1, 2, 3, 4, 5
966 COLOR_DIFF_ADD_LINE, COLOR_DIFF_DEL_LINE, COLOR_DIFF_OFFSET = 6, 7, 8
966 967
967 E_QUIT, E_HISTEDIT = 1, 2 968 E_QUIT, E_HISTEDIT = 1, 2
968 E_PAGEDOWN, E_PAGEUP, E_LINEUP, E_LINEDOWN, E_RESIZE = 3, 4, 5, 6, 7 969 E_PAGEDOWN, E_PAGEUP, E_LINEUP, E_LINEDOWN, E_RESIZE = 3, 4, 5, 6, 7
969 MODE_INIT, MODE_PATCH, MODE_RULES, MODE_HELP = 0, 1, 2, 3 970 MODE_INIT, MODE_PATCH, MODE_RULES, MODE_HELP = 0, 1, 2, 3
970 971
1247 curses.init_pair(COLOR_HELP, curses.COLOR_WHITE, curses.COLOR_BLUE) 1248 curses.init_pair(COLOR_HELP, curses.COLOR_WHITE, curses.COLOR_BLUE)
1248 curses.init_pair(COLOR_SELECTED, curses.COLOR_BLACK, curses.COLOR_WHITE) 1249 curses.init_pair(COLOR_SELECTED, curses.COLOR_BLACK, curses.COLOR_WHITE)
1249 curses.init_pair(COLOR_WARN, curses.COLOR_BLACK, curses.COLOR_YELLOW) 1250 curses.init_pair(COLOR_WARN, curses.COLOR_BLACK, curses.COLOR_YELLOW)
1250 curses.init_pair(COLOR_OK, curses.COLOR_BLACK, curses.COLOR_GREEN) 1251 curses.init_pair(COLOR_OK, curses.COLOR_BLACK, curses.COLOR_GREEN)
1251 curses.init_pair(COLOR_CURRENT, curses.COLOR_WHITE, curses.COLOR_MAGENTA) 1252 curses.init_pair(COLOR_CURRENT, curses.COLOR_WHITE, curses.COLOR_MAGENTA)
1253 curses.init_pair(COLOR_DIFF_ADD_LINE, curses.COLOR_GREEN, -1)
1254 curses.init_pair(COLOR_DIFF_DEL_LINE, curses.COLOR_RED, -1)
1255 curses.init_pair(COLOR_DIFF_OFFSET, curses.COLOR_MAGENTA, -1)
1252 1256
1253 # don't display the cursor 1257 # don't display the cursor
1254 try: 1258 try:
1255 curses.curs_set(0) 1259 curses.curs_set(0)
1256 except curses.error: 1260 except curses.error:
1343 curses.color_pair(COLOR_CURRENT) | curses.A_BOLD) 1347 curses.color_pair(COLOR_CURRENT) | curses.A_BOLD)
1344 else: 1348 else:
1345 addln(rulesscr, y, 2, rule) 1349 addln(rulesscr, y, 2, rule)
1346 rulesscr.noutrefresh() 1350 rulesscr.noutrefresh()
1347 1351
1348 def renderstring(win, state, output): 1352 def renderstring(win, state, output, diffcolors=False):
1349 maxy, maxx = win.getmaxyx() 1353 maxy, maxx = win.getmaxyx()
1350 length = min(maxy - 1, len(output)) 1354 length = min(maxy - 1, len(output))
1351 for y in range(0, length): 1355 for y in range(0, length):
1352 win.addstr(y, 0, output[y]) 1356 line = output[y]
1357 if diffcolors:
1358 if line and line[0] == '+':
1359 win.addstr(
1360 y, 0, line, curses.color_pair(COLOR_DIFF_ADD_LINE))
1361 elif line and line[0] == '-':
1362 win.addstr(
1363 y, 0, line, curses.color_pair(COLOR_DIFF_DEL_LINE))
1364 elif line.startswith('@@ '):
1365 win.addstr(
1366 y, 0, line, curses.color_pair(COLOR_DIFF_OFFSET))
1367 else:
1368 win.addstr(y, 0, line)
1369 else:
1370 win.addstr(y, 0, line)
1353 win.noutrefresh() 1371 win.noutrefresh()
1354 1372
1355 def renderpatch(win, state): 1373 def renderpatch(win, state):
1356 start = state['modes'][MODE_PATCH]['line_offset'] 1374 start = state['modes'][MODE_PATCH]['line_offset']
1357 renderstring(win, state, patchcontents(state)[start:]) 1375 renderstring(win, state, patchcontents(state)[start:], diffcolors=True)
1358 1376
1359 def layout(mode): 1377 def layout(mode):
1360 maxy, maxx = stdscr.getmaxyx() 1378 maxy, maxx = stdscr.getmaxyx()
1361 helplen = len(helplines(mode)) 1379 helplen = len(helplines(mode))
1362 return { 1380 return {