nim-asciitables

simple terminal ascii tables for nim
授权协议 BSD-3-Clause License
开发语言 C/C++
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 严宇
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

nim-asciitables

simple terminal ascii tables for nim

DEPRECATION

project is deprecated in favor for nim-terminaltables

How to use

asciitables has a very simple api

  • setHeaders to set column names
  • addRow to add a row to the table
  • addRows to add multiple rows at once
  • suggestWidths to suggest widths for each column

options and styles

  • cellEdge controls the the corners of each cell default is +
  • colSeparator is the separator after each column set to | by default
  • rowSeparator is the separator between rows and is set to - by default
  • separateRows is option to add a row separator after rendering each row or not.

Examples

when isMainModule:
  var t = newAsciiTable()
  t.tableWidth = 80
  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)

should render

+---------------------------+---------------------------+---------------------------+
|ID                         |Name                       |Date                       |
+---------------------------+---------------------------+---------------------------+
|1                          |Aaaa                       |2018-10-2                  |
+---------------------------+---------------------------+---------------------------+
|2                          |bbvbbba                    |2018-10-2                  |
+---------------------------+---------------------------+---------------------------+
|399                        |CCC                        |1018-5-2                   |
+---------------------------+---------------------------+---------------------------+

t.tableWidth = 0
  printTable(t)
+---+-------+---------+
|ID |Name   |Date     |
+---+-------+---------+
|1  |Aaaa   |2018-10-2|
+---+-------+---------+
|2  |bbvbbba|2018-10-2|
+---+-------+---------+
|399|CCC    |1018-5-2 |
+---+-------+---------+

and if you don't want separateRows

t.tableWidth = 0
  t.separateRows = false
  printTable(t)
+---+-------+---------+
|ID |Name   |Date     |
+---+-------+---------+
|1  |Aaaa   |2018-10-2|
|2  |bbvbbba|2018-10-2|
|399|CCC    |1018-5-2 |
+---+-------+---------+

and to suggest widths for columns

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)

you will see

+----------+--------------------------------------------------------------------------------+------------------------------+
|ID        |Name                                                                            |Date                          |
+----------+--------------------------------------------------------------------------------+------------------------------+
|1         |Aaaa                                                                            |2018-10-2                     |
+----------+--------------------------------------------------------------------------------+------------------------------+
|2         |bbvbbba                                                                         |2018-10-2                     |
+----------+--------------------------------------------------------------------------------+------------------------------+
|399       |CCC                                                                             |1018-5-2                      |
+----------+--------------------------------------------------------------------------------+------------------------------+

Why

I couldn't find any terminal ascii table library for nim and I found myself writing horrible code like this

var widths = @[0,0,0,0]  #id, name, ports, root
      for k, v in info:
        if len($v.id) > widths[0]:
          widths[0] = len($v.id)
        if len($v.name) > widths[1]:
          widths[1] = len($v.name)
        if len($v.ports) > widths[2]:
          widths[2] = len($v.ports)
        if len($v.root) > widths[3]:
          widths[3] = len($v.root)
      
      var sumWidths = 0
      for w in widths:
        sumWidths += w
      
      echo "-".repeat(sumWidths)

      let extraPadding = 5
      echo "| 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)
  

      for k, v in info:
        let nroot = 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 = ""

Pull requests are very welcome :)

 相关资料
  • Nim

    Nimrod(已改名为 Nim)是一种静态类型的编译系统编程语言。它结合了 Python、Ada和Modula等成熟语言的成功理念。 高效的 Nim生成不依赖于虚拟机的本机无依赖可执行文件,这些可执行文件很小并且允许重新分配。 Nim编译器和生成的可执行文件支持所有主要平台,例如Windows,Linux,BSD和macOS。 在C ++和Rust的启发下,Nim的内存管理是确定性的,并且可以使用

  • nim-terminaltables terminal tables for nim API API docs available hereterminaltables has a very small API newUnicodeTable uses unicodeStyle newAsciiTable uses asciiStyle Table style is configurable us

  • Dashing is a library to quickly create terminal-based dashboards in Nim. Dashing provides high-level components: vertical and horizontal charts, gauges, log panes, text windows and screen splitting. I

  • Gatabase Works with ARC, ORC, --panics:on, --experimental:strictFuncs. Use Gatabase is designed as 1 simplified Strong Static Typed Connection-Pooling Compile-Time SQL DSL Sugar. Nim mimics SQL. ~1000

  • allographer An asynchronous query builder library inspired by Laravel/PHP and Orator/Python for Nim Easy to access Rdb Query Builder import asyncdispatchimport allographer/connectionimport allographer

  • libssh2.nim Nim wrapper for libssh2 For document please refer to libssh2 website Requirements In order to use this wrapper, libssh2 must installed on your system: Mac OSX:$ port install libssh2 Ubuntu

相关阅读

相关文章

相关问答

相关文档