Skip to content

Commit 1cd138e

Browse files
committed
fix highlight failing in windows
1 parent a4e45e6 commit 1cd138e

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

csvtk/cmd/grep.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/shenwei356/breader"
3232
"github.com/spf13/cobra"
3333
"github.com/fatih/color"
34+
"github.com/mattn/go-colorable"
3435
)
3536

3637
// grepCmd represents the seq command
@@ -81,7 +82,6 @@ var grepCmd = &cobra.Command{
8182
ignoreCase := getFlagBool(cmd, "ignore-case")
8283
useRegexp := getFlagBool(cmd, "use-regexp")
8384
invert := getFlagBool(cmd, "invert")
84-
noHighlight := getFlagBool(cmd, "no-highlight")
8585

8686
patternsMap := make(map[string]*regexp.Regexp)
8787
for _, pattern := range patterns {
@@ -130,12 +130,20 @@ var grepCmd = &cobra.Command{
130130

131131
// fuzzyFields := getFlagBool(cmd, "fuzzy-fields")
132132
fuzzyFields := false
133+
noHighlight := false
134+
var writer *csv.Writer
135+
if config.OutFile == "-" {
136+
outfh := colorable.NewColorableStdout()
137+
writer = csv.NewWriter(outfh)
138+
}else{
139+
noHighlight = true
140+
outfh, err := xopen.Wopen(config.OutFile)
141+
checkError(err)
142+
defer outfh.Close()
143+
writer = csv.NewWriter(outfh)
144+
}
133145

134-
outfh, err := xopen.Wopen(config.OutFile)
135-
checkError(err)
136-
defer outfh.Close()
137146

138-
writer := csv.NewWriter(outfh)
139147
if config.OutTabs || config.Tabs {
140148
writer.Comma = '\t'
141149
} else {
@@ -292,7 +300,7 @@ var grepCmd = &cobra.Command{
292300
},
293301
}
294302

295-
var redText = color.New(color.FgRed).SprintFunc()
303+
var redText = color.New(color.FgHiRed).SprintFunc()
296304
func init() {
297305
RootCmd.AddCommand(grepCmd)
298306
grepCmd.Flags().StringP("fields", "f", "1", `key field, column name or index`)
@@ -301,7 +309,4 @@ func init() {
301309
grepCmd.Flags().BoolP("ignore-case", "i", false, `ignore case`)
302310
grepCmd.Flags().BoolP("use-regexp", "r", false, `patterns are regular expression`)
303311
grepCmd.Flags().BoolP("invert", "v", false, `invert match`)
304-
grepCmd.Flags().BoolP("no-highlight", "n", false, `no highlight for matched data. use this in Windows if highlight does not work.`)
305-
306-
//grepCmd.Flags().BoolP("fuzzy-fields", "F", false, `using fuzzy fileds, e.g. *name or id123*`)
307312
}

csvtk/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var RootCmd = &cobra.Command{
3434
Short: "Another cross-platform, efficient and practical CSV/TSV toolkit",
3535
Long: `Another cross-platform, efficient and practical CSV/TSV toolkit
3636
37-
Version: 0.2.6
37+
Version: 0.2.7
3838
3939
Author: Wei Shen <[email protected]>
4040

doc/docs/download.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66

77
## Current Version
88

9-
- [csvtk v0.2.6](https://github.com/shenwei356/csvtk/releases/tag/v0.2.6)
10-
- fix one error message of `grep`
11-
- highlight matched fields in result of `grep`
9+
- [csvtk v0.2.7](https://github.com/shenwei356/csvtk/releases/tag/v0.2.7)
10+
- fix highlight failing in windows
1211

1312
## Installation
1413

@@ -32,6 +31,9 @@ You can also add the directory of the executable file to environment variable
3231

3332
## Previous Versions
3433

34+
- [csvtk v0.2.6](https://github.com/shenwei356/csvtk/releases/tag/v0.2.6)
35+
- fix one error message of `grep`
36+
- highlight matched fields in result of `grep`
3537
- [csvtk v0.2.5](https://github.com/shenwei356/csvtk/releases/tag/v0.2.5)
3638
- fix bug of `stat` that failed to considerate files with header row
3739
- add subcommand `stat2` - summary of selected number fields

doc/docs/usage.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Usage
1818
```
1919
Another cross-platform, efficient and practical CSV/TSV toolkit
2020
21-
Version: 0.2.6
21+
Version: 0.2.7
2222
2323
Author: Wei Shen <[email protected]>
2424
@@ -253,7 +253,6 @@ Flags:
253253
-f, --fields string key field, column name or index (default "1")
254254
-i, --ignore-case ignore case
255255
-v, --invert invert match
256-
-n, --no-highlight no highlight for matched data. use this in Windows if highlight does not work.
257256
-p, --pattern value query pattern (multiple values supported) (default [])
258257
-P, --pattern-file string pattern files (could also be CSV format)
259258
-r, --use-regexp patterns are regular expression

doc/site

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 1c31d9c6d909d6242370dd8a1a83913594e04047
1+
Subproject commit 013d5bb16d63502f35761b1e8d865a79ed04715c

0 commit comments

Comments
 (0)