A go library to improve readability in terminal apps using tabular data

Overview

uitable GoDoc Build Status

uitable is a go library for representing data as tables for terminal applications. It provides primitives for sizing and wrapping columns to improve readability.

Example Usage

Full source code for the example is available at example/main.go

table := uitable.New()
table.MaxColWidth = 50

table.AddRow("NAME", "BIRTHDAY", "BIO")
for _, hacker := range hackers {
  table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio)
}
fmt.Println(table)

Will render the data as:

NAME          BIRTHDAY          BIO
Ada Lovelace  December 10, 1815 Ada was a British mathematician and writer, chi...
Alan Turing   June 23, 1912     Alan was a British pioneering computer scientis...

For wrapping in two columns:

table = uitable.New()
table.MaxColWidth = 80
table.Wrap = true // wrap columns

for _, hacker := range hackers {
  table.AddRow("Name:", hacker.Name)
  table.AddRow("Birthday:", hacker.Birthday)
  table.AddRow("Bio:", hacker.Bio)
  table.AddRow("") // blank
}
fmt.Println(table)

Will render the data as:

Name:     Ada Lovelace
Birthday: December 10, 1815
Bio:      Ada was a British mathematician and writer, chiefly known for her work on
          Charles Babbage's early mechanical general-purpose computer, the Analytical
          Engine

Name:     Alan Turing
Birthday: June 23, 1912
Bio:      Alan was a British pioneering computer scientist, mathematician, logician,
          cryptanalyst and theoretical biologist

Installation

$ go get -v github.com/gosuri/uitable

Bitdeli Badge

Comments
  • Support wide characters

    Support wide characters

    diff --git a/example/main.go b/example/main.go
    index c59b2e7..3200da5 100644
    --- a/example/main.go
    +++ b/example/main.go
    @@ -17,10 +17,10 @@ var hackers = []hacker{
    
     func main() {
        table := uitable.New()
    -   table.MaxColWidth = 50
    +   table.MaxColWidth = 39
    
        fmt.Println("==> List")
    -   table.AddRow("NAME", "BIRTHDAY", "BIO")
    +   table.AddRow("名前", "誕生日", "BIO")
        for _, hacker := range hackers {
            table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio)
        }
    @@ -28,11 +28,11 @@ func main() {
    
        fmt.Print("\n==> Details\n")
        table = uitable.New()
    -   table.MaxColWidth = 80
    +   table.MaxColWidth = 70
        table.Wrap = true
        for _, hacker := range hackers {
    -       table.AddRow("Name:", hacker.Name)
    -       table.AddRow("Birthday:", hacker.Birthday)
    +       table.AddRow("名前:", hacker.Name)
    +       table.AddRow("誕生日:", hacker.Birthday)
            table.AddRow("Bio:", hacker.Bio)
            table.AddRow("") // blank
        }
    
    opened by mattn 4
  • Update table with response.

    Update table with response.

    How can I update my table when fetching some information from any web source. For example:

    1    -   -
    2    -   -
    

    Should turn into

    1  bar   foo
    2  Foo   bar
    

    Is this possible with the current lib?

    question 
    opened by KeizerDev 2
  • Wide characters are not supported.

    Wide characters are not supported.

    I have patch but it make changes for strutil. If you allow, I'll send p-r.

    diff --git a/table.go b/table.go
    index 705950d..c4df2e3 100644
    --- a/table.go
    +++ b/table.go
    @@ -7,6 +7,7 @@ import (
    
        "github.com/gosuri/uitable/util/strutil"
        "github.com/gosuri/uitable/util/wordwrap"
    +   "github.com/mattn/go-runewidth"
     )
    
     var (
    @@ -153,8 +154,9 @@ type Cell struct {
     func (c *Cell) LineWidth() uint {
        width := 0
        for _, s := range strings.Split(c.String(), "\n") {
    -       if len(s) > width {
    -           width = len(s)
    +       w := runewidth.StringWidth(s)
    +       if w > width {
    +           width = w
            }
        }
        return uint(width)
    diff --git a/util/strutil/strutil.go b/util/strutil/strutil.go
    index 25c4424..275a49d 100644
    --- a/util/strutil/strutil.go
    +++ b/util/strutil/strutil.go
    @@ -3,15 +3,17 @@ package strutil
    
     import (
        "bytes"
    +   "github.com/mattn/go-runewidth"
     )
    
     // Returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.
     func PadRight(str string, length int, pad byte) string {
    -   if len(str) >= length {
    +   slen := runewidth.StringWidth(str)
    +   if slen >= length {
            return str
        }
        buf := bytes.NewBufferString(str)
    -   for i := 0; i < length-len(str); i++ {
    +   for i := 0; i < length-slen; i++ {
            buf.WriteByte(pad)
        }
        return buf.String()
    @@ -19,11 +21,12 @@ func PadRight(str string, length int, pad byte) string {
    
     // Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character.
     func PadLeft(str string, length int, pad byte) string {
    -   if len(str) >= length {
    +   slen := runewidth.StringWidth(str)
    +   if slen >= length {
            return str
        }
        var buf bytes.Buffer
    -   for i := 0; i < length-len(str); i++ {
    +   for i := 0; i < length-slen; i++ {
            buf.WriteByte(pad)
        }
        buf.WriteString(str)
    @@ -33,13 +36,14 @@ func PadLeft(str string, length int, pad byte) string {
     // Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds
     // the desired length or pads spaces to the right of the string when length is smaller than desired
     func Resize(s string, length uint) string {
    +   slen := runewidth.StringWidth(s)
        n := int(length)
    -   if len(s) == n {
    +   if slen == n {
            return s
        }
        // Pads only when length of the string smaller than len needed
        s = PadRight(s, n, ' ')
    -   if len(s) > n {
    +   if slen > n {
            b := []byte(s)
            var buf bytes.Buffer
            for i := 0; i < n-3; i++ {
    
    opened by mattn 1
  • uitable: add Table.Lines() to allow accessing the table content as []string

    uitable: add Table.Lines() to allow accessing the table content as []string

    This PR adds a new function Lines() to Table, making it possible to access the table's string output as a collection (slice) of strings. This is useful if you'd like to use the rows in a different way than have everything as one big consolidated string.

    Table.String() simply uses the output of Lines().

    opened by herbygillot 0
  • Error color wrap disaply

    Error color wrap disaply

    Add some extra chars in the multi-color example, the display of wrap fails: image

    That's because we use runewidth.StringWidth to calculate the length of the cell, but use len to judge whether to wrap.

    opened by FogDong 0
  • Adding ppc64le architecture support on travis-ci

    Adding ppc64le architecture support on travis-ci

    Hi, I had added ppc64le(Linux on Power) architecture support on travis-ci in the PR and looks like its been successfully added. I believe it is ready for the final review and merge. The travis ci build logs can be verified from the link below. https://travis-ci.com/github/kishorkunal-raj/uitable/builds/191402290

    Reason behind running tests on ppc64le: This package is included in the ppc64le versions of RHEL and Ubuntu - this allows the top of tree to be tested continuously as it is for Intel, making it easier to catch any possible regressions on ppc64le before the distros begin their clones and builds. This reduces the work in integrating this package into future versions of RHEL/Ubuntu.

    Please have a look.

    Regards, Kishor Kunal Raj

    opened by kishorkunal-raj 1
  • Weird formatting on Bash on Ubuntu running in Windows using WSL when VirtualTerminalLevel is 1

    Weird formatting on Bash on Ubuntu running in Windows using WSL when VirtualTerminalLevel is 1

    This is quite a weird one that I ran into a while ago when using Helm. The original ticket can be found here;

    https://github.com/helm/helm/issues/8242

    When using Bash on Ubuntu in Windows using WSL, apparently the terminal is still rendered using underlying Windows components. If the registry setting HKCU\Console\VirtualTerminalLevel is set to 1, the spacing of the tables get messed up.

    HKCU\Console\VirtualTerminalLevel is normally not set, or set to 0. It needs to be set to 1 in order to enable ANSI color sequences in Windows CLI, which is why I had it set. Changing the setting back to 0 fixed the spacing issue.

    I think this means that the tabling sends certain control characters that mess up the spacing somehow.

    Image; tty

    Note that kubectl output is just fine, even with this setting set. I understood from the Helm team (see original ticket) that they use some custom formatter.

    I realize this is a very obscure issue, but it should be easy enough to reproduce on Windows 10 with WSL and Ubuntu. Just set the registry key, open a new bash window and try some tabled output.

    opened by gboor 0
  • Cell formatter

    Cell formatter

    Make it possible to attach a formatter function to each table cell. This is primarily useful to colorize terminal output, but could perhaps be used in other ways too.

    The formatter function is selected by the function inTable.CellFormatter. Given a cells x and y coordinates, it should return a formatter function.

    opened by svenax 1
Owner
Greg Osuri
Founder & CEO, Akash Network / Overclock Labs
Greg Osuri
Tabular simplifies printing ASCII tables from command line utilities

tabular Tabular simplifies printing ASCII tables from command line utilities without the need to pass large sets of data to it's API. Simply define th

InVision 68 Oct 28, 2022
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

asciigraph Go package to make lightweight ASCII line graphs ╭┈╯. Installation go get github.com/guptarohit/asciigraph Usage Basic graph package main

Rohit Gupta 2.1k Jan 1, 2023
Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method

ctc - Console Text Colors The non-invasive cross-platform terminal color library does not need to modify the Print method Virtual unix-like environmen

null 41 Nov 9, 2022
uilive is a go library for updating terminal output in realtime

uilive uilive is a go library for updating terminal output in realtime. It provides a buffered io.Writer that is flushed at a timed interval. uilive p

Greg Osuri 1.5k Dec 28, 2022
A go library to render progress bars in terminal applications

uiprogress A Go library to render progress bars in terminal applications. It provides a set of flexible features with a customizable API. Progress bar

Greg Osuri 2k Dec 29, 2022
Intuitive package for prettifying terminal/console output. http://godoc.org/github.com/ttacon/chalk

chalk Chalk is a go package for styling console/terminal output. Check out godoc for some example usage: http://godoc.org/github.com/ttacon/chalk The

Trey Tacon 418 Dec 23, 2022
An ANSI colour terminal package for Go

colourize An ANSI colour terminal package for Go. Supports all ANSI colours and emphasis. Not compatible with Windows systems. Installation go get gi

Trey Bastian 26 Sep 26, 2022
Simple tables in terminal with Go

Simple tables in terminal with Go This package allows to generate and display ascii tables in the terminal, f.e.: +----+------------------+-----------

Alexey Popov 408 Dec 29, 2022
Terminal based dashboard.

Termdash is a cross-platform customizable terminal based dashboard. The feature set is inspired by the gizak/termui project, which in turn was inspire

Jakub Sobon 2.2k Dec 28, 2022
Golang terminal dashboard

termui termui is a cross-platform and fully-customizable terminal dashboard and widget library built on top of termbox-go. It is inspired by blessed-c

Zack Guo 12.3k Dec 27, 2022
Yet Another CLi Spinner; providing over 70 easy to use and customizable terminal spinners for multiple OSes

Yet Another CLi Spinner (for Go) Package yacspin provides yet another CLi spinner for Go, taking inspiration (and some utility code) from the https://

Tim Heckman 387 Dec 25, 2022
Terminal string styling for go done right, with full and painless Windows 10 support.

GChalk GChalk is a library heavily inspired by chalk, the popular Node.js terminal color library, and using go ports of supports-color and ansi-styles

Jason Walton 318 Dec 28, 2022
A CLI to adjust display brightness using xrandr. Made because Ubuntu desktop was baking my eyeballs in low light.

bright This is a simple program to set monitor brightness using xrandr. I made this because Ubuntu does not come with a built-in way to change monitor

Micah Parks 0 Jan 15, 2022
Small library for simple and convenient formatted stylized output to the console.

cfmt cfmt is a small library for simple and convenient formatted stylized output to the console, providing an interface that is exactly the same as th

Makhnev Petr 54 Jan 7, 2023
A tiny library for super simple Golang tables

Tabby A tiny library for super simple Golang tables Get Tabby go get github.com/cheynewallace/tabby Import Tabby import "github.com/cheynewallace/tabb

Cheyne Wallace 325 Nov 23, 2022
It is a proxy to improve article readability, a directory for your favorite articles, and a way to make the internet lighter and more accessible.

timoneiro It is a work in progress. Some features are unimplemented yet. The helmsman's goal is to be a way to browse articles without all the distrac

Cesar Gimenes 6 Jun 13, 2022
tabitha is a no-frills tabular formatter for the terminal.

tabitha tabitha is a no-frills tabular formatter for the terminal. Features Supports padding output to the longest display text, honoring ANSI colors

Jim Schubert 1 Aug 19, 2022
Gotabulate - Easily pretty-print your tabular data with Go

Gotabulate - Easily pretty-print tabular data Summary Go-Tabulate - Generic Go Library for easy pretty-printing of tabular data. Installation go get g

Vadim Kravcenko 294 Dec 27, 2022
A Go native tabular data extraction package. Currently supports .xls, .xlsx, .csv, .tsv formats.

grate A Go native tabular data extraction package. Currently supports .xls, .xlsx, .csv, .tsv formats. Why? Grate focuses on speed and stability first

Jeremy Jay 110 Dec 26, 2022