From 62da3ebc08949d60a9d7b6ce84d9fe6ed68110db Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Wed, 14 Nov 2012 00:48:46 +0000 Subject: [PATCH] Use heuristics to decide what to colorize In practise pygments seems to have a very hard time processing large files and files with long lines, so try to avoid using it in those cases. Signed-off-by: Alberto Bertogli --- git-arr | 2 +- utils.py | 24 ++++++++++++++++++++++-- views/blob.html | 2 +- views/commit.html | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/git-arr b/git-arr index 8688c12..3a44427 100755 --- a/git-arr +++ b/git-arr @@ -158,7 +158,7 @@ def with_utils(f): """ utilities = { 'shorten': utils.shorten, - 'has_colorizer': utils.has_colorizer, + 'can_colorize': utils.can_colorize, 'colorize_diff': utils.colorize_diff, 'colorize_blob': utils.colorize_blob, 'abort': bottle.abort, diff --git a/utils.py b/utils.py index 3bd281f..cee4bb7 100644 --- a/utils.py +++ b/utils.py @@ -18,8 +18,28 @@ def shorten(s, width = 60): return s return s[:57] + "..." -def has_colorizer(): - return pygments is not None +def can_colorize(s): + """True if we can colorize the string, False otherwise.""" + if pygments is None: + return False + + # Pygments can take a huge amount of time with long files, or with very + # long lines; these are heuristics to try to avoid those situations. + if len(s) > (512 * 1024): + return False + + # If any of the first 5 lines is over 300 characters long, don't colorize. + start = 0 + for i in range(5): + pos = s.find('\n', start) + if pos == -1: + break + + if pos - start > 300: + return False + start = pos + 1 + + return True def colorize_diff(s): lexer = lexers.DiffLexer(encoding = 'utf-8') diff --git a/views/blob.html b/views/blob.html index 4d5f7d0..283bbcd 100644 --- a/views/blob.html +++ b/views/blob.html @@ -36,7 +36,7 @@ {{!fname.html}} -% if has_colorizer(): +% if can_colorize(blob): {{!colorize_blob(fname.unicode, blob)}} % else:
diff --git a/views/commit.html b/views/commit.html
index 9a9e99d..bc3119c 100644
--- a/views/commit.html
+++ b/views/commit.html
@@ -55,7 +55,7 @@
 
 
-% if has_colorizer(): +% if can_colorize(c.diff.body): {{!colorize_diff(c.diff.body)}} % else: