diff --git a/ftplugin/vimwiki.vim b/ftplugin/vimwiki.vim index cbd8c05..cdcb0fe 100644 --- a/ftplugin/vimwiki.vim +++ b/ftplugin/vimwiki.vim @@ -8,6 +8,7 @@ augroup taskwiki augroup END command! TaskWikiProjects :py Splits.projects() +command! TaskWikiProjectsSummary :py Splits.summary() command! -range TaskWikiInfo :,py SelectedTasks().info() command! -range TaskWikiLink :,py SelectedTasks().link() diff --git a/taskwiki/regexp.py b/taskwiki/regexp.py index 175d2f9..073aa35 100644 --- a/taskwiki/regexp.py +++ b/taskwiki/regexp.py @@ -54,3 +54,10 @@ GENERIC_VIEWPORT = re.compile( ')?' '[=]+' # Header ending ) + +ANSI_ESCAPE_SEQ = re.compile( + '\x1b' # literal ESC + '\[' # literal [ + '[;\d]*' # zero or more digits or semicolons + '[A-Za-z]' # a letter + ) diff --git a/taskwiki/taskwiki.py b/taskwiki/taskwiki.py index 1b045d2..59086c9 100644 --- a/taskwiki/taskwiki.py +++ b/taskwiki/taskwiki.py @@ -57,6 +57,11 @@ class Splits(object): output = tw.execute_command(['projects']) util.show_in_split(output, name="projects", vertical=True) + @staticmethod + def summary(): + output = util.tw_execute_colorful(tw, ['summary']) + util.show_in_split(output, name="summary", vertical=True) + class SelectedTasks(object): def __init__(self): diff --git a/taskwiki/util.py b/taskwiki/util.py index abb9a3a..9c07c8f 100644 --- a/taskwiki/util.py +++ b/taskwiki/util.py @@ -1,5 +1,10 @@ # Various utility functions import vim # pylint: disable=F0401 +import regexp + +# Detect if command AnsiEsc is available +ANSI_ESC_AVAILABLE = vim.eval('exists(":AnsiEsc")') == '2' + def tw_modstring_to_args(line): output = [] @@ -87,13 +92,17 @@ def get_current_line_number(): def selected_line_numbers(): return range(vim.current.range.start, vim.current.range.end + 1) +def strip_ansi_escape_sequence(string): + return regexp.ANSI_ESCAPE_SEQ.sub("", string) + def show_in_split(lines, size=None, position="belowright", vertical=False, name="taskwiki"): # Compute the size of the split if size is None: if vertical: # Maximum number of columns used + small offset - size = max([len(l) for l in lines]) + 5 + # Strip the color codes, since they do not show up in the split + size = max([len(strip_ansi_escape_sequence(l)) for l in lines]) + 5 else: # Number of lines size = len(lines) @@ -116,3 +125,12 @@ def show_in_split(lines, size=None, position="belowright", vertical=False, # Make the split easily closable vim.command("nnoremap q :bd") vim.command("nnoremap :bd") + + if ANSI_ESC_AVAILABLE: + vim.command("AnsiEsc") + +def tw_execute_colorful(tw, *args, **kwargs): + if ANSI_ESC_AVAILABLE: + override = kwargs.setdefault('config_override', {}) + override['_forcecolor'] = "yes" + return tw.execute_command(*args, **kwargs)