From fa378e224d93950005027ffc9d2c82d056138fb1 Mon Sep 17 00:00:00 2001 From: Masayuki Igawa Date: Sat, 10 Sep 2016 13:06:10 +0900 Subject: [PATCH] Add trailing-whitespace style This commit adds trailing-whitespace style for emphasizing. In gerrit, we can find a trailing-whitespace very easily because it is emphasized in red. So this commit imitates it. Change-Id: Ie3f35c678093c51d8e39aeeaab2ea17d504e89fb --- gertty/gitrepo.py | 26 ++++++++++++++++++++++---- gertty/palette.py | 1 + 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/gertty/gitrepo.py b/gertty/gitrepo.py index 71b360f..4aed83e 100644 --- a/gertty/gitrepo.py +++ b/gertty/gitrepo.py @@ -303,6 +303,20 @@ class Repo(object): ret.append(x.split('\t')) return ret + trailing_ws_re = re.compile('\s+$') + def _emph_trail_ws(self, style, line): + result = (style, line) + re_result = self.trailing_ws_re.search(line) + if (re_result): + span = re_result.span() + if len(line[:span[0]]) == 0: + ws_line = ('trailing-ws', line) + else: + ws_line = [(style, line[:span[0]]), + ('trailing-ws', line[span[0]:span[1]])] + result = ws_line + return result + def intralineDiff(self, old, new): # takes a list of old lines and a list of new lines prevline = None @@ -341,9 +355,11 @@ class Repo(object): accumulator += c if accumulator: if emphasis: - result.append((prevstyle+'-word', accumulator)) + result.append(self._emph_trail_ws(prevstyle+'-word', + accumulator)) else: - result.append((prevstyle+'-line', accumulator)) + result.append(self._emph_trail_ws(prevstyle+'-line', + accumulator)) if prevstyle == 'added': output_new.append(result) elif prevstyle == 'removed': @@ -352,7 +368,8 @@ class Repo(object): continue if prevline is not None: if prevstyle == 'added' or prevstyle == 'context': - output_new.append((prevstyle+'-line', prevline)) + output_new.append(self._emph_trail_ws(prevstyle+'-line', + prevline)) if prevstyle == 'removed' or prevstyle == 'context': output_old.append((prevstyle+'-line', prevline)) if key == '+': @@ -365,7 +382,8 @@ class Repo(object): #self.log.debug('prev'+repr(prevline)) if prevline is not None: if prevstyle == 'added': - output_new.append((prevstyle+'-line', prevline)) + output_new.append(self._emph_trail_ws(prevstyle+'-line', + prevline)) elif prevstyle == 'removed': output_old.append((prevstyle+'-line', prevline)) #self.log.debug(repr(output_old)) diff --git a/gertty/palette.py b/gertty/palette.py index 645f7de..d9729c6 100644 --- a/gertty/palette.py +++ b/gertty/palette.py @@ -49,6 +49,7 @@ DEFAULT_PALETTE={ 'line-number': ['dark gray', ''], 'focused-line-number': ['dark gray,standout', ''], 'search-result': ['default,standout', ''], + 'trailing-ws': ['light red,standout', ''], # Change view 'change-data': ['dark cyan', ''], 'focused-change-data': ['light cyan', ''],