Documentation
¶
Overview ¶
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).
Index ¶
Examples ¶
Constants ¶
const ( Left = iota Right )
Variables ¶
This section is empty.
Functions ¶
func Positions ¶ added in v0.3.2
Positions returns separator positions from multiple lines and header line number. Lines before the header line are ignored.
func ToTable ¶
ToTable parses a slice of lines and returns a table.
Example ¶
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]]
func ToTableN ¶
ToTableN parses a slice of lines and returns a table, but limits the number of splits.
Example ¶
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.
Types ¶
type GuessWidth ¶
type GuessWidth struct { // Widths is the width of the column. Widths []Cols // ScanNum is the number to scan to analyze. ScanNum int // Header is the base line number. It starts from 0. Header int // limitSplit is the maximum number of columns to split. LimitSplit int // MinLines is the minimum number of lines to recognize as a separator. // 1 if only the header, 2 or more if there is a blank in the body. MinLines int // TrimSpace is whether to trim the space in the value. TrimSpace bool // contains filtered or unexported fields }
GuessWidth reads records from printf-like output.
func NewReader ¶
func NewReader(r io.Reader) *GuessWidth
NewReader returns a new Reader that reads from r.
func (*GuessWidth) Read ¶
func (g *GuessWidth) Read() ([]string, error)
Read reads one row and returns a slice of columns. Scan is executed first if it is not preRead.
func (*GuessWidth) ReadAll ¶
func (g *GuessWidth) ReadAll() [][]string
ReadAll reads all rows and returns a two-dimensional slice of rows and columns.
func (*GuessWidth) SetJustified ¶ added in v0.4.0
func (g *GuessWidth) SetJustified(threshold int) []Cols
SetJustified sets the justification of the column.
func (*GuessWidth) UpdateMaxWidth ¶ added in v0.4.0
func (g *GuessWidth) UpdateMaxWidth(columns []string) []Cols
UpdateMaxWidth updates the maximum width of the column.