init
This commit is contained in:
commit
f6140a1fc3
11 files changed
+308
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"gosh/types"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
func GoshGrep(sh *types.Shell, args map[string]string, input string, regex string) types.CmdOutput {
|
||||
re := regexp.MustCompile(regex)
|
||||
lines := strings.Split(input, "\n")
|
||||
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
|
||||
value, ok := args["-C"]
|
||||
var err error
|
||||
context_lines := 0
|
||||
|
||||
if ok {
|
||||
context_lines, err = strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||
}
|
||||
context_lines++
|
||||
}
|
||||
|
||||
var output strings.Builder
|
||||
for i, line := range lines {
|
||||
if re.MatchString(line) {
|
||||
// Print behind
|
||||
if context_lines != 0 {
|
||||
for bi := i - context_lines; bi < i; bi++ {
|
||||
if bi < 0 {
|
||||
continue
|
||||
} else {
|
||||
output.WriteString(lines[bi] + "\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output.WriteString(line + "\n")
|
||||
|
||||
// Print after
|
||||
if context_lines != 0 {
|
||||
for ai := i + 1; ai < i+context_lines; ai++ {
|
||||
if ai > len(lines)-1 {
|
||||
continue
|
||||
} else {
|
||||
output.WriteString(lines[ai] + "\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return types.CmdOutput{Id: 0, Output: output.String()}
|
||||
}
|
||||
Reference in new issue
Block a user