pr

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2020 License: MIT Imports: 8 Imported by: 1

README

pr

Package pr provides various printing routines analogous to the pr *NIX utility.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FprintFlex

func FprintFlex(w io.Writer, hspace int, list []string)

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

func FprintGrid(w io.Writer, hspace, columns int, list []string)

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

func GetTerminalWidth(fd int) int

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 PrintFlex

func PrintFlex(list []string)

func PrintGrid

func PrintGrid(columns int, list []string)

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.

Directories

Path Synopsis
This is the example program from the documentation.
This is the example program from the documentation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL