Skip to content

Commit

Permalink
v0.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
shenwei356 committed Nov 23, 2016
1 parent b037d13 commit 69dbb9b
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 75 deletions.
29 changes: 22 additions & 7 deletions csvtk/cmd/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd

import (
"fmt"
"os"
"runtime"
"sort"
"strconv"
Expand All @@ -39,6 +40,16 @@ var boxCmd = &cobra.Command{
Short: "plot boxplot",
Long: `plot boxplot
Notes:
1. Output file can be set by flag -o/--out-file.
2. File format is determined by the out file suffix.
Supported formats: eps, jpg|jpeg, pdf, png, svg, and tif|tiff
3. If flag -o/--out-file not set (default), image is written to stdout,
you can display the image by pipping to "display" command of Imagemagic
or just redirect to file.
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
Expand All @@ -58,10 +69,6 @@ var boxCmd = &cobra.Command{
horiz := getFlagBool(cmd, "horiz")
w := vg.Length(getFlagNonNegativeFloat64(cmd, "box-width"))

if config.OutFile == "-" {
config.OutFile = "boxplot.png"
}

groups := make(map[string]plotter.Values)
groupOrderMap := make(map[string]int)
var f float64
Expand Down Expand Up @@ -185,10 +192,18 @@ var boxCmd = &cobra.Command{
log.Warning("flag --y-max ignored for command box")
}

// Save the plot to a PNG file.
if err := p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch, config.OutFile); err != nil {
// Save image
if isStdin(config.OutFile) {
fh, err := p.WriterTo(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
plotConfig.format)
checkError(err)
_, err = fh.WriteTo(os.Stdout)
checkError(err)
} else {
checkError(p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
config.OutFile))
}
},
}
Expand Down
26 changes: 16 additions & 10 deletions csvtk/cmd/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cmd

import (
"bytes"
"encoding/csv"
"fmt"
"regexp"
Expand Down Expand Up @@ -84,6 +85,7 @@ var grepCmd = &cobra.Command{
useRegexp := getFlagBool(cmd, "use-regexp")
invert := getFlagBool(cmd, "invert")
verbose := getFlagBool(cmd, "verbose")
noHighlight := getFlagBool(cmd, "no-highlight")

patternsMap := make(map[string]*regexp.Regexp)
for _, pattern := range patterns {
Expand All @@ -105,6 +107,7 @@ var grepCmd = &cobra.Command{
}

if patternFile != "" {
noHighlight = true
fn := func(line string) (interface{}, bool, error) {
line = strings.TrimRight(line, "\r\n")
if line == "" {
Expand Down Expand Up @@ -139,7 +142,6 @@ var grepCmd = &cobra.Command{

fuzzyFields := getFlagBool(cmd, "fuzzy-fields")
// fuzzyFields := false
noHighlight := getFlagBool(cmd, "no-highlight")
var writer *csv.Writer
if config.OutFile == "-" {
outfh := colorable.NewColorableStdout()
Expand Down Expand Up @@ -269,13 +271,15 @@ var grepCmd = &cobra.Command{

var target string
var hitOne, hit bool
var reHit *regexp.Regexp
for _, f := range fields {
target = record[f-1]
hitOne = false
if useRegexp {
for _, re := range patternsMap {
if re.MatchString(target) {
hitOne = true
reHit = re
break
}
}
Expand Down Expand Up @@ -306,20 +310,22 @@ var grepCmd = &cobra.Command{
}

if !noHighlight {
var j int
var buf bytes.Buffer
record2 := make([]string, len(record)) //with color
for i, c := range record {
if _, ok := fieldsMap[i+1]; (!negativeFields && ok) || (negativeFields && !ok) {
if useRegexp {
v := ""
for _, re := range patternsMap {
if re.MatchString(record[i]) {
v = re.ReplaceAllString(c, redText(re.FindAllString(c, 1)[0]))
break
} else {
v = c
}
j = 0
buf.Reset()
for _, f := range reHit.FindAllStringIndex(c, -1) {
buf.WriteString(c[j:f[0]])
buf.WriteString(redText(c[f[0]:f[1]]))
j = f[1]
}
record2[i] = v
buf.WriteString(c[j:len(c)])

record2[i] = buf.String()
} else {
record2[i] = redText(c)
}
Expand Down
2 changes: 1 addition & 1 deletion csvtk/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

// VERSION of csvtk
const VERSION = "0.4.1"
const VERSION = "0.4.2"

func checkError(err error) {
if err != nil {
Expand Down
28 changes: 22 additions & 6 deletions csvtk/cmd/hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd

import (
"fmt"
"os"
"runtime"
"strconv"

Expand All @@ -38,6 +39,15 @@ var histCmd = &cobra.Command{
Short: "plot histogram",
Long: `plot histogram
Notes:
1. Output file can be set by flag -o/--out-file.
2. File format is determined by the out file suffix.
Supported formats: eps, jpg|jpeg, pdf, png, svg, and tif|tiff
3. If flag -o/--out-file not set (default), image is written to stdout,
you can display the image by pipping to "display" command of Imagemagic
or just redirect to file.
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
Expand All @@ -60,9 +70,7 @@ var histCmd = &cobra.Command{
if plotConfig.ylab == "" {
plotConfig.ylab = "Count"
}
if config.OutFile == "-" {
config.OutFile = "histogram.png"
}

if plotConfig.xlab == "" && plotConfig.groupFieldStr == "" && len(headerRow) > 0 {
plotConfig.xlab = headerRow[0]
}
Expand Down Expand Up @@ -126,10 +134,18 @@ var histCmd = &cobra.Command{
p.Y.Max = plotConfig.ymax
}

// Save the plot to a PNG file.
if err := p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch, config.OutFile); err != nil {
// Save image
if isStdin(config.OutFile) {
fh, err := p.WriterTo(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
plotConfig.format)
checkError(err)
_, err = fh.WriteTo(os.Stdout)
checkError(err)
} else {
checkError(p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
config.OutFile))
}
},
}
Expand Down
31 changes: 21 additions & 10 deletions csvtk/cmd/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cmd

import (
"fmt"
"os"
"runtime"
"sort"
"strconv"
Expand All @@ -41,6 +42,15 @@ var lineCmd = &cobra.Command{
Short: "line plot and scatter plot",
Long: `line plot and scatter plot
Notes:
1. Output file can be set by flag -o/--out-file.
2. File format is determined by the out file suffix.
Supported formats: eps, jpg|jpeg, pdf, png, svg, and tif|tiff
3. If flag -o/--out-file not set (default), image is written to stdout,
you can display the image by pipping to "display" command of Imagemagic
or just redirect to file.
`,
Run: func(cmd *cobra.Command, args []string) {
config := getConfigs(cmd)
Expand Down Expand Up @@ -100,13 +110,6 @@ var lineCmd = &cobra.Command{

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

if config.OutFile == "-" {
if scatter {
config.OutFile = "scatter.png"
} else {
config.OutFile = "lineplot.png"
}
}
groups := make(map[string]plotter.XYs)
groupOrderMap := make(map[string]int)
var x, y float64
Expand Down Expand Up @@ -232,10 +235,18 @@ var lineCmd = &cobra.Command{
p.Y.Max = plotConfig.ymax
}

// Save the plot to a PNG file.
if err := p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch, config.OutFile); err != nil {
// Save image
if isStdin(config.OutFile) {
fh, err := p.WriterTo(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
plotConfig.format)
checkError(err)
_, err = fh.WriteTo(os.Stdout)
checkError(err)
} else {
checkError(p.Save(plotConfig.width*vg.Inch,
plotConfig.height*vg.Inch,
config.OutFile))
}
},
}
Expand Down
17 changes: 16 additions & 1 deletion csvtk/cmd/plot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ var plotCmd = &cobra.Command{
Notes:
1. File format is determined by the out file (-o/--out-file) suffix.
1. Output file can be set by flag -o/--out-file.
2. File format is determined by the out file suffix.
Supported formats: eps, jpg|jpeg, pdf, png, svg, and tif|tiff
3. If flag -o/--out-file not set (default), image is written to stdout,
you can display the image by pipping to "display" command of Imagemagic
or just redirect to file.
`,
}
Expand All @@ -65,6 +69,9 @@ func init() {
plotCmd.PersistentFlags().IntP("label-size", "", 14, "label font size")
plotCmd.PersistentFlags().Float64P("axis-width", "", 1.5, "axis width")
plotCmd.PersistentFlags().Float64P("tick-width", "", 1.5, "axis tick width")

plotCmd.PersistentFlags().StringP("format", "", "png", `image format for stdout when flag -o/--out-file not given. available values: eps, jpg|jpeg, pdf, png, svg, and tif|tiff.`)

}

func getPlotConfigs(cmd *cobra.Command) *plotConfigs {
Expand Down Expand Up @@ -132,6 +139,13 @@ func getPlotConfigs(cmd *cobra.Command) *plotConfigs {
}
}

config.format = getFlagString(cmd, "format")
switch strings.ToLower(config.format) {
case "eps", "jpg", "jpeg", "pdf", "png", "svg", "tif", "tiff":
default:
checkError(fmt.Errorf("invalid image format. available format: eps, jpg|jpeg, pdf, png, svg, and tif|tiff"))
}

return config
}

Expand All @@ -142,4 +156,5 @@ type plotConfigs struct {
width, height, axisWidth, tickWidth vg.Length
xmin, xmax, ymin, ymax float64
xminStr, xmaxStr, yminStr, ymaxStr string
format string
}
31 changes: 22 additions & 9 deletions csvtk/cmd/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ more on: http://shenwei356.github.io/csvtk/usage/#replace
Special replacement symbols:
{nr} Record number, starting from 1
{kv} Corresponding value of the key ($1) by key-value file
{kv} Corresponding value of the key (captured variable $n) by key-value file,
n can be specified by flag -I (--key-capt-idx) (default: 1)
`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -83,6 +83,8 @@ Special replacement symbols:

kvFile := getFlagString(cmd, "kv-file")
keepKey := getFlagBool(cmd, "keep-key")
keyCaptIdx := getFlagPositiveInt(cmd, "key-capt-idx")
keyMissRepl := getFlagString(cmd, "key-miss-repl")

var replaceWithNR bool
if reNR.MatchString(replacement) {
Expand Down Expand Up @@ -166,6 +168,7 @@ Special replacement symbols:
var record2 []string // for output
var r string
var found []string
var founds [][]string
var k string
nr := 0
for chunk := range csvReader.Ch {
Expand Down Expand Up @@ -258,18 +261,25 @@ Special replacement symbols:
}

if replaceWithKV {
found = patternRegexp.FindStringSubmatch(record2[f])
if len(found) > 0 {
k = string(found[1])
founds = patternRegexp.FindAllStringSubmatch(record2[f], -1)
if len(founds) > 1 {
checkError(fmt.Errorf(`pattern "%s" matches multiple targets in "%s", this will cause chaos`, p, record2[f]))
}
if len(founds) > 0 {
found = founds[0]
if keyCaptIdx > len(found)-1 {
checkError(fmt.Errorf("value of flag -I (--key-capt-idx) overflows"))
}
k = string(found[keyCaptIdx])
if ignoreCase {
k = strings.ToLower(k)
}
if _, ok = kvs[k]; ok {
r = reKV.ReplaceAllString(r, kvs[k])
} else if keepKey {
r = reKV.ReplaceAllString(r, found[1])
r = reKV.ReplaceAllString(r, found[keyCaptIdx])
} else {
r = reKV.ReplaceAllString(r, "")
r = reKV.ReplaceAllString(r, keyMissRepl)
}
}
}
Expand All @@ -294,12 +304,15 @@ func init() {
replaceCmd.Flags().StringP("replacement", "r", "",
"replacement. supporting capture variables. "+
" e.g. $1 represents the text of the first submatch. "+
"ATTENTION: use SINGLE quote NOT double quotes in *nix OS or "+
"use the \\ escape character.")
"ATTENTION: for *nix OS, use SINGLE quote NOT double quotes or "+
`use the \ escape character. Record number is also supported by "{nr}".`+
`use ${1} instead of $1 when {kv} given!`)
replaceCmd.Flags().BoolP("ignore-case", "i", false, "ignore case")
replaceCmd.Flags().StringP("kv-file", "k", "",
`tab-delimited key-value file for replacing key with value when using "{kv}" in -r (--replacement)`)
replaceCmd.Flags().BoolP("keep-key", "K", false, "keep the key as value when no value found for the key")
replaceCmd.Flags().IntP("key-capt-idx", "I", 1, "capture variable index of key (1-based)")
replaceCmd.Flags().StringP("key-miss-repl", "", "", "replacement for key with no corresponding value")
}

var reNR = regexp.MustCompile(`\{(NR|nr)\}`)
Expand Down
Loading

0 comments on commit 69dbb9b

Please sign in to comment.