Commits

ZyX_I committed 0a46bc5

Added support for column titles

  • Participants
  • Parent commits ce7d397

Comments (0)

Files changed (2)

File macros/fstree.vim

     let tdata.nodes=s:F.gennodes
     return tdata
 endfunction
-let s:columns.size={}
-function s:columns.size.function(tdata)
+function s:columns.size(tdata)
     let path=s:_r.os.path.join(a:tdata.path)
     return getfsize(path)
 endfunction
-let s:columns.permissions={}
-function s:columns.permissions.function(tdata)
+function s:columns.permissions(tdata)
     let path=s:_r.os.path.join(a:tdata.path)
     return getfperm(path)
 endfunction
             \                   'coldefault': ['name', 'size'],
             \                      'options': {'colopts':
             \                                   {'size':
-            \                                       {'align': 'right'}},
+            \                                       {'align': 'right',
+            \                                        'title': 'Size',
+            \                                       'talign': 'left'}},
             \                                  'treechars': ' ╻╹┃╺┏┗┣╸┓┛┫━┳┻╋',}})
 call frawor#Lockvar(s:, '')

File plugin/gtregen.vim

     let a:fdict[a:tid]=tree
     let s:trees[a:tid]=tree
 endfunction
+let s:aligns=['left', 'right', 'center']
 let s:optcheck='dict {columns   list type ""'.
-            \       ' colopts   dict {- dict {align (|in [left right'.
-            \                                           ' center])'.
-            \                               ' long  (|in [truncate wrap '.
+            \       ' colopts   dict {- dict {align  (in aligns)'.
+            \                               ' long   (in [truncate wrap '.
             \                                            'ignore])'.
-            \                               ' minw  range 0 inf'.
+            \                               ' minw   range 0 inf'.
+            \                               ' title  type ""'.
+            \                               ' talign (in aligns)'.
             \                               '}}'.
             \       ' treechars (type ""  match /\v^%(%(\t)@!\p){16}$/ '.
             \                   '?=(@%@.p._r.strdisplaywidth(@.@)==16))'.
             \['_ _ (match @\v^[^:/\x0a]+$@ #treeex(.) not key trees) '.
             \     'isfunc '.
             \     'dict {columns '.
-            \               '(dict {?match /\v^\w+$/ '.
-            \                        '(haskey function '.
-            \                         'dict {function  (|isfunc) '.
-            \                               'header    type ""})'.
-            \                     '}) '.
+            \               '(dict {?match /\v^\w+$/  (|isfunc)}) '.
             \           'coldefault  (value @^ haskey columns '.
             \                        'list either is "name", key @^^.columns)'.
             \           'write       (|isfunc) '.
 let s:tree.cons.del=s:_f.wrapfunc(s:tree.cons.del)
 "▶1 Register feature
 call s:_f.newfeature('tree', s:tree)
-"▶1 gencolumns :: tree, tdata, options, level → [[String]]
+"▶1 gencolumns :: tree, tdata, options, level → [Number:[String]]
 function s:F.gencolumns(tree, tdata, options, level)
     let columns=copy(a:options.columns)
     let r=[[a:level]+map(copy(columns),
                 \        '((has_key(a:tdata.columns, v:val))?'.
                 \               '(a:tdata.columns[v:val]):'.
-                \               '(a:tree.columns[v:val].function(a:tdata)))')]
+                \               '(call(a:tree.columns[v:val], [a:tdata],{})))')]
     if has_key(a:tdata, 'nodes')
         if type(a:tdata.nodes)==2
             let a:tdata.nodes_generator=a:tdata.nodes
     let prevlevel=0
     for level in collevels
         let s=''
-        if i==0
+        if level==0
+        elseif level==1
             let s=a:chars[(len(collevels)>1)+4]
         else
             let nextlevel=get(collevels, i+1, 0)
     endfor
     "▲2
     let columns=s:F.gencolumns(tree, tdata, options, 1)
+    "▶2 Get column titles
+    let hastitles=0
+    let titles=map(copy(options.columns),
+                \  'get(options.colopts[v:val], "title", "")')
+    if !empty(filter(copy(titles), '!empty(v:val)'))
+        call insert(columns, [0]+titles)
+        let hastitles=1
+    endif
+    "▶2 Get column widths
     let widths=[]
     let colnum=len(columns[0])
     let i=1
         let totalwidth+=colwidths[-1]
         let i+=1
     endwhile
+    "▶2 TODO Wrap/truncate
     if totalwidth>winwidth(0)
-        " TODO wrapping/truncating (determined by options)
     endif
+    "▶2 Get titles line
+    if hastitles
+        let hwidths=map(copy(widths), 'remove(v:val, 0)')
+        call remove(columns, 0)
+        let hline=join(map(copy(titles),
+                    \      's:F.align(v:val, get(options.colopts['.
+                    \                               'options.columns[v:key]], '.
+                    \                           '"talign", "left"), '.
+                    \                'hwidths[v:key], colwidths[v:key])'))
+    endif
+    "▶2 Get other lines
     let firstchars=s:F.firstchars(columns, options.treechars)
     let lines=map(copy(columns),
                 \ 'firstchars[v:key].'.
                 \                               '\"align\", \"left\"), '.
                 \                               'widths[v:key+1][".v:key."], '.
                 \                               'colwidths[v:key+1])"), "")')
-    " TODO Header
+    "▲2
+    if hastitles
+        call insert(lines, hline)
+    endif
     setlocal modifiable noreadonly
     call setline('.', lines)
     setlocal nomodified nomodifiable readonly