Add tab function to vim.moon and refactor
vim.column is now called vim.col and has been turned into a wrapper for the tab function. The vim.row wrapper has been added and does what you'd expect. vim.box is also a wrapper for vim.tab now. Some other refactoring has been done.
This commit is contained in:
parent
9ba10aff75
commit
9a8141d73b
1 changed files with 53 additions and 29 deletions
82
lua/vim.moon
82
lua/vim.moon
|
@ -1,4 +1,15 @@
|
|||
-- vim: set noexpandtab :miv --
|
||||
|
||||
-- ┌──────────────────────────┐
|
||||
-- │ Generic Helper Functions │
|
||||
-- └──────────────────────────┘
|
||||
|
||||
match_all = (str, pat, init=0) ->
|
||||
s,e = str\find(pat, init)
|
||||
if s then
|
||||
return str\sub(s,e), match_all(str, pat, e+1)
|
||||
match_lines = (str) -> match_all(str, '[^\n]+')
|
||||
|
||||
tree = (tab, pref='') ->
|
||||
print tab.title and tab.title or '┐'
|
||||
|
||||
|
@ -13,37 +24,49 @@ tree = (tab, pref='') ->
|
|||
else
|
||||
print tostring element
|
||||
|
||||
pad = (str='', len) ->
|
||||
str..string.rep(" ", len-#str)
|
||||
|
||||
-- ┌──────────────────┐
|
||||
-- │ Actual Functions │
|
||||
-- └──────────────────┘
|
||||
|
||||
import max from math
|
||||
column = (col) ->
|
||||
if type(col)!="table" then col = {col}
|
||||
pad = (str, len) ->
|
||||
str..string.rep(" ", len-#str)
|
||||
width = 0
|
||||
for box in *col
|
||||
if type(box)!="table" then box = {box}
|
||||
for elem in *box
|
||||
width = max(width, #elem)
|
||||
|
||||
print "┌─"..string.rep("─",width).."─┐"
|
||||
for idx,box in ipairs(col)
|
||||
if type(box)!="table" then box={box}
|
||||
last = idx==#col
|
||||
for elem in *box
|
||||
io.write "│ "
|
||||
io.write pad elem, width
|
||||
io.write " │"
|
||||
print!
|
||||
unless last
|
||||
print "├─"..string.rep("─",width).."─┤"
|
||||
print "└─"..string.rep("─",width).."─┘"
|
||||
chktbl = (obj) -> error("Object is not a table", 2) if type(obj) ~= 'table'
|
||||
to_tab = (obj) -> type(obj)=='table' and obj or {match_lines tostring obj}
|
||||
|
||||
match_all = (str, pat, init=0) ->
|
||||
s,e = str\find(pat, init)
|
||||
if s then
|
||||
return str\sub(s,e), match_all(str, pat, e+1)
|
||||
tab = (rows) ->
|
||||
-- Get Measurements
|
||||
chktbl(rows)
|
||||
width = {}
|
||||
height = {}
|
||||
for row, fields in ipairs rows
|
||||
chktbl(fields)
|
||||
height[row] = 1
|
||||
for col, lines in ipairs fields
|
||||
lines = to_tab lines
|
||||
height[row] = max(height[row], #lines)
|
||||
for line in *lines do
|
||||
width[col] = max(width[col] or 1, #line)
|
||||
fields[col] = lines
|
||||
bars = [string.rep('─', i+2) for i in *width]
|
||||
-- Top Line
|
||||
print '┌'..table.concat(bars, '┬')..'┐'
|
||||
-- Rows
|
||||
for row, fields in ipairs rows
|
||||
for line=1,height[row]
|
||||
io.write '│' -- Outer Left Border
|
||||
for col, lines in ipairs fields
|
||||
io.write ' '..pad(lines[line], width[col])..' '
|
||||
io.write '│' if col < #fields
|
||||
print '│' -- Outer Left Border
|
||||
print '├'..table.concat(bars, '┼')..'┤' unless row == #rows
|
||||
print '└'..table.concat(bars, '┴')..'┘'
|
||||
|
||||
box = (box) ->
|
||||
column { match_all box, '[^\n]+' }
|
||||
col = (col) -> tab([{elem} for elem in *col])
|
||||
row = (row) -> tab{row}
|
||||
box = (box) -> tab{{box}}
|
||||
|
||||
CLASS = [[
|
||||
print vim.col {
|
||||
|
@ -68,7 +91,8 @@ draw = ->
|
|||
|
||||
|
||||
{
|
||||
:tree, :column, :box, :draw, :CLASS
|
||||
:tree, :box, :draw, :tab, :col, :row
|
||||
:CLASS
|
||||
-- Aliases
|
||||
col: column
|
||||
table: tab
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue