nim-asciitables simple terminal ascii tables for n开源项目

我要开发同款
匿名用户2021年12月17日
40阅读
开发技术C/C++
所属分类应用工具、终端/远程登录
授权协议BSD-3-Clause License

作品详情

nim-asciitables

simpleterminalasciitablesfornim

DEPRECATION

projectisdeprecatedinfavorfornim-terminaltables

Howtouse

asciitableshasaverysimpleapi

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|+----------+--------------------------------------------------------------------------------+------------------------------+Why

Icouldn'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:)

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论