align

package module
Version: v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2021 License: MIT Imports: 6 Imported by: 1

README

align

A general purpose application that aligns text

GoDoc Build Status Go Report Card Coverage Status

The focus of this application is to provide a fast, efficient, and useful tool for aligning text. Its creation is the result of inspiration from several other amazing alignment tools, namely column or the Sublime Text plugin AlignTab.

See the Wiki for usage examples!

Included
  • A simple yet useful CLI with options to specify your delimiter, input and output files, etc.
  • Align by any string as your delimiter or separator, not just a single character.
  • If your separator string is contained within the data itself, it can be escaped by specifying a text qualifier.
  • Right, Center, or Left justification of each field.

Why?

Sometimes, it's just easier to align a CSV (or delimited file) by its delimiter and view the columns in your plain text editor (which saves you from opening Excel!).

Another use is to align blocks of code by = or =>, etc.

Install
$ go get github.com/Guitarbum722/align
$ make install

$ # build all binaries
$ make release
Usage - CLI examples
Usage: align [-h] [-f] [-o] [-q] [-s] [-d] [-a] [-c] [-i] [-p]
Options:
  -h | --help  help
  -f           input file.  If not specified, pipe input to stdin
  -o           output file. (default: stdout)
  -q           text qualifier (if applicable)
  -s           delimiter (default: ',')
  -d           output delimiter (defaults to the value of sep)
  -a           <left>, <right>, <center> justification (default: left)
  -c           output specific fields (default: all fields)
  -i           override justification by column number (e.g. 2:center,5:right)
  -p           extra padding surrounding delimiter

Specify your input file, output file, delimiter. You can also pipe input to stdin (if the -f option is provided, it will take precedence over Stdin) If no -o option is provided, stdout will be used.

$ align -f input_file.csv -o output_file.csv

$ align -f input_file.csv -o 

$ cat awesome.csv | align

Do you have rows with a different number of fields? This might be more common with code, but align doesn't care!

$ echo "field1|field2\nValue1|Value2\nCoolValue1|CoolValue2|CoolValue3" | align -s \|
field1     | field2
Value1     | Value2
CoolValue1 | CoolValue2 | CoolValue3

Column filtering (specifiy output fields and optionally override the justification of the output fields). This might be useful if you would like to display a dollar amount or number field differently. The specified fields are indexed at 1.

# output fields 1,3,5 justified 'right'
$ cat file.csv | align -a right -c 1,3,5

# output fields 1,2,3,7,8 with default justification (left) except for field 7, which is right justified
$ cat file.csv | align -c 1,2,3,7,8 -i 1:right,7:center

#output all fields by default, with right justification, with overridden justification on certain columns
$ cat file.csv | align -a right -i 1:center,5:left

Support for worldwide characters.

first          , last              , middle  , email
paul           , danny             ,  かど    , や製油

It is perfectly acceptable to even use emojis as your input/output delimiters.

first  😮 last     😮 email
Hector 😮 Gonzalez 😮 h.g@nothing.com

Add additional padding if desired with the -p flag. Default is 1 space, and 0 will output with no additional padding. If the value supplied is less than 0, then the behavior will be as if it were set to 0 and no padding will be applied.

# padding of 4 spaces surrounding the delimiter.
align -p 4
Contributions

If you have suggestions or discover a bug, please open an issue. If you think you can make the fix, please use the Fork / Pull Request on your feature branch approach.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Align

type Align struct {
	// contains filtered or unexported fields
}

Align scans input and writes output with aligned text.

func NewAlign

func NewAlign(in io.Reader, out io.Writer, sep string, qu TextQualifier) *Align

NewAlign creates and initializes a ScanWriter with in and out as its initial Reader and Writer and sets del to the desired delimiter to be used for alignment. It is meant to read the contents of its io.Reader to determine the length of each field and output the results in an aligned format. Left Justification is used by default. See UpdatePadding to set the Justification.

func (*Align) Align

func (a *Align) Align()

Align determines the length of each field of text around the configured delimiter and aligns all of the text by the delimiter.

func (*Align) FilterColumns

func (a *Align) FilterColumns(c []int)

FilterColumns sets which column numbers should be output.

func (*Align) OutputSep

func (a *Align) OutputSep(outsep string)

OutputSep sets the output separator string with outsep if a different value from the input sep is desired.

func (*Align) UpdatePadder added in v1.1.0

func (a *Align) UpdatePadder(padder PadGrower)

UpdatePadder sets the Align's padder implementation if a different one is desired from the default.

func (*Align) UpdatePadding

func (a *Align) UpdatePadding(p PaddingOpts)

UpdatePadding uses PaddingOpts p to update the Align's padding options.

type Grower added in v1.1.0

type Grower interface {
	Grow(n int)
	Reset()
}

Grower grows by the given number of bytes n. Reset will set the Grower to 0.

type Justification

type Justification byte

Justification is used to set the alignment of the column contents itself along the right, left, or center.

const (
	JustifyRight Justification = iota + 1
	JustifyCenter
	JustifyLeft
)

Left, Right or Center Justification options.

type PadGrower added in v1.1.0

type PadGrower interface {
	Grower
	Padder
}

PadGrower makes a string with the ability to grow the underlying buffer.

type Padder added in v1.1.0

type Padder interface {
	io.Writer
	fmt.Stringer
	WriteByte(c byte) error
	WriteString(s string) (int, error)
	Bytes() []byte
}

Padder builds a string and can return its string value.

type PaddingOpts

type PaddingOpts struct {
	Justification  Justification
	ColumnOverride map[int]Justification //override the Justification of specified columns
	Pad            int                   // padding surrounding the separator
}

PaddingOpts provides configurability for left/center/right Justification and padding length.

type TextQualifier

type TextQualifier struct {
	On        bool
	Qualifier string
}

TextQualifier is used to configure the scanner to account for a text qualifier.

Source Files

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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