init
This commit is contained in:
commit
f6140a1fc3
11 files changed
+308
No files matched your search
+18
@@ -0,0 +1,18 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"gosh/types"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
func GoshCat(sh *types.Shell, filename string) types.CmdOutput {
|
||||
data, err := os.ReadFile(filename)
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
if err != nil {
|
||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||
}
|
||||
|
||||
return types.CmdOutput{Id: 0, Output: string(data)}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gosh/types"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
func GoshCd(sh *types.Shell, dir string) types.CmdOutput {
|
||||
err := os.Chdir(dir)
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
if err != nil {
|
||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||
}
|
||||
|
||||
dir, _ = os.Getwd()
|
||||
sh.Cd = dir
|
||||
|
||||
return types.CmdOutput{Id: 0, Output: ""}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import "fmt"
|
||||
|
||||
func GoshClear() {
|
||||
fmt.Print("\033[H\033[2J")
|
||||
}
|
||||
+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()}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"gosh/types"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
func GoshLs(sh *types.Shell) types.CmdOutput {
|
||||
children, err := os.ReadDir(sh.Cd)
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
if err != nil {
|
||||
return types.CmdOutput{Id: 1, Output: red(err)}
|
||||
}
|
||||
|
||||
var output strings.Builder
|
||||
for _, child := range children {
|
||||
if child.IsDir() {
|
||||
output.WriteString(color.CyanString(child.Name()+"/") + "\n")
|
||||
} else {
|
||||
output.WriteString(child.Name() + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
return types.CmdOutput{Id: 0, Output: output.String()}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"gosh/types"
|
||||
)
|
||||
|
||||
func GoshPwd(sh *types.Shell) types.CmdOutput {
|
||||
return types.CmdOutput{Id: 0, Output: sh.Cd}
|
||||
}
|
||||
Reference in new issue
Block a user