csvformat

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: MIT, MIT Imports: 10 Imported by: 0

README

csvformat

One thing that takes up a ton of time is getting the output to look right. With csvformat you can take any text string that is formatted in CSV and create a nice table format. Example:

func main() {
	csvstuff := getCsv() // This could be any function that returns a text string with CSV.
	csvout := csvformat.NewGrid()
	fmt.Println(csvout.Gridout(string(csvstuff)))
}

See minimal.go in the ex folder. Function getCsv() just does a simple http.Client to https://cdn.wsform.com/wp-content/uploads/2021/04/weekday.csv The main point is that you just need to format it in csv, and you don't have to do any additional formatting.

Output:

+--------------+-----------------+------------+--------------+
|     Name     |   Abbreviation  |   Numeric  |   Numeric-2  |
+==============+=================+============+==============+
|    Monday    |       Mon.      |      1     |      01      |
|    Tuesday   |       Tue.      |      2     |      02      |
|   Wednesday  |       Wed.      |      3     |      03      |
|   Thursday   |       Thu.      |      4     |      04      |
|    Friday    |       Fri.      |      5     |      05      |
|   Saturday   |       Sat.      |      6     |      06      |
|    Sunday    |       Sun.      |      7     |      07      |
+--------------+-----------------+------------+--------------+

Better Example:

func main() {
	csvtext := fmt.Sprintf("One,Two,Three\nFour,Five,Six\nSeven,Eight,Nine")
	csvgrid := csvformat.NewGrid()
	csvgrid.Headline = "First,Second,Third" // This is optional. If the CSV already contains a heading line. don't set this.
	csvgrid.Render = rendr                  // There are several different formats.
	out, err := csvgrid.Gridout(csvtext)
	if err != nil {
		fmt.Println("error:", err)
		os.Exit(1)
	}
	fmt.Println(out)
}

Output:

+----------+-----------+----------+
|   First  |   Second  |   Third  |
+==========+===========+==========+
|    One   |    Two    |   Three  |
|   Four   |    Five   |    Six   |
|   Seven  |   Eight   |   Nine   |
+----------+-----------+----------+

Output formats:

  • simple
  • plain
  • tab
  • text
  • html Todo: format as a bootstrap table.
  • mysql

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Jsonout

func Jsonout(text string, pp bool) (string, error)

func TimeStamp

func TimeStamp(a ...any) string

TimeStamp Returns the current time as timestamp: %Y%m%d%H%M%s optionally you can give it a time format in accordance with Go's time package.

Types

type Excel

type Excel struct {
	Header             string
	FileName           string
	Space              bool
	Delimeter          string
	Sheet              string
	FormatHeaderFooter excelize.FormatHeaderFooter
	HeadStyle          excelize.Style
	NormStyle          excelize.Style
}

func NewExcel

func NewExcel() *Excel

NewExcel Returns type Excel with some defaults.

Align: left
NoHeader: False
Wrap: false
Indent: "" (empty)
LineBetweenRow: false
Columns: All (empty)

New Excel gives defaults. You can change these after NewExcel or just set it using the struct as shown in NewExcel. your own use might look like this myexcek := nsres.Excel{Delimiter: "|"

func (*Excel) Excelout

func (excel *Excel) Excelout(text string, fname string) (string, error)

type Grid

type Grid struct {
	Render           string
	Align            string
	NoHeader         bool
	Wrap             bool
	Indent           string
	NoLineBetweenRow bool
	Space            bool
	Columns          string
	Delimiter        string
	Number           bool
	OutDelimiter     string
	Headline         string
}

Grid Render Used for Gridout function. Renders output text in a grid, text, tab.

	Render:
		simple Simple tab delimited with underlined header.
		plain pretty much just strips the "," for a space
		tab   Tab spaced output
		html  Output in simple HTML. Header has grey background with thin lines between cells
		mysql Looks similar to mysql shell output
		grid  uses ANSI Graphics. Not compatible with all terminals
		gridt MySQL
	Align: Right, left or center
	NoHeader: don't print a header
	Wrap: wrap cell output
	NoLineBetweenRow: put a blank line inbetween each row
	Columns: List the columns you want output.
	Space: Do not trim spaces from column. "1,   2" will output "1|    2" instead of "1|2"
	Delimeter: Delimeter between text. Default is a ","
 Number -- automatically add number the output

func NewGrid

func NewGrid() *Grid

NewGrid Returns a Grid with some defaults.

Render: mysql
Align: left
NoHeader: False
Wrap: false
Indent: "" (empty)
LineBetweenRow: false
Columns: All (empty)

New grid gives defaults. You can change these after NewGrid or just set it using the struct as shown in NewGrid. your own use might look like this mygrid := nsres.Grid{Render: "grid", Align: "Right", NoHeader: false, Wrap: false, Indent: "\t", LineBetweenRow: false, Space: false, Columns: "", Delimiter: "|"

func (*Grid) Gridout

func (g *Grid) Gridout(text string) (string, error)

Gridout Don't waste time getting the output looking nice, just make it csv, then pass it to gridoutl. Gridout Prints according to Grid struct. Render: simple. Tab format with lines on top plain. Text output no gridlines extra spaces in between

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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