Compare commits

...

3 Commits

Author SHA1 Message Date
JISAUAY
4add774464 "i forgot to commit the rest" 2025-06-30 13:17:57 -05:00
JISAUAY
c198640510 "added system path execution" 2025-06-30 13:17:38 -05:00
JISAUAY
789d268ee6 added better auto complete 2025-06-27 15:57:36 -05:00
9 changed files with 126 additions and 21 deletions

View File

@ -2,7 +2,7 @@ package cmd
import (
"gosh/types"
"gosh/util"
"gosh/utils"
"os"
"github.com/fatih/color"
@ -10,7 +10,7 @@ import (
func GoshCat(sh *types.Shell, file_path string) types.CmdOutput {
file_path = util.ExpandHome(file_path)
file_path = utils.ExpandHome(file_path)
data, err := os.ReadFile(file_path)
red := color.New(color.FgRed).SprintFunc()

View File

@ -4,14 +4,14 @@ import (
"os"
"gosh/types"
"gosh/util"
"gosh/utils"
"github.com/fatih/color"
)
func GoshCd(sh *types.Shell, dir string) types.CmdOutput {
dir = util.ExpandHome(dir)
dir = utils.ExpandHome(dir)
err := os.Chdir(dir)
red := color.New(color.FgRed).SprintFunc()

View File

@ -2,7 +2,7 @@ package cmd
import (
"gosh/types"
"gosh/util"
"gosh/utils"
"os"
"strings"
@ -19,7 +19,7 @@ func GoshLs(sh *types.Shell, paths ...string) types.CmdOutput {
dir = sh.Cd
}
dir = util.ExpandHome(dir)
dir = utils.ExpandHome(dir)
children, err := os.ReadDir(dir)

View File

@ -2,7 +2,7 @@ package cmd
import (
"gosh/types"
"gosh/util"
"gosh/utils"
"path/filepath"
"github.com/fatih/color"
@ -10,7 +10,7 @@ import (
func GolshPyapath(sh *types.Shell, path string) types.CmdOutput {
path = util.ExpandHome(path)
path = utils.ExpandHome(path)
abs, err := filepath.Abs(path)

2
go.mod
View File

@ -3,6 +3,8 @@ module gosh
go 1.21.5
require (
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect

7
go.sum
View File

@ -1,3 +1,9 @@
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@ -5,6 +11,7 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=

51
main.go
View File

@ -1,15 +1,17 @@
package main
import (
"bufio"
"fmt"
"gosh/cmd"
"gosh/types"
"gosh/utils"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/anmitsu/go-shlex"
"github.com/chzyer/readline"
"github.com/fatih/color"
)
@ -105,7 +107,12 @@ func gosh_process_input(sh *types.Shell, input string) {
for _, cmdStr := range commands {
cmdStr = strings.TrimSpace(cmdStr)
parts := strings.Fields(cmdStr)
parts, err := shlex.Split(cmdStr, false)
if err != nil {
fmt.Println(err)
}
if len(parts) == 0 {
continue
}
@ -153,7 +160,18 @@ func gosh_process_input(sh *types.Shell, input string) {
default:
if !check_pathes(sh, parts[0], parts[1:]) {
fmt.Println("Unknown command:", parts[0])
comd := exec.Command(parts[0], parts[1:]...)
out, err := comd.CombinedOutput()
if err != nil {
fmt.Print("Error: ")
color.Red(err.Error())
return
}
fmt.Println(string(out))
return
}
}
@ -163,10 +181,6 @@ func gosh_process_input(sh *types.Shell, input string) {
}
func main() {
reader := bufio.NewReader(os.Stdin)
c := color.New(color.FgCyan)
dir, _ := os.Getwd()
// Init shell
@ -181,18 +195,31 @@ func main() {
_, err := os.Stat(golshrc_path)
golshrc_exists := err == nil
fmt.Println(golshrc_exists)
if golshrc_exists {
fmt.Println("exists")
process_golshscript(&sh, golshrc_path)
}
// Set up readline
rl, err := readline.NewEx(&readline.Config{
Prompt: sh.Cd + "> ",
AutoComplete: &utils.AutoCompleter{Sh: &sh},
InterruptPrompt: "^C",
EOFPrompt: "exit",
})
if err != nil {
panic(err)
}
defer rl.Close()
cd_path_color := color.New(color.FgBlue)
var should_exit = false
for {
c.Print(sh.Cd)
fmt.Print("> ")
rl.SetPrompt(cd_path_color.Sprint("GOLSH "+sh.Cd+"") + " ")
input, err := reader.ReadString('\n')
input, err := rl.Readline()
if err != nil {
fmt.Println("Error reading input: ", err)

69
utils/AutoCompleter.go Normal file
View File

@ -0,0 +1,69 @@
package utils
import (
"gosh/types"
"os"
"strings"
)
type AutoCompleter struct {
Sh *types.Shell
}
func (a *AutoCompleter) Do(line []rune, pos int) ([][]rune, int) {
cmds := []string{"ls", "cd", "pwd", "cat", "grep", "clear", "apath", "exit"}
potential_suggestions := []string{}
s_line := string(line)
words := strings.Fields(s_line)
last_word := ""
if len(words) > 0 {
last_word = words[len(words)-1]
}
// If typing the command (first word), suggest commands
if len(words) == 0 || (len(words) == 1 && s_line[len(s_line)-1] != ' ') {
potential_suggestions = append(potential_suggestions, cmds...)
} else if words[0] == "cd" || words[0] == "ls" {
// Suggest directories for 'cd'
last_word := ""
if len(words) > 1 {
last_word = words[len(words)-1]
}
children, err := os.ReadDir(a.Sh.Cd)
if err == nil {
for _, child := range children {
if child.IsDir() && (last_word == "" || strings.HasPrefix(child.Name(), last_word)) {
potential_suggestions = append(potential_suggestions, child.Name())
}
}
}
} else if words[0] == "cat" {
// Suggest directories for 'cat'
last_word := ""
if len(words) > 1 {
last_word = words[len(words)-1]
}
children, err := os.ReadDir(a.Sh.Cd)
if err == nil {
for _, child := range children {
if !child.IsDir() && (last_word == "" || strings.HasPrefix(child.Name(), last_word)) {
potential_suggestions = append(potential_suggestions, child.Name())
}
}
}
}
var suggestions [][]rune
for _, s := range potential_suggestions {
// Only append the part that should be completed (remove the prefix)
if last_word != "" && strings.HasPrefix(s, last_word) {
suggestions = append(suggestions, []rune(s[len(last_word):]))
} else {
suggestions = append(suggestions, []rune(s))
}
}
return suggestions, len([]rune(s_line))
}

View File

@ -1,4 +1,4 @@
package util
package utils
import (
"os"