Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Oct 5, 2016
1 parent 1942c9f commit ce4c936
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 170 deletions.
2 changes: 1 addition & 1 deletion csvtk/cmd/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var boxCmd = &cobra.Command{
runtime.GOMAXPROCS(config.NumCPUs)

file := files[0]
headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)
headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)

// =======================================

Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/csv2md.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var csv2mdCmd = &cobra.Command{
file := files[0]
fieldStr := "*"
fuzzyFields := true
headerRow, data, _ := parseCSVfile(cmd, config,
headerRow, _, data, _, _ := parseCSVfile(cmd, config,
file, fieldStr, fuzzyFields)

var header []string
Expand Down
16 changes: 14 additions & 2 deletions csvtk/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func fuzzyField2Regexp(field string) *regexp.Regexp {
}

func parseCSVfile(cmd *cobra.Command, config Config, file string,
fieldStr string, fuzzyFields bool) ([]string, [][]string, []int) {
fieldStr string, fuzzyFields bool) ([]string, []int, [][]string, []string, [][]string) {
fields, colnames, negativeFields, needParseHeaderRow := parseFields(cmd, fieldStr, config.NoHeaderRow)
var fieldsMap map[int]struct{}
var fieldsOrder map[int]int // for set the order of fields
Expand Down Expand Up @@ -413,7 +413,9 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string,
var colnamesMap map[string]*regexp.Regexp

var HeaderRow []string
var HeaderRowAll []string
var Data [][]string
var DataAll [][]string

checkFields := true

Expand Down Expand Up @@ -477,9 +479,13 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string,

items := make([]string, len(fields))
for i, f := range fields {
if f > len(record) {
continue
}
items[i] = record[f-1]
}
HeaderRow = items
HeaderRowAll = record
continue
}
if checkFields {
Expand Down Expand Up @@ -520,9 +526,15 @@ func parseCSVfile(cmd *cobra.Command, config Config, file string,
items[i] = record[f-1]
}
Data = append(Data, items)
if fieldStr != "*" {
DataAll = append(DataAll, record)
}
}
}
return HeaderRow, Data, fields
if fieldStr != "*" {
return HeaderRow, fields, Data, HeaderRowAll, DataAll
}
return HeaderRow, fields, Data, HeaderRowAll, Data
}

func removeComma(s string) string {
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var histCmd = &cobra.Command{
runtime.GOMAXPROCS(config.NumCPUs)

file := files[0]
headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)
headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)

// =======================================

Expand Down
3 changes: 1 addition & 2 deletions csvtk/cmd/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Multiple keys supported, but the orders are ignored.
var key string
var items []string
for i, file := range files {
headerRow, data, fields := parseCSVfile(cmd, config,
_, fields, _, headerRow, data := parseCSVfile(cmd, config,
file, allFields[i], fuzzyFields)
if firstFile {
HeaderRow, Data, Fields = headerRow, data, fields
Expand All @@ -100,7 +100,6 @@ Multiple keys supported, but the orders are ignored.
for _, f := range fields {
fieldsMap[f] = struct{}{}
}

// csv to map
keysMaps := make(map[string][]string)
items = make([]string, len(fields))
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var lineCmd = &cobra.Command{
}

file := files[0]
headerRow, data, fields := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)
headerRow, fields, data, _, _ := parseCSVfile(cmd, config, file, plotConfig.fieldStr, false)

// =======================================

Expand Down
15 changes: 7 additions & 8 deletions csvtk/cmd/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,25 @@ var prettyCmd = &cobra.Command{

alignRight := getFlagBool(cmd, "align-right")
separator := getFlagString(cmd, "separator")
minWidth :=getFlagNonNegativeInt(cmd, "min-width")
maxWidth :=getFlagNonNegativeInt(cmd, "max-width")
minWidth := getFlagNonNegativeInt(cmd, "min-width")
maxWidth := getFlagNonNegativeInt(cmd, "max-width")

outfh, err := xopen.Wopen(config.OutFile)
checkError(err)
defer outfh.Close()


file := files[0]
fieldStr := "*"
fuzzyFields := true
headerRow, data, _ := parseCSVfile(cmd, config,
headerRow, _, data, _, _ := parseCSVfile(cmd, config,
file, fieldStr, fuzzyFields)

var header []string
var datas [][]string
if len(headerRow) > 0 {
header = headerRow
datas = data
} else{
} else {
if len(data) == 0 {
checkError(fmt.Errorf("no data found in file: %s", file))
} else if len(data) > 0 {
Expand All @@ -76,16 +75,16 @@ var prettyCmd = &cobra.Command{
columns := make([]prettytable.Column, len(header))
for i, c := range header {
columns[i] = prettytable.Column{Header: c, AlignRight: alignRight,
MinWidth: minWidth, MaxWidth:maxWidth}
MinWidth: minWidth, MaxWidth: maxWidth}
}
tbl, err := prettytable.NewTable(columns...)
checkError(err)
tbl.Separator = separator
for _, record := range datas {
// have to do this stupid convertion
record2:=make([]interface{}, len(record))
record2 := make([]interface{}, len(record))
for i, r := range record {
record2[i]=r
record2[i] = r
}
tbl.AddRow(record2...)
}
Expand Down
4 changes: 2 additions & 2 deletions csvtk/cmd/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"strconv"
"strings"

"github.com/shenwei356/xopen"
"github.com/shenwei356/util/stringutil"
"github.com/shenwei356/xopen"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -88,7 +88,7 @@ var sortCmd = &cobra.Command{
}

file := files[0]
headerRow, data, _ := parseCSVfile(cmd, config,
headerRow, _, _, _, data := parseCSVfile(cmd, config,
file, fieldsStr, fuzzyFields)
if len(data) == 0 {
checkError(fmt.Errorf("no data to sort"))
Expand Down
Loading

0 comments on commit ce4c936

Please sign in to comment.