Skip to content

Commit

Permalink
Merge pull request #2 from ryanuber/e-nopanic
Browse files Browse the repository at this point in the history
Remove flat string input
  • Loading branch information
ryanuber committed Apr 19, 2014
2 parents 9140a67 + 5172186 commit 785d943
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 69 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
language: go
go:
- tip
before_install:
- go get github.com/axw/gocov/gocov
- go get github.com/mattn/goveralls
- go get code.google.com/p/go.tools/cmd/cover
script:
- $HOME/gopath/bin/goveralls -repotoken E9Trto8c7lAwRJkOQR89OoUJRgKscAhqM
23 changes: 3 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Columnize
Easy column-formatted output for golang

[![Build Status](https://travis-ci.org/ryanuber/columnize.svg)](https://travis-ci.org/ryanuber/columnize)
[![Coverage Status](https://coveralls.io/repos/ryanuber/columnize/badge.png?branch=master)](https://coveralls.io/r/ryanuber/columnize?branch=master)

Columnize is a really small Go package that makes building CLI's a little bit
easier. In some CLI designs, you want to output a number similar items in a
Expand Down Expand Up @@ -32,8 +31,7 @@ func main() {
}
```

As you can see, you just give it a list of strings and a delimiter.
And the result:
As you can see, you just pass in a list of strings. And the result:

```
Name Gender Age
Expand All @@ -44,21 +42,6 @@ Sally Female 26
Columnize is tolerant of missing or empty fields, or even empty lines, so
passing in extra lines for spacing should show up as you would expect.

Columnize will also accept a plain string as input. This makes it easy if you
already have a CLI that can build up some output and just pass it through
Columnize, like so

```go
output := "Name | Gender | Age\n"
output += "Bob | Male | 38\n"
output += "Sally | Female | 26\n"

result := columnize.SimpleFormat(output)
fmt.Println(result)
```

Columnize will panic if it is passed a type other than `string` or `[]string`.

Configuration
=============

Expand All @@ -79,7 +62,7 @@ Usage
=====

```go
SimpleFormat(intput interface{}) string
SimpleFormat(intput []string) string

Format(input interface{}, config *Config) string
Format(input []string, config *Config) string
```
18 changes: 3 additions & 15 deletions columnize.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,8 @@ func getStringFormat(widths []int, columns int, space string) string {

// Format is the public-facing interface that takes either a plain string
// or a list of strings and returns nicely aligned output.
func Format(input interface{}, config *Config) string {
func Format(lines []string, config *Config) string {
var result string
var lines []string

switch in := input.(type) {
case string:
lines = strings.Split(in, "\n")

case []string:
lines = in

default:
panic("Expected string or []string")
}

widths := getWidthsFromLines(lines, config.Delim)

Expand All @@ -102,7 +90,7 @@ func Format(input interface{}, config *Config) string {
}

// Convenience function for using Columnize as easy as possible.
func SimpleFormat(input interface{}) string {
func SimpleFormat(lines []string) string {
config := DefaultConfig()
return Format(input, config)
return Format(lines, config)
}
28 changes: 0 additions & 28 deletions columnize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ func TestListOfStringsInput(t *testing.T) {
}
}

func TestStringInput(t *testing.T) {
input := "Column A | Column B | Column C\n"
input += "x | y | z"

config := DefaultConfig()
output := Format(input, config)

expected := "Column A Column B Column C\n"
expected += "x y z"

if output != expected {
t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output)
}
}

func TestEmptyLinesOutput(t *testing.T) {
input := []string{
"Column A | Column B | Column C",
Expand Down Expand Up @@ -176,16 +161,3 @@ func TestSimpleFormat(t *testing.T) {
t.Fatalf("\nexpected:\n%s\n\ngot:\n%s", expected, output)
}
}

func TestBadOptions(t *testing.T) {
input := 123
config := DefaultConfig()

defer func() {
if recover() == nil {
t.Fatalf("Expected panic passing unsupported type")
}
}()

Format(input, config)
}

0 comments on commit 785d943

Please sign in to comment.