From 9e46cc910577da2dd85eabf9287afc662a126ef2 Mon Sep 17 00:00:00 2001 From: JISAUAY Date: Fri, 27 Jun 2025 11:23:15 -0500 Subject: [PATCH] added ~ as home processing --- .golshrc | 1 - cmd/cat.go | 8 ++++++-- cmd/cd.go | 4 ++++ cmd/ls.go | 17 +++++++++++++++-- cmd/pyapath.go | 4 ++++ main.go | 34 +++++++++++++++++++++++++++++++++- util/util.go | 17 +++++++++++++++++ 7 files changed, 79 insertions(+), 6 deletions(-) delete mode 100644 .golshrc create mode 100644 util/util.go diff --git a/.golshrc b/.golshrc deleted file mode 100644 index fdc83c2..0000000 --- a/.golshrc +++ /dev/null @@ -1 +0,0 @@ -apath ~/Documents/misc/ado-linker \ No newline at end of file diff --git a/cmd/cat.go b/cmd/cat.go index a1b91f8..9d380ec 100644 --- a/cmd/cat.go +++ b/cmd/cat.go @@ -2,13 +2,17 @@ package cmd import ( "gosh/types" + "gosh/util" "os" "github.com/fatih/color" ) -func GoshCat(sh *types.Shell, filename string) types.CmdOutput { - data, err := os.ReadFile(filename) +func GoshCat(sh *types.Shell, file_path string) types.CmdOutput { + + file_path = util.ExpandHome(file_path) + + data, err := os.ReadFile(file_path) red := color.New(color.FgRed).SprintFunc() if err != nil { return types.CmdOutput{Id: 1, Output: red(err)} diff --git a/cmd/cd.go b/cmd/cd.go index b8faa46..8c4ec04 100644 --- a/cmd/cd.go +++ b/cmd/cd.go @@ -4,11 +4,15 @@ import ( "os" "gosh/types" + "gosh/util" "github.com/fatih/color" ) func GoshCd(sh *types.Shell, dir string) types.CmdOutput { + + dir = util.ExpandHome(dir) + err := os.Chdir(dir) red := color.New(color.FgRed).SprintFunc() if err != nil { diff --git a/cmd/ls.go b/cmd/ls.go index 17dcd93..c9e9d17 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -2,14 +2,27 @@ package cmd import ( "gosh/types" + "gosh/util" "os" "strings" "github.com/fatih/color" ) -func GoshLs(sh *types.Shell) types.CmdOutput { - children, err := os.ReadDir(sh.Cd) +func GoshLs(sh *types.Shell, paths ...string) types.CmdOutput { + + var dir string + + if len(paths) > 0 && paths[0] != "" { + dir = paths[0] + } else { + dir = sh.Cd + } + + dir = util.ExpandHome(dir) + + children, err := os.ReadDir(dir) + red := color.New(color.FgRed).SprintFunc() if err != nil { return types.CmdOutput{Id: 1, Output: red(err)} diff --git a/cmd/pyapath.go b/cmd/pyapath.go index 7653b8e..a1ee205 100644 --- a/cmd/pyapath.go +++ b/cmd/pyapath.go @@ -2,12 +2,16 @@ package cmd import ( "gosh/types" + "gosh/util" "path/filepath" "github.com/fatih/color" ) func GolshPyapath(sh *types.Shell, path string) types.CmdOutput { + + path = util.ExpandHome(path) + abs, err := filepath.Abs(path) red := color.New(color.FgRed).SprintFunc() diff --git a/main.go b/main.go index 025bbc5..0901407 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,21 @@ func parse_flags(parts []string) (map[string]string, []string) { return flags, args } +func process_golshscript(sh *types.Shell, file_path string) { + data, err := os.ReadFile(file_path) + + if err != nil { + fmt.Println("Error: ", err) + return + } + + lines := strings.Split(string(data), "\n") + + for _, line := range lines { + gosh_process_input(sh, strings.TrimSpace(line)) + } +} + func check_pathes(sh *types.Shell, name string, args []string) bool { var output types.CmdOutput @@ -105,7 +120,11 @@ func gosh_process_input(sh *types.Shell, input string) { } prevOutput = cmd.GoshCd(sh, parts[1]) case "ls": - prevOutput = cmd.GoshLs(sh) + if len(parts) > 1 { + prevOutput = cmd.GoshLs(sh, parts[1:]...) + } else { + prevOutput = cmd.GoshLs(sh) + } case "cat": if len(parts) < 2 { fmt.Println("Usage: cat ") @@ -155,6 +174,19 @@ func main() { Cd: dir, PyPaths: []string{}} + // Check for and execute .golshrc + golshrc_path, _ := os.UserHomeDir() + golshrc_path = golshrc_path + "/.golshrc" + + _, err := os.Stat(golshrc_path) + + golshrc_exists := err == nil + fmt.Println(golshrc_exists) + if golshrc_exists { + fmt.Println("exists") + process_golshscript(&sh, golshrc_path) + } + var should_exit = false for { c.Print(sh.Cd) diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..0ca574f --- /dev/null +++ b/util/util.go @@ -0,0 +1,17 @@ +package util + +import ( + "os" + "strings" +) + +func ExpandHome(path string) string { + if strings.HasPrefix(path, "~") { + home, err := os.UserHomeDir() + if err == nil { + return strings.Replace(path, "~", home, 1) + } + } + + return path +}