ZyX_I avatar ZyX_I committed 1b27d94

Added @/table module

Comments (0)

Files changed (11)

         4.1. map resource                         |frawor-r-map|
         4.2. os resource                          |frawor-r-os|
         4.3. py resource                          |frawor-r-py|
+        4.4. printtable resource                  |frawor-r-printtable|
     5. Options                                    |frawor-options|
         5.1. Mapping options                      |frawor-mapoptions|
     6. Type definitions                           |frawor-types|
         Run python command or list of commands. Use additional arguments to 
         make your internal variables available inside python command.
 
+------------------------------------------------------------------------------
+4.4. printtable resource                                 *frawor-r-printtable*
+
+Resource `printtable' is defined in plugin/frawor/table. It is a single 
+function which is used for outputting table data.
+Usage: s:_r.printtable({lines}[, {opts}]), where {lines} is a list of lists of 
+strings each list representing a line and each string in a list representing 
+column contents: >
+    :call s:_r.printtable([['foo', 'bar', 'baz'], ['f', 'b']])
+    foo  bar  baz
+    f    b
+{opts} is an optional dictionary with the following keys:
+Key          Description ~
+header       Defines a table header (it has just the same format as a single 
+             line). It is highlighted by PreProc group by default: >
+             :call s:_r.printtable([['a', 'b']], {'header': ['A', 'B']})
+             A  B
+             a  b
+vseparator   Defines a vertical separator between columns: >
+             :let lines=[['a', 'b', 'c'], ['d', 'e', 'f']]
+             :call s:_r.printtable(lines, {'vseparator': ' | '})
+             a | b | c
+             d | e | f
+vseparators  List of separators: >
+             :call s:_r.printtable(lines, {'vseparators': [' : ', ' | ']})
+             a : b | c
+             d : e | f
+<            Instead of specifing separator as a string you may specify 
+             a two-tuple (String, hlgroup). In this case separator will be 
+             highlighted. Separotor highlight group is discarded when printing 
+             table header.
+align        Defines align for all columns (but not for header line). Possible 
+             values: "left" (default), "center", "right": >
+             :let lines2=[['abc', 'def', 'ghi'], ['a', 'b', 'c']]
+             :call s:_r.printtable(lines2, {'align': 'center'})
+             abc  def  ghi
+              a    b    c
+aligns       List of aligns: >
+             :call s:_r.printtable(lines2,
+                                  \{'aligns': ['right', 'center', 'left']})
+             abc  def  ghi
+               a   b   c
+halign, haligns
+             Like align and aligns, but for header.
+hl, hls, hhl, hhls
+             Defines higlight groups for columns. `hl' defines one hl group 
+             for all data in table, `hhl' overrides PreProc highlighting for 
+             header. `hls' and `hhls' have the same relation to `hl' and `hhl' 
+             as `aligns' has to `align'.
+
 ==============================================================================
 5. Options                                                    *frawor-options*
                                                              *g:fraworOptions*

plugin/frawor/table.vim

+"▶1 Header
+scriptencoding utf-8
+execute frawor#Setup('0.0', {'@/resources': '0.0'}, 1)
+"▶1 strdisplaywidth  :: String, column → UInt
+if exists('*strdisplaywidth')
+    let s:F.strdisplaywidth=function('strdisplaywidth')
+else
+    function s:F.strdisplaywidth(str, col)
+        let chars=split(a:str, '\v.@=')
+        let curcol=a:col
+        for char in chars
+            if char[0] is# "\t"
+                let curcol+=(&ts-curcol%&ts)
+            " TODO fullwidth characters?
+            else
+                let curcol+=1
+            endif
+        endfor
+        return curcol-a:col
+    endfunction
+endif
+"▶1 printstr         :: len, String, col, align, hl → col + :echon
+function s:F.printstr(len, str, col, align, hl)
+    if a:hl isnot 0
+        execute 'echohl' a:hl
+    endif
+    let width=s:F.strdisplaywidth(a:str, a:col)
+    if width>=a:len
+        echon a:str
+        if a:hl isnot 0
+            echohl NONE
+        endif
+        return width
+    endif
+    let spacenum=a:len-s:F.strdisplaywidth(a:str, a:col)
+    if a:align is# 'left'
+        echon a:str . repeat(' ', spacenum)
+    elseif a:align is# 'right'
+        echon repeat(' ', spacenum) . a:str
+    else
+        let lspn=spacenum/2
+        let rspn=spacenum-lspn
+        echon repeat(' ', lspn) . a:str . repeat(' ', rspn)
+    endif
+    if a:hl isnot 0
+        echohl NONE
+    endif
+    return width+spacenum
+endfunction
+"▶1 printtline       :: cols, aligns, seps, lengths, hlgroups, colnum → + :echo
+function s:F.printtline(columns, aligns, vseparators, lengths, hlgroups, colnum)
+    let i=0
+    let col=0
+    while i<a:colnum
+        let col+=s:F.printstr(get(a:lengths, i, 0), get(a:columns, i, ''), col,
+                    \         get(a:aligns, i, 'left'), get(a:hlgroups, i, 0))
+        let [separator, sephl]=get(a:vseparators, i, ['  ', 0])
+        let col+=s:F.printstr(1, separator, col, 'center', sephl)
+        let i+=1
+    endwhile
+    return col
+endfunction
+"▶1 printtable       :: lines[, opts] → + :echo
+function s:F.printtable(lines, ...)
+    let opts=get(a:000, 0, {})
+    let lineswh=get(opts, 'header', [])+a:lines
+    let colnum=max(map(copy(lineswh), 'len(v:val)'))
+    if has_key(opts, 'vseparator')
+        let vseparators=repeat([opts.vseparator], colnum-1)
+    elseif has_key(opts, 'vseparators')
+        let vseparators=copy(opts.vseparators)
+    else
+        let vseparators=[]
+    endif
+    call map(vseparators, 'type(v:val)=='.type('').'?[v:val, 0]:v:val')
+    let lengths=[]
+    let i=0
+    let col=0
+    while i<colnum
+        call add(lengths, max(map(copy(lineswh),
+                    \   '(i<len(v:val))?s:F.strdisplaywidth(v:val[i], col):0')))
+        let col+=lengths[-1]
+        let col+=s:F.strdisplaywidth(get(vseparators, i, ['  ', 0])[0], col)
+        let i+=1
+    endwhile
+    " TODO split columns
+    if has_key(opts, 'header')
+        if has_key(opts, 'halign')
+            let haligns=repeat([opts.halign], colnum)
+        elseif has_key(opts, 'haligns')
+            let haligns=opts.haligns
+        else
+            let haligns=[]
+        endif
+        echo ''
+        execute 'echohl' get(opts, 'hhl', 'PreProc')
+        let hvseparators=map(copy(vseparators), '[v:val[0], 0]')
+        call s:F.printtline(opts.header, haligns, hvseparators, lengths,
+                    \       get(opts, 'hhls', []), colnum)
+        echohl NONE
+    endif
+    if has_key(opts, 'align')
+        let aligns=repeat([opts.align], colnum)
+    elseif has_key(opts, 'aligns')
+        let aligns=opts.aligns
+    else
+        let aligns=[]
+    endif
+    if has_key(opts, 'hl')
+        execute 'echohl' opts.hl
+    endif
+    for line in a:lines
+        echo ''
+        call s:F.printtline(line, aligns, vseparators, lengths,
+                    \       get(opts, 'hls', []), colnum)
+    endfor
+    if has_key(opts, 'hl')
+        echohl NONE
+    endif
+endfunction
+"▶1 Register resource
+call s:_f.postresource('printtable', s:F.printtable)
+"▶1
+call frawor#Lockvar(s:, '')
+" vim: fmr=▶,▲ sw=4 ts=4 sts=4 et tw=80

test/global-functions-reload.ok

 1      echo 1
    endfunction
 
-   function <SNR>25_testFunction()
+   function <SNR>26_testFunction()
 1      echo 2
    endfunction
 
 1      echo 1
    endfunction
 
-   function <SNR>25_testFunction()
+   function <SNR>26_testFunction()
 1      echo 2
    endfunction

test/ignoredeps-feature.ok

 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua

test/reload-frawor.ok

 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua
 unloadpre: plugin/frawor/perl
 unloadpre: plugin/frawor/python
 unloadpre: plugin/frawor/ruby
+unloadpre: plugin/frawor/table
 unloadpre: plugin/frawor/tcl
 unloadpre: plugin/ignoredeps-feature
 unloadpre: plugin/frawor/options
 unload: plugin/frawor/perl
 unload: plugin/frawor/python
 unload: plugin/frawor/ruby
+unload: plugin/frawor/table
 unload: plugin/frawor/tcl
 unload: plugin/ignoredeps-feature
 _unload
 load: plugin/writefile-feature
 register: plugin/frawor/tcl
 load: plugin/frawor/tcl
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/ruby
 load: plugin/frawor/ruby
 register: plugin/frawor/python

test/reload-ignoredeps.ok

 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua
 _unload
 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua

test/reload-writefile.ok

 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua
 _unload
 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua

test/rtp/plugin/table.vim

+execute frawor#Setup('0.0', {'@/table': '0.0'}, 1)
+hi PreProc ctermfg=Blue
+hi User1   ctermfg=Red
+hi User2   ctermfg=Green
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']])
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C'],
+            \                          'halign': 'center'})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C'],
+            \                         'haligns': ['left', 'center', 'right']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C'],
+            \                         'haligns': ['left', 'center', 'right']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-'],
+            \         ['+', '-', ':']], {'header': ['A', 'B', 'C'],
+            \                            'aligns': ['left', 'center', 'right']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-'],
+            \         ['+', '-', ':']], {'header': ['A', 'B', 'C'],
+            \                             'align': 'center'})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-'],
+            \         ['+', '-', ':']], {'header': ['A', 'B', 'C'],
+            \                       'vseparators': [' | ', ' + ']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-'],
+            \         ['+', '-', ':']], {'header': ['A', 'B', 'C'],
+            \                        'vseparator': ' | '})
+
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C'], 'hl': 'User1'})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-']], {'header': ['A', 'B', 'C'], 'hls': ['User1', 'User2']})
+call s:_r.printtable([['abc def', 'ghi', 'jklmno'],
+            \         ["a\ts", '-'],
+            \         ['+', '-', ':']], {'header': ['A', 'B', 'C'],
+            \                       'vseparators': [[' | ', 'User2'], ' + ']})
+:let &rtp.=",".escape($TESTDIR, ',\').'/rtp'
+:let g:testfile="plugin/".g:curtest.".vim"
+:source test.vim
+>>> messages
+abc def    ghi  jklmno  
+a	s  -            
+A          B    C       
+abc def    ghi  jklmno  
+a	s  -            
+    A       B     C     
+abc def    ghi  jklmno  
+a	s  -            
+A           B        C  
+abc def    ghi  jklmno  
+a	s  -            
+A           B        C  
+abc def    ghi  jklmno  
+a	s  -            
+A          B    C       
+abc def    ghi  jklmno  
+a	s   -           
++           -        :  
+A          B    C       
+ abc def   ghi  jklmno  
+a	s   -           
+    +       -     :     
+A         | B   + C       
+abc def   | ghi + jklmno  
+a	s | -   +         
++         | -   + :       
+A         | B   | C       
+abc def   | ghi | jklmno  
+a	s | -   |         
++         | -   | :       
+A          B    C       
+abc def    ghi  jklmno  
+a	s  -            
+A          B    C       
+abc def    ghi  jklmno  
+a	s  -            
+A         | B   + C       
+abc def   | ghi + jklmno  
+a	s | -   +         
++         | -   + :       
+<<< messages

test/unload-self.ok

 register: plugin/frawor/autocommands
 load: plugin/frawor/autocommands
+register: plugin/frawor/table
+load: plugin/frawor/table
 register: plugin/frawor/checks
 load: plugin/frawor/checks
 register: plugin/frawor/lua
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.