guesswidth

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: MIT Imports: 7 Imported by: 2

README

guesswidth

Go Reference Go

Overview

guesswidth guesses the column position for fixed-width formats(fwf).

The output of the ps command has no delimiters, making the values difficult to machine-readable. guesswidth guesses smarter than just space delimiters.

guesswidth is guessed based on the position of characters in the header. So having a header will give you better results.

Install command

There is also a guesswidth command.

go install github.com/noborus/guesswidth/cmd/guesswidth@latest

Guess the width output with no delimiters in the command.

$ ps
    PID TTY          TIME CMD
 302965 pts/3    00:00:12 zsh
 733211 pts/3    00:00:00 ps
 733212 pts/3    00:00:00 tee
 733213 pts/3    00:00:00 guesswidth

Split the output like this:

$ ps | guesswidth
    PID| TTY     |     TIME|CMD
 302965| pts/3   | 00:00:08|zsh
 539529| pts/3   | 00:00:00|ps
 539530| pts/3   | 00:00:00|guesswidth

It can be converted to csv.

$ ps | guesswidth csv
PID,TTY,TIME,CMD
302965,pts/3,00:00:12,zsh
733211,pts/3,00:00:00,ps
733212,pts/3,00:00:00,tee
733213,pts/3,00:00:00,guesswidth

Examples

guesswidth inserts a delimiter (| by default) (Colors are changed here for clarity).

Even if there are spaces in the header or body, they will be separated correctly.

ps

ps

docker ps

docker-ps

docker node

docker node ls

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

This section is empty.

Variables

This section is empty.

Functions

func Positions added in v0.3.2

func Positions(lines []string, header int, minLines int) []int

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

func ToTable

func ToTable(lines []string, header int, trimSpace bool) [][]string

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

func ToTableN(lines []string, header int, numSplit int, trimSpace bool) [][]string

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.

func Version added in v0.2.0

func Version() string

Types

type GuessWidth

type GuessWidth struct {

	// 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) Scan

func (g *GuessWidth) Scan(num int)

Scan preReads and parses the lines.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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