Skip to content

Commit

Permalink
must error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmtmmnk committed Feb 26, 2021
1 parent a86fbb1 commit a7a38d0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
11 changes: 10 additions & 1 deletion filter/ripgrep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package filter

import (
"os/exec"

"github.com/tjmtmmnk/ilse/util"
)

type rg struct{}
Expand Down Expand Up @@ -47,8 +49,15 @@ func (r *rg) Search(q string, option *SearchOption) ([]SearchResult, error) {
}

if err != nil {
util.Logger.Warn(err)
return []SearchResult{}, err
}

results, err := convert(string(out), option)
if err != nil {
util.Logger.Warn(err)
return []SearchResult{}, err
}

return convert(string(out), option), nil
return results, nil
}
30 changes: 18 additions & 12 deletions filter/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filter

import (
"errors"
"strconv"
"strings"
)
Expand All @@ -13,36 +14,41 @@ func isValidRegex(q string) bool {
return true
}

func convert(result string, option *SearchOption) []SearchResult {
var results []SearchResult
func convert(result string, option *SearchOption) ([]SearchResult, error) {
results := make([]SearchResult, 0, option.Limit)
for i, s := range strings.Split(string(result), "\n") {
if i > option.Limit {
break
}
ignore, result := split(s, option)
if !ignore {
results = append(results, result)
result, err := split(s, option)
if err != nil {
return []SearchResult{}, err
}
if result != nil {
results = append(results, *result)
}
}
return results
return results, nil
}

func split(str string, option *SearchOption) (ignore bool, result SearchResult) {
func split(str string, option *SearchOption) (*SearchResult, error) {
switch option.Command {
case RipGrep:
// first remove reset flag included in path, line
str = strings.Replace(str, "\x1b[0m", "", 4)
splitted := strings.Split(str, ":")
if len(splitted) < 3 {
ignore = true
return
return nil, nil
}

fileName := splitted[0]
lineNum, _ := strconv.Atoi(splitted[1])
lineNum, err := strconv.Atoi(splitted[1])
if err != nil {
return nil, errors.New("line number wrong format")
}
// change reset flag included in text to black foreground
text := strings.ReplaceAll(splitted[2], "\x1b[0m", "\x1b[39;40m")
result = SearchResult{fileName, lineNum, text}
return &SearchResult{fileName, lineNum, text}, nil
}
return
return nil, errors.New("wrong option")
}

0 comments on commit a7a38d0

Please # to comment.