termtable

package module
v0.0.0-...-2b6a844 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 4 Imported by: 0

README

termtable

A simple golang terminal table drawing library which allows you to populate the table row by row.

This is how your terminal tables actually could look ;)

   Path                            | Country    | Device        | Max First Input Delay    | Server response time    | Time to interactive    | Cumulative Layout Shift                     | Largest Contentful Paint                   
=========================================================================================================================================================================================================================================
   /                               | us         | mobile        | 4003ms                   | 16ms                    | 0.000000               | 379ms                                       | 1245ms                                     
                                   |            |               |                          |                         |                        |                                             | div.hero-image                             
                                   |            |               |                          |                         |                        |                                             |  >div.tv-container                         
                                   |            |               |                          |                         |                        |                                             |   >picture                                 
                                   |            |               |                          |                         |                        |                                             |    >img                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   /de                             | us         | mobile        | 2184ms                   | 16ms                    | 0.000000               | 563ms                                       | 1325ms                                     
                                   |            |               |                          |                         |                        |                                             | a.title-list-grid__item--link              
                                   |            |               |                          |                         |                        |                                             |  >div.title-poster                         
                                   |            |               |                          |                         |                        |                                             |   >picture.picture-comp                    
                                   |            |               |                          |                         |                        |                                             |    >img.picture-comp__img                  
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   /                               | us         | desktop       | 3795ms                   | 16ms                    | 0.000000               | 118ms                                       | 1327ms                                     
                                   |            |               |                          |                         |                        |                                             | div.hero-image                             
                                   |            |               |                          |                         |                        |                                             |  >div.tv-container                         
                                   |            |               |                          |                         |                        |                                             |   >picture                                 
                                   |            |               |                          |                         |                        |                                             |    >img                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   /de                             | us         | desktop       | 2704ms                   | 16ms                    | 0.000000               | 124ms                                       | 1328ms                                     
                                   |            |               |                          |                         |                        |                                             | a.title-list-grid__item--link              
                                   |            |               |                          |                         |                        |                                             |  >div.title-poster                         
                                   |            |               |                          |                         |                        |                                             |   >picture.picture-comp                    
                                   |            |               |                          |                         |                        |                                             |    >img.picture-comp__img                  
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Motivation

All the other table writer libraries I found always required me to have all the data at the moment of drawing. As I use JSONL in on of my APIs the data comes one record after another, which made the other libraries not a fit.

Simple example

  Path       | Country| Device    
==================================
  /          | us     | mobile    
----------------------------------
  /          | us     | desktop   
----------------------------------
  /de        | de     | mobile    
----------------------------------

You can print this beautiful table with the below code example. (Errors dropped for brevity. You should check them!)

package main

import (
	"github.com/simonfrey/termtable"
	"os"
)

func main() {
	// Creates a new termtable instance with " | " as spacing between columns
	tt := termtable.New(os.Stdout," | ")

	tt.WriteHeader([]termtable.HeaderField{
		{
			Field: termtable.NewStringField("Path"),
		},
		{
			Field: termtable.NewStringField("Country"),
			Width: termtable.IntPointer(4),
		},
		{
			Field: termtable.NewStringField("Device"),
			Width: termtable.IntPointer(10),
		},
	})

	tt.WriteRowDivider('=')
	tt.WriteRow([]termtable.Field{
		termtable.NewStringField("/"),
		termtable.NewStringField("us"),
		termtable.NewStringField("mobile"),
	})
	tt.WriteRowDivider('-')
	tt.WriteRow([]termtable.Field{
		termtable.NewStringField("/"),
		termtable.NewStringField("us"),
		termtable.NewStringField("desktop"),
	})
	tt.WriteRowDivider('-')
	tt.WriteRow([]termtable.Field{
		termtable.NewStringField("/de"),
		termtable.NewStringField("de"),
		termtable.NewStringField("mobile"),
	})
	tt.WriteRowDivider('-')
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntPointer

func IntPointer(i int) *int

func WhiteSpace

func WhiteSpace(x int) string

Types

type ColorField

type ColorField struct {
	// contains filtered or unexported fields
}

ColorField is a Field holding a string which will be colored on cli

func NewColorField

func NewColorField(s string, c *color.Color) ColorField

func (ColorField) Len

func (cc ColorField) Len() int

func (ColorField) String

func (cc ColorField) String() string

type Field

type Field interface {
	String() string
	Len() int
}

Field is are regular Field in a row

type HeaderField

type HeaderField struct {
	Field Field
	Width *int
}

HeaderField hold a regular field and optionally allows to configure the width of the Field

type StringField

type StringField struct {
	// contains filtered or unexported fields
}

StringField holds a string

func NewEmptyField

func NewEmptyField() StringField

NewEmptyField returns a StringField holding an empty string ""

func NewStringField

func NewStringField(s string) StringField

func (StringField) Len

func (sc StringField) Len() int

func (StringField) String

func (sc StringField) String() string

type TermTable

type TermTable struct {
	// contains filtered or unexported fields
}

func New

func New(w io.Writer, columnDivider string) *TermTable

func (*TermTable) WriteHeader

func (tt *TermTable) WriteHeader(row []HeaderField) error

func (*TermTable) WriteRow

func (tt *TermTable) WriteRow(row []Field) error

func (*TermTable) WriteRowDivider

func (tt *TermTable) WriteRowDivider(divider rune) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL