From 55f524de774873a2d8106e01b42b236f8c5bcbb4 Mon Sep 17 00:00:00 2001 From: Bailey Ling Date: Sun, 8 Sep 2013 19:06:57 -0400 Subject: [PATCH] support for conditional parts. resolves #238. --- autoload/airline/parts.vim | 4 ++++ autoload/airline/section.vim | 13 ++++++++++--- doc/airline.txt | 3 +++ t/parts.vim | 5 +++++ t/section.vim | 7 +++++++ 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/autoload/airline/parts.vim b/autoload/airline/parts.vim index 56afc059..f1b7c00a 100644 --- a/autoload/airline/parts.vim +++ b/autoload/airline/parts.vim @@ -26,6 +26,10 @@ function! airline#parts#define_minwidth(key, width) call airline#parts#define(a:key, { 'minwidth': a:width }) endfunction +function! airline#parts#define_condition(key, predicate) + call airline#parts#define(a:key, { 'condition': a:predicate }) +endfunction + function! airline#parts#define_empty(keys) for key in a:keys call airline#parts#define_raw(key, '') diff --git a/autoload/airline/section.vim b/autoload/airline/section.vim index e2d37020..516d000d 100644 --- a/autoload/airline/section.vim +++ b/autoload/airline/section.vim @@ -36,12 +36,19 @@ function! s:create(parts, append) let minwidth = get(part, 'minwidth', 0) if a:append > 0 && idx != 0 - let val .= printf('%%{airline#util#append(%s,%s)}', func, minwidth) + let partval = printf('%%{airline#util#append(%s,%s)}', func, minwidth) elseif a:append < 0 && idx != len(a:parts) - 1 - let val .= printf('%%{airline#util#prepend(%s,%s)}', func, minwidth) + let partval = printf('%%{airline#util#prepend(%s,%s)}', func, minwidth) else - let val .= printf('%%{airline#util#wrap(%s,%s)}', func, minwidth) + let partval = printf('%%{airline#util#wrap(%s,%s)}', func, minwidth) endif + + if exists('part.condition') + let partval = substitute(partval, '{', '{'.(part.condition).' ? ', '') + let partval = substitute(partval, '}', ' : ""}', '') + endif + + let val .= partval let _ .= val endfor return _ diff --git a/doc/airline.txt b/doc/airline.txt index 41c12fd3..ed7d0721 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -370,6 +370,9 @@ Here is how you would define a part that is visible only if the window width greater than a minimum width. > call airline#parts#define_minwidth('foo', 50) < +Parts can be configured to be visible conditionally. > + call airline#parts#define_condition('foo', 'getcwd() =~ "work_dir"') +< Note: Part definitions are combinative; e.g. the two examples above modify the same `foo` part. diff --git a/t/parts.vim b/t/parts.vim index 5891536a..ba4341d7 100644 --- a/t/parts.vim +++ b/t/parts.vim @@ -25,5 +25,10 @@ describe 'parts' call airline#parts#define_minwidth('mw', 123) Expect airline#parts#get('mw').minwidth == 123 end + + it 'can define a condition' + call airline#parts#define_condition('part', '1') + Expect airline#parts#get('part').condition == '1' + end end diff --git a/t/section.vim b/t/section.vim index 44c62889..de9f2081 100644 --- a/t/section.vim +++ b/t/section.vim @@ -55,5 +55,12 @@ describe 'section' let s = airline#section#create_right(['%t', 'asdf', '%{getcwd()}']) Expect s == '%t < asdf < %{getcwd()}' end + + it 'should empty out parts that do not pass their condition' + call airline#parts#define_text('conditional', 'conditional') + call airline#parts#define_condition('conditional', '0') + let s = airline#section#create(['conditional']) + Expect s == '%{0 ? airline#util#wrap("conditional",0) : ""}' + end end