Commits

ZyX_I  committed bfe2cd2

Made plugin draw fancy characters in front of each tree item

  • Participants
  • Parent commits af929cf

Comments (0)

Files changed (1)

File plugin/gtregen.vim

     endif
     return r
 endfunction
+"▶1 hasbrother :: collevels, i → Bool
+function s:F.hasbrother(cls, i)
+    let cl=a:cls[a:i]
+    for l in a:cls[(a:i+1):]
+        if l<cl
+            return 0
+        elseif l==cl
+            return 1
+        endif
+    endfor
+    return 0
+endfunction
+"▶1 firstchars :: columns → [String]
+" {down}|({up}<<1)|({right}<<2)|({left}<<3)
+let s:chars=split(' ╷╵│╶┌└├╴┐┘┤─┬┴┼', '\v.@=')
+function s:F.firstchars(columns)
+    let collevels=map(copy(a:columns), 'v:val[0]')
+    let r=[]
+    let i=0
+    let hasbrothers=[0, 0]
+    let prevlevel=0
+    for level in collevels
+        let s=''
+        if i==0
+            if len(collevels)>1
+                let s='┌'
+            else
+                let s='╶'
+            endif
+        else
+            let nextlevel=get(collevels, i+1, 0)
+            let hasbrother=s:F.hasbrother(collevels, i)
+            let hasobrother=get(hasbrothers, level, 0)
+            if len(hasbrothers)-1>=level
+                call remove(hasbrothers, level, -1)
+            endif
+            call add(hasbrothers, hasbrother)
+            let up=(level<prevlevel)
+            let haschild=(level<nextlevel)
+            let numbars=(up?(level):(prevlevel))-1
+            let s.=join(map(range(1, numbars),
+                        \   's:chars[3*hasbrothers[v:val]]'), '')
+            if up
+                let s.=s:chars[(hasbrother||haschild)+2*hasobrother+4]
+            elseif level>prevlevel
+                let s.=s:chars[hasbrothers[level-1]+2+4]
+                let s.=s:chars[(hasbrother||haschild)+4+8]
+            else
+                let s.=s:chars[(hasbrother||haschild)+2+4]
+            endif
+        endif
+        let r+=[s]
+        let i+=1
+        let prevlevel=level
+    endfor
+    return r
+endfunction
 "▶1 setup :: tid → + buffer
 function s:F.setup(tid)
     let tree=s:trees[a:tid]
         let options.colopts[col]={}
     endfor
     "▲2
-    let columns=s:F.gencolumns(tree, tdata, options, 0)
+    let columns=s:F.gencolumns(tree, tdata, options, 1)
     let widths=[]
     let colnum=len(columns[0])
     let i=1
     if totalwidth>winwidth(0)
         " TODO wrapping/truncating (determined by options)
     endif
+    let firstchars=s:F.firstchars(columns)
     let lines=map(copy(columns),
-                \ 'repeat(" ", v:val[0]).'.
-                \ '" ".s:F.align(v:val[1], get(options.colopts['.
-                \                               'options.columns[0]], '.
-                \                             '"align", "left"), '.
-                \                             'widths[0][v:key], '.
-                \                             'colwidths[0]).'.
+                \ 'firstchars[v:key].'.
+                \ 's:F.align(v:val[1], get(options.colopts['.
+                \                           'options.columns[0]], '.
+                \                         '"align", "left"), '.
+                \                         'widths[0][v:key], '.
+                \                         'colwidths[0]).'.
                 \ 'join(map(v:val[2:], '.
                 \          '"\" \".s:F.align(v:val, '.
                 \                           'get(options.colopts['.