guesswidth package - github.com/noborus/guesswidth - Go Packages

Package guesswidth handles the format as formatted by printf. Spaces exist as delimiters, but spaces are not always delimiters. The width seems to be a fixed length, but it doesn't always fit. guesswidth finds the column separation position from the reference line(header) and multiple lines(body).

This section is empty.

Positions returns separator positions from multiple lines and header line number. Lines before the header line are ignored.

ToTable parses a slice of lines and returns a table.

package main

import (
	"fmt"

	"github.com/noborus/guesswidth"
)

func main() {
	lines := []string{
		"    PID TTY          TIME CMD",
		"1595989 pts/6    00:00:01 zsh",
		"1690373 pts/6    00:00:00 ps",
	}
	table := guesswidth.ToTable(lines, 1, true)
	fmt.Println(table)
}
Output:
[[PID TTY TIME CMD] [1595989 pts/6 00:00:01 zsh] [1690373 pts/6 00:00:00 ps]]

ToTableN parses a slice of lines and returns a table, but limits the number of splits.

package main

import (
	"fmt"
	"strings"

	"github.com/noborus/guesswidth"
)

func main() {
	lines := []string{
		"2022-12-21T09:50:16+0000 WARN A warning that should be ignored is usually at this level and should be actionable.",
		"2022-12-21T09:50:17+0000 INFO This is less important than debug log and is often used to provide context in the current task.",
		"2022-12-10T05:33:53+0000 DEBUG This is a debug log that shows a log that can be ignored.",
		"2022-12-10T05:33:53+0000 INFO This is less important than debug log and is often used to provide context in the current task.",
	}
	table := guesswidth.ToTableN(lines, 1, 2, true)
	for _, columns := range table {
		fmt.Println(strings.Join(columns, ","))
	}
}
Output:
2022-12-21T09:50:16+0000,WARN,A warning that should be ignored is usually at this level and should be actionable.
2022-12-21T09:50:17+0000,INFO,This is less important than debug log and is often used to provide context in the current task.
2022-12-10T05:33:53+0000,DEBUG,This is a debug log that shows a log that can be ignored.
2022-12-10T05:33:53+0000,INFO,This is less important than debug log and is often used to provide context in the current task.
type Cols struct {
	Width     int
	Justified int
	
}
type GuessWidth struct {

	
	Widths []Cols

	
	ScanNum int
	Header int
	
	LimitSplit int
	
	
	MinLines int
	
	TrimSpace bool
	
}

GuessWidth reads records from printf-like output.

NewReader returns a new Reader that reads from r.

Read reads one row and returns a slice of columns. Scan is executed first if it is not preRead.

ReadAll reads all rows and returns a two-dimensional slice of rows and columns.

func (g *GuessWidth) Scan(num int)

Scan preReads and parses the lines.

func (g *GuessWidth) SetJustified(threshold int) []Cols

SetJustified sets the justification of the column.

func (g *GuessWidth) UpdateMaxWidth(columns []string) []Cols

UpdateMaxWidth updates the maximum width of the column.