-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
450 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import "github.com/keyneston/mktable/table" | ||
|
||
type FormatValue struct { | ||
format table.Format | ||
} | ||
|
||
func (fv *FormatValue) Get() interface{} { | ||
return fv.GetFormat() | ||
} | ||
|
||
func (fv *FormatValue) GetFormat() table.Format { | ||
if fv.format == "" { | ||
return table.FormatRE | ||
} | ||
|
||
return fv.format | ||
} | ||
|
||
func (fv *FormatValue) String() string { | ||
return string(fv.format) | ||
} | ||
|
||
func (fv *FormatValue) Set(in string) error { | ||
f, err := table.ParseFormat(in) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fv.format = f | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package table | ||
|
||
import "regexp" | ||
|
||
type TableConfig struct { | ||
MaxPadding int | ||
SkipHeaders bool | ||
Format Format | ||
Alignments map[int]Alignment | ||
Seperator *regexp.Regexp | ||
NewLine NewLine | ||
} | ||
|
||
func NewConfig() TableConfig { | ||
return TableConfig{} | ||
} | ||
|
||
func (t TableConfig) SetSeperator(in string) TableConfig { | ||
t.Seperator = regexp.MustCompile(in) | ||
t.Format = FormatRE | ||
|
||
return t | ||
} | ||
|
||
func (t TableConfig) SetFormat(format Format) TableConfig { | ||
t.Format = format | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package table | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Format string | ||
|
||
const ( | ||
FormatRE Format = "regexp" | ||
FormatCSV Format = "csv" | ||
FormatTSV Format = "tsv" | ||
FormatMK Format = "mk" | ||
FormatUnknown Format = "unknown" | ||
) | ||
|
||
func AllFormats() []string { | ||
return []string{ | ||
string(FormatRE), | ||
string(FormatMK), | ||
string(FormatCSV), | ||
} | ||
} | ||
|
||
func ParseFormat(in string) (Format, error) { | ||
switch strings.ToLower(in) { | ||
case "re", "regexp", "regex": | ||
return FormatRE, nil | ||
case "csv": | ||
return FormatCSV, nil | ||
// case "tsv": | ||
// return FormatTSV, nil | ||
case "mk", "markdown": | ||
return FormatMK, nil | ||
default: | ||
return FormatUnknown, fmt.Errorf("Unknown format %q", in) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package table | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
) | ||
|
||
func (t *Table) readFormatCSV(r io.Reader) error { | ||
reader := csv.NewReader(r) | ||
|
||
var err error | ||
t.data, err = reader.ReadAll() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package table | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
) | ||
|
||
func TestReadFormatCSV(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input string | ||
expected [][]string | ||
} | ||
|
||
cases := []testCase{ | ||
{ | ||
name: "basic", input: "a\nb\nc\n", | ||
expected: [][]string{{"a"}, {"b"}, {"c"}}, | ||
}, | ||
{ | ||
name: "multi-column", input: "a,1\nb,2\nc,3\n", | ||
expected: [][]string{{"a", "1"}, {"b", "2"}, {"c", "3"}}, | ||
}, | ||
{ | ||
name: "quotations", input: "a,\",\"\nb,2\nc,3\n", | ||
expected: [][]string{{"a", ","}, {"b", "2"}, {"c", "3"}}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
tb := NewTable(NewConfig().SetFormat(FormatCSV)) | ||
if err := tb.Read(bytes.NewBufferString(c.input)); err != nil { | ||
t.Errorf("Error doing read: %v", err) | ||
continue | ||
} | ||
|
||
if diff := deep.Equal(c.expected, tb.data); diff != nil { | ||
t.Errorf("Table.Read(%q) =\n%v", c.name, strings.Join(diff, "\n")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package table | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"io" | ||
"strings" | ||
) | ||
|
||
func (t *Table) readFormatMK(r io.Reader) error { | ||
scanner := bufio.NewScanner(r) | ||
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { | ||
start := 0 | ||
for i := range data { | ||
switch data[i] { | ||
case '\n': | ||
token := bytes.TrimSpace(data[start:i]) | ||
if len(token) > 0 { | ||
return i, token, nil | ||
} | ||
|
||
return i + 1, []byte{'\n'}, nil | ||
case '|': | ||
if i > 0 && data[i-1] == '\\' { | ||
continue | ||
} | ||
if i == 0 { | ||
start = i + 1 | ||
continue | ||
} | ||
|
||
token := bytes.TrimSpace(data[start:i]) | ||
return i + 1, token, nil | ||
} | ||
} | ||
|
||
return 0, nil, nil | ||
}) | ||
|
||
current := []string{} | ||
alignments := map[int]Alignment{} | ||
isHeader := false | ||
column := 0 | ||
|
||
for scanner.Scan() { | ||
token := scanner.Text() | ||
|
||
if token == "\n" { | ||
if isHeader { | ||
t.Alignments = alignments | ||
} else { | ||
t.data = append(t.data, current) | ||
} | ||
|
||
alignments = map[int]Alignment{} | ||
column = 0 | ||
current = nil | ||
isHeader = false | ||
continue | ||
} | ||
|
||
// Check if it is likely part of a header row, by removing all header | ||
// row chars and seeing if we have nothing left | ||
if len(strings.Trim(token, ":-")) == 0 { | ||
isHeader = true | ||
alignments[column] = parseAlignmentHeader(token) | ||
} | ||
|
||
current = append(current, token) | ||
|
||
column++ | ||
} | ||
if err := scanner.Err(); err != nil && !errors.Is(err, io.EOF) { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package table | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
) | ||
|
||
func TestReadFormatMK(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input string | ||
expected [][]string | ||
} | ||
|
||
cases := []testCase{ | ||
{ | ||
name: "basic", input: "| a | b | c |\n", | ||
expected: [][]string{{"a", "b", "c"}}, | ||
}, | ||
{ | ||
name: "multiline", input: "| a | b | c |\n| --- | ---| ---|\n|1 | 2|3|\n", | ||
expected: [][]string{{"a", "b", "c"}, {"1", "2", "3"}}, | ||
}, | ||
{ | ||
name: "trailing space", input: "| a | \n| --- |\n|1 |\n", | ||
expected: [][]string{{"a"}, {"1"}}, | ||
}, | ||
{ | ||
name: "header", input: "| --- | --- | --- |\n", | ||
expected: nil, | ||
}, | ||
{ | ||
name: "trailing pipe", input: "| a | b | \\| |\n", | ||
expected: [][]string{{"a", "b", `\|`}}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
tb := NewTable(TableConfig{ | ||
Format: FormatMK, | ||
}) | ||
if err := tb.Read(bytes.NewBufferString(c.input)); err != nil { | ||
t.Errorf("Error doing read: %v", err) | ||
continue | ||
} | ||
|
||
if diff := deep.Equal(c.expected, tb.data); diff != nil { | ||
t.Errorf("Table.Read(%q) =\n%v", c.name, strings.Join(diff, "\n")) | ||
} | ||
} | ||
} |
Oops, something went wrong.