New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

github.com/prvn/table_writer

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/prvn/table_writer

  • v0.0.0-20160501024437-0e40d9efecd8
  • Source
  • Go
  • Socket score

Version published
Created
Source

Table Writer

Introduction

This is a simple utility program to print data in a pretty table format to console. This package provides features like

  • Simplistic API with full flexibility to mix and match different styles of printing a row
  • Can print all data at once as table OR print individual row when needed
  • Can position text inside the column to center, right or left. This can be changed on per row basis

Installation

go get github.com/prvn/table_writer

Examples

Initialization

// Header for table
columnHeaders := []string{"Id", "Count", "Timestamp"}

// Id will not be bigger than 4 digits, for better readability, I give it 6 character width
// Count will be up to 6 digits, 9 sounds reasonable readability wise
// Timestamp is epoch, so never beyond 10 digits, 12 looks reasonable space
columnMaxWidths := []int{6, 9, 12}

tableWriter, err := table_writer.NewTableWriter(headers, widths)

Printing Table (All at once)

row1 := []string{"1", "11", "1461910429"}
row2 := []string{"2", "12", "1461910429"}
row3 := []string{"1000", "99999", "1461910429"}
tableWriter.PrintTable([][]string{row1, row2, row3}, table_writer.AlignCenter)

will print table like

+------+---------+------------+
|  Id  |  Count  | Timestamp  |
+------+---------+------------+
|  1   |   11    | 1461910429 |
|  2   |   12    | 1461910429 |
| 1000 | 999999  | 1461910429 |
+------+---------+------------+

Printing Table (Stream data)

Initialize table writer as mentioned in Initialization

// Print header
tableWriter.PrintHeader()
row1 := []string{"1", "11", "1461910429"}
row2 := []string{"2", "12", "1461910429"}
row3 := []string{"1000", "99999", "1461910429"}
rows := [][]string{row1, row2, row3}

// Add rows one by one
for _, r := range rows {
	tableWriter.PrintRow(r, table_writer.AlignCenter)
}

// OR Add all rows at once
tableWriter.PrintRows(rows, table_writer.AlignCenter)

// Print footer
tableWriter.PrintFooter()

Will print table like

+------+---------+------------+
|  Id  |  Count  | Timestamp  |
+------+---------+------------+
|  1   |   11    | 1461910429 |
|  2   |   12    | 1461910429 |
| 1000 | 999999  | 1461910429 |
+------+---------+------------+

Print Table & Summarize

Follow steps from above and add following code

summaryRow1 := "Id's: 3"
summaryRow2 := "Count: 1000022"
tableWriter.PrintRowAsOneColumn(summaryRow1, table_writer.AlignRight)
tableWriter.PrintRowAsOneColumn(summaryRow2, table_writer.AlignRight)
tableWriter.PrintFooter()

Will print table like

+------+---------+------------+
|  Id  |  Count  | Timestamp  |
+------+---------+------------+
|  1   |   11    | 1461910429 |
|  2   |   12    | 1461910429 |
| 1000 | 999999  | 1461910429 |
+------+---------+------------+
|Id's: 3                      |
|Count: 1000022               |
+------+---------+------------+

FAQs

Package last updated on 01 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc