Giter Site home page Giter Site logo

align's Introduction

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 😮 [email protected]

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.

align's People

Contributors

briandowns avatar guitarbum722 avatar mattn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

align's Issues

Delimiter being added when it's not necessary

There are scenarios where there is a line that does NOT contain the delimiter in which to align, yet the delimiter is being added by Export().

An example would be to align a block of code by =

const ( 
    ok = true
    notOk = false
)

...will currently output something like so:

const ( =
ok      = true
notOk   = false
)       =

It needs to determine that a line that does NOT contain the desired delimiter should not have one appended.

CLI functionality

Currently, the CLI is quick and dirty. It should provide more functionality, options, instructions and actual sub-commands.

Unable to Use a Hyphen as First Character in Field Separator

When testing out different separators, I wanted to use an -> however it wasn't happy with. Upon further digging, it appears that the Fatih Flags sees that as a boolean flag and ignores it.

This can either be considered a limitation to be accepted or possibly dealt with. One thing to do would be to avoid the validArg() call by determining if a separator has a hyphen in it's values 0 position. I'm sure there are better ways.

var idx int
for i, j := range args {
    if j == "-s" {
        idx = i
    }
}
var hasHyphen bool
if string(args[idx+1]) == "-" {
    hasHyphen = true
}

If there's a hyphen found, bypass the validArgs call and process as expected. Then again, this is probably a fairly fringe edge case that will likely never arise.

Output columns transpose if a text qualified field starts with a comma

Input:

first,last,email,address,title,phone
,,,,,
,," ,",,,666-666-6666
,,", ",,,666-666-6666
,,,,,555-555-5555
,,,,"Level 3,,, Manager",
grogu,,[email protected],?,"Formerly known as, baby yoda",

Output:

first , last    , email                , address                , title                          , phone             
      ,         ,                      ,                        ,                                
      ,         , " ,"                 ,                        ,                                , 666-666-6666 
      ,         , "                    ,  "                     ,                                ,              , 666-666-6666 
      ,         ,                      ,                        ,                                , 555-555-5555 
      ,         ,                      ,                        , "Level 3,,, Manager"           
grogu ,         , [email protected]      , ?                      , "Formerly known as, baby yoda" 



Specify output delimiter

A neat feature would be to specify the desired output delimiter (if you wanted it to change from the input).

There are similar tools that do this, so it would be a good option for the user if true-up is to be a desired replacement.

Example:

  • input: first,last,middle

  • output: first | last | middle

Text qualified fields containing the delimiter

It is common for the delimiter of the CSV to be contained within some of the fields values. The user of this library should be able to specify a text qualifier, such as ' or " and ignore the delimiter character if it is actually part of the field value.

This avoids the transposition of fields during alignment.

Refactor export() method to reduce allocations

The export() method does quite a few allocations. In one instance, using a single line of comma delimited data (7 columns), there are 17 allocations.

Explore the use of strings.Builder - I think this could be done without affecting the API.

fails to work on tab seperator

align fails to work on tab separator.

i have tried the following example :

ods2tsv vat2 vat2.ods | align -s "\t" -d "&"

[enhancement] Options: space around delimiter

Spacing Options

  • space around
  • leading space
  • trailing space
  • initial leading space
  • final trailing space

To consider the spacing option, let's use the following input:

| Prop | Type | Default | Description |
| :-------: | :-------: | :-------: | ------- |
| tag | String    | `"div"`   | Element tagName _(any valid HTML tag name)_ |
| inline   | Boolean   | `false`   | `display: inline-flex` |

Space Around

Space around would specify the minimum amount of white-space to add before and after a delimiter. In this shortcut option, initial leading space and final trailing space would not be included.

// space around: 1
| Prop      | Type      | Default   | Description |
| :-------: | :-------: | :-------: |   -------   |
| tag       | String    | `"div"`   | Element tagName _(any valid HTML tag name)_ |
| inline    | Boolean   | `false`   | `display: inline-flex` |

Leading Space

Leading space would only specify the minimum amount of space immediately following a delimiter, excepting an initial delimiter not preceded by content.

// leading space: 1
| Prop     | Type     | Default  | Description|
| :-------:| :-------:| :-------:|   -------  |
| tag      | String   | `"div"`  | Element tagName _(any valid HTML tag name)_|
| inline   | Boolean  | `false`  | `display: inline-flex`|

Trailing Space

Trailing space would only specify the minimum amount of space immediately preceding a delimiter, excepting one that is not followed by content.

// trailing space: 1
|Prop      |Type      |Default   |Description |
|:-------: |:-------: |:-------: |  -------   |
|tag       |String    |`"div"`   |Element tagName _(any valid HTML tag name)_ |
|inline    |Boolean   |`false`   |`display: inline-flex` |

Initial Leading Space

Specify an amount of space to place before all content

Final Trailing Space

Specify an amount of space to place after all content

Add Ability to Filter Columns

I'd love to see the ability to filter columns from output.

Example

Data

id,created_at,updated_at,deleted_at,first_name,last_name,age,city
76,<timestamp>,<timestamp>,<timestamp>,brian,downs,37,Phoenix

Command

align -f delimited_data_file -c 5,6,8

Output

brian,downs,Phoenix

latest release binary giving error output

v1.1.1 is running well on my Arch Linux machine.

latest release binary is giving error output as below :

align-linux: line 1: syntax error near unexpected token `newline'
align-linux: line 1: `!<arch>'

Last column in row empty if empty

If the last field (column) in the input is null, then the last column is completely omitted from the output.
In other words, the final delimiter does not appear in the row in the output.

This does not occur if the last column contains an empty quoted string.

Sample input:

first,last,email,address,title,phone
john,doe,[email protected],123 Any St.,Lord of Scrum,111-111-1111
jane,doe,[email protected],123 Any Street,,222-222-2222
tim,biziboi,[email protected],222 W. Avenue 3,"Director, Sr.",333-333-3333
,,,,,444-444-4444
,,,"222 3rd Ave., Apt. 4",,
,,,"222 3rd Ave., Apt. 4",,""
,,,,,
,,,,,555-555-5555
,,,,"Level 3,,, Manager",
grogu,,[email protected],?,"Formerly known as, baby yoda",

Result:

first , last    , email                , address                , title                          , phone        
john  , doe     , [email protected] , 123 Any St.            , Lord of Scrum                  , 111-111-1111 
jane  , doe     , [email protected]  , 123 Any Street         ,                                , 222-222-2222 
tim   , biziboi , [email protected] , 222 W. Avenue 3        , "Director, Sr."                , 333-333-3333 
      ,         ,                      ,                        ,                                , 444-444-4444 
      ,         ,                      , "222 3rd Ave., Apt. 4" ,                                
      ,         ,                      , "222 3rd Ave., Apt. 4" ,                                , ""           
      ,         ,                      ,                        ,                                
      ,         ,                      ,                        ,                                , 555-555-5555 
      ,         ,                      ,                        , "Level 3,,, Manager"           
grogu ,         , [email protected]      , ?                      , "Formerly known as, baby yoda" 

Output should have leading padding

The output is currently aligned, but with without any leading padding on each field. Ex:

Wow    ,Wow
Umyeah ,Oh wow

It would look nicer with leading padding. Ex:

Wow    , Wow
Umyeah , Oh wow

Specify a multi-byte delimiter corrupts output

If a multli-byte character is specified as the character to align on, then the output gets corrupted.

I thought that the ColumnCounts() method handled this with the simple range loop, but something is still broken.

No Whitespace In Output

Is there a way to output the contents of the delimited file with no whitespace characters?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.