simpleterminalasciitablesfornim
DEPRECATIONprojectisdeprecatedinfavorfornim-terminaltables
Howtouseasciitableshasaverysimpleapi
setHeaderstosetcolumnnamesaddRowtoaddarowtothetableaddRowstoaddmultiplerowsatoncesuggestWidthstosuggestwidthsforeachcolumnoptionsandstylescellEdgecontrolsthethecornersofeachcelldefaultis+colSeparatoristheseparatoraftereachcolumnsetto|bydefaultrowSeparatoristheseparatorbetweenrowsandissetto-bydefaultseparateRowsisoptiontoaddarowseparatorafterrenderingeachrowornot.ExampleswhenisMainModule:vart=newAsciiTable()t.tableWidth=80t.setHeaders(@["ID","Name","Date"])t.addRow(@["1","Aaaa","2018-10-2"])t.addRow(@["2","bbvbbba","2018-10-2"])t.addRow(@["399","CCC","1018-5-2"])printTable(t)shouldrender
+---------------------------+---------------------------+---------------------------+|ID|Name|Date|+---------------------------+---------------------------+---------------------------+|1|Aaaa|2018-10-2|+---------------------------+---------------------------+---------------------------+|2|bbvbbba|2018-10-2|+---------------------------+---------------------------+---------------------------+|399|CCC|1018-5-2|+---------------------------+---------------------------+---------------------------+t.tableWidth=0printTable(t)+---+-------+---------+|ID|Name|Date|+---+-------+---------+|1|Aaaa|2018-10-2|+---+-------+---------+|2|bbvbbba|2018-10-2|+---+-------+---------+|399|CCC|1018-5-2|+---+-------+---------+andifyoudon'twantseparateRows
t.tableWidth=0t.separateRows=falseprintTable(t)+---+-------+---------+|ID|Name|Date|+---+-------+---------+|1|Aaaa|2018-10-2||2|bbvbbba|2018-10-2||399|CCC|1018-5-2|+---+-------+---------+andtosuggestwidthsforcolumns
t.reset()t.suggestWidths(@[10,80,30])t.setHeaders(@["ID","Name","Date"])t.addRow(@["1","Aaaa","2018-10-2"])t.addRow(@["2","bbvbbba","2018-10-2"])t.addRow(@["399","CCC","1018-5-2"])printTable(t)youwillsee
+----------+--------------------------------------------------------------------------------+------------------------------+|ID|Name|Date|+----------+--------------------------------------------------------------------------------+------------------------------+|1|Aaaa|2018-10-2|+----------+--------------------------------------------------------------------------------+------------------------------+|2|bbvbbba|2018-10-2|+----------+--------------------------------------------------------------------------------+------------------------------+|399|CCC|1018-5-2|+----------+--------------------------------------------------------------------------------+------------------------------+WhyIcouldn'tfindanyterminalasciitablelibraryfornimandIfoundmyselfwritinghorriblecodelikethis
varwidths=@[0,0,0,0]#id,name,ports,rootfork,vininfo:iflen($v.id)>widths[0]:widths[0]=len($v.id)iflen($v.name)>widths[1]:widths[1]=len($v.name)iflen($v.ports)>widths[2]:widths[2]=len($v.ports)iflen($v.root)>widths[3]:widths[3]=len($v.root)varsumWidths=0forwinwidths:sumWidths+=wecho"-".repeat(sumWidths)letextraPadding=5echo"|ID"&"".repeat(widths[0]+extraPadding-4)&"|Name"&"".repeat(widths[1]+extraPadding-6)&"|Ports"&"".repeat(widths[2]+extraPadding-6)&"|Root"&"".repeat(widths[3]-6)echo"-".repeat(sumWidths)fork,vininfo:letnroot=replace(v.root,"https://hub.grid.tf/","").strip()echo"|"&$v.id&"".repeat(widths[0]-len($v.id)-1+extraPadding)&"|"&v.name&"".repeat(widths[1]-len(v.name)-1+extraPadding)&"|"&v.ports&"".repeat(widths[2]-len(v.ports)+extraPadding)&"|"&nroot&"".repeat(widths[3]-len(v.root)+extraPadding-2)&"|"echo"-".repeat(sumWidths)result=""Pullrequestsareverywelcome:)
评论