Documentation ¶
Overview ¶
Package pr implements functions for pretty printing of information.
The following example program demonstrates imitating the columns outputted by the "ls" program. Run it like so: "ls -1 | ./test".
package main import ( "bufio" "flag" "fmt" "io" "os" "strings" "github.com/goulash/pr" ) func main() { reader := bufio.NewReader(os.Stdin) buffer := make([]string, 0, 32) for { line, err := reader.ReadString('\n') if err != nil { if err != io.EOF { fmt.Errorf("error: %s\n", err) } break } buffer = append(buffer, strings.TrimSpace(line)) } width := flag.Int("width", -1, "width of the terminal") flag.Parse() if *width < 0 { pr.PrintFlex(buffer) } else { pr.FprintFlex(os.Stdout, *width, buffer) } }
Index ¶
- func FprintFlex(w io.Writer, hspace int, list []string)
- func FprintGrid(w io.Writer, hspace, columns int, list []string)
- func GetTerminalWidth(fd int) int
- func PrintFlex(list []string)
- func PrintGrid(columns int, list []string)
- func SetColumnPadding(padding int)
- func StderrTerminalWidth() int
- func StdoutTerminalWidth() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FprintFlex ¶
FprintFlex prints the items in the given list in as many columns as makes sense, given the horizontal space available.
It will not print items in more columns than necessary: the minimum number of columns is used to attain the minimum row count.
func FprintGrid ¶
FprintGrid tries to print the items in the given list in the given number of columns.
If it is not possible to fit the items in that number of columns, then the items are printed in as many columns as possible.
func GetTerminalWidth ¶
GetTerminalWidth returns the current width of the connected terminal for the given file descriptor. If fd is not connected to a terminal, then -1 is returned.
Note: this only works on Linux.
func SetColumnPadding ¶
func SetColumnPadding(padding int)
SetColumnPadding sets the number of spaces that are between two columns. The default value is two, which comes from the GNU utility "ls".
func StderrTerminalWidth ¶
func StderrTerminalWidth() int
StderrTerminalWidth returns the current width of the terminal (if any) connected to Stderr.
Note: this only works on Linux.
func StdoutTerminalWidth ¶
func StdoutTerminalWidth() int
StdoutTerminalWidth returns the current width of the terminal (if any) connected to Stdout.
Note: this only works on Linux.
Types ¶
This section is empty.