util: Add optional color support with AnsiEsc

This commit is contained in:
Tomas Babej 2015-03-21 16:51:06 +01:00
parent 2e2d19eaed
commit f22484f8d7
4 changed files with 32 additions and 1 deletions

View file

@ -8,6 +8,7 @@ augroup taskwiki
augroup END augroup END
command! TaskWikiProjects :py Splits.projects() command! TaskWikiProjects :py Splits.projects()
command! TaskWikiProjectsSummary :py Splits.summary()
command! -range TaskWikiInfo :<line1>,<line2>py SelectedTasks().info() command! -range TaskWikiInfo :<line1>,<line2>py SelectedTasks().info()
command! -range TaskWikiLink :<line1>,<line2>py SelectedTasks().link() command! -range TaskWikiLink :<line1>,<line2>py SelectedTasks().link()

View file

@ -54,3 +54,10 @@ GENERIC_VIEWPORT = re.compile(
')?' ')?'
'[=]+' # Header ending '[=]+' # Header ending
) )
ANSI_ESCAPE_SEQ = re.compile(
'\x1b' # literal ESC
'\[' # literal [
'[;\d]*' # zero or more digits or semicolons
'[A-Za-z]' # a letter
)

View file

@ -57,6 +57,11 @@ class Splits(object):
output = tw.execute_command(['projects']) output = tw.execute_command(['projects'])
util.show_in_split(output, name="projects", vertical=True) 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): class SelectedTasks(object):
def __init__(self): def __init__(self):

View file

@ -1,5 +1,10 @@
# Various utility functions # Various utility functions
import vim # pylint: disable=F0401 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): def tw_modstring_to_args(line):
output = [] output = []
@ -87,13 +92,17 @@ def get_current_line_number():
def selected_line_numbers(): def selected_line_numbers():
return range(vim.current.range.start, vim.current.range.end + 1) 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, def show_in_split(lines, size=None, position="belowright", vertical=False,
name="taskwiki"): name="taskwiki"):
# Compute the size of the split # Compute the size of the split
if size is None: if size is None:
if vertical: if vertical:
# Maximum number of columns used + small offset # 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: else:
# Number of lines # Number of lines
size = len(lines) size = len(lines)
@ -116,3 +125,12 @@ def show_in_split(lines, size=None, position="belowright", vertical=False,
# Make the split easily closable # Make the split easily closable
vim.command("nnoremap <silent> <buffer> q :bd<CR>") vim.command("nnoremap <silent> <buffer> q :bd<CR>")
vim.command("nnoremap <silent> <buffer> <enter> :bd<CR>") vim.command("nnoremap <silent> <buffer> <enter> :bd<CR>")
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)