Skip to content

Commit

Permalink
(Closed #21) Added find / replace / uncolor option and colorize all
Browse files Browse the repository at this point in the history
  • Loading branch information
hahwul committed Feb 11, 2021
1 parent 1baa3c2 commit 9225469
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ func main() {
versionOption := flag.Bool("version", false, "Version of gee")
appendOption := flag.Bool("append", false, "Append mode for files")
chunkedLineOption := flag.Int("chunked", 0, "Chuked files from line (e.g output / output_1 / output_2)")
withLineOption := flag.Bool("with-line", false, "With line number")
withTimeOption := flag.Bool("with-time", false, "With timestamp")
withLineOption := flag.Bool("with-line", false, "With line number (colorize blue)")
withTimeOption := flag.Bool("with-time", false, "With timestamp (colorize blue)")
prefixOption := flag.String("prefix", "", "Prefix string")
suffixOption := flag.String("suffix", "", "Suffix string")
rmnlOption := flag.Bool("rmnl", false, "Remove newline(\\r\\n)")
distributeOption := flag.Bool("distribute", false, "Distribution to files")
colorOption := flag.Bool("uncolor", false, "Uncolorize stdout")
regexOption := flag.String("regex", "", "Match with Regular Expression (like grep)")
regexvOption := flag.String("regexv", "", "Unmatch with Regular Expression (like grep -v)")
findOption := flag.String("find", "", "Find string in line (colorize red)")
replaceOption := flag.String("replace", "", "Replace string in line with '-find' option")

// Custom usage
flag.Usage = func() {
Expand Down Expand Up @@ -66,6 +69,9 @@ func main() {
Distribute: *distributeOption,
Regex: *regexOption,
RegexV: *regexvOption,
Replace: *replaceOption,
Find: *findOption,
Color: !*colorOption,
}

// Running gee app
Expand Down
13 changes: 4 additions & 9 deletions pkg/gee/gee.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,14 @@ func Gee(options model.Options) {
regexBool = RegexV(options.RegexV, l)
}
if regexBool {
line := ""
if options.WithLine {
l = "[" + strconv.Itoa(stdLine) + "] " + l
}
if options.WithTimestamp {
l = "[" + GetNowTime() + "] " + l
}
linec, line := StringProc(l, stdLine, options)

// Prefix and Suffix
line = options.Prefix + l + options.Suffix
line = options.Prefix + line + options.Suffix
linec = options.Prefix + linec + options.Suffix

// Print to Stdout
StdPrint(line, options)
StdPrint(linec, options)

// Write to files
if (stdLine > options.ChunkedLine) && (options.ChunkedLine != 0) {
Expand Down
33 changes: 33 additions & 0 deletions pkg/gee/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package gee

import (
"strconv"
"strings"

"github.com/hahwul/gee/pkg/model"
"github.com/logrusorgru/aurora"
)

// StringProc is string
func StringProc(l string, stdLine int, options model.Options) (string, string) {
result := l
resultPlain := l
au := aurora.NewAurora(options.Color)
if options.Find != "" {
if options.Replace != "" {
result = strings.Replace(result, options.Find, au.BrightRed(options.Replace).String(), -1)
resultPlain = strings.Replace(resultPlain, options.Find, options.Replace, -1)
} else {
result = strings.Replace(result, options.Find, au.BrightRed(options.Find).String(), -1)
}
}
if options.WithLine {
result = au.BrightBlue("["+strconv.Itoa(stdLine)+"] ").String() + result
resultPlain = "[" + strconv.Itoa(stdLine) + "] " + resultPlain
}
if options.WithTimestamp {
result = au.BrightBlue("["+GetNowTime()+"] ").String() + result
resultPlain = "[" + GetNowTime() + "] " + resultPlain
}
return result, resultPlain
}
3 changes: 3 additions & 0 deletions pkg/model/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ type Options struct {
RegexV string
Split string
Distribute bool
Replace string
Find string
Color bool
}

0 comments on commit 9225469

Please sign in to comment.