From 94f0ccf9f6c2118ededab7605bf976af83b045ec Mon Sep 17 00:00:00 2001 From: Devin Singh Date: Sun, 18 Apr 2021 21:09:27 -0500 Subject: [PATCH 1/3] chore: formatting --- lua.go | 26 +++++++++++++------------- main.go | 46 ++++++++++++++++++++++++++++------------------ shell.go | 27 +++++++++++++++++---------- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/lua.go b/lua.go index eca357b..9b31d6d 100644 --- a/lua.go +++ b/lua.go @@ -2,10 +2,10 @@ package main import ( "fmt" - "os" - lfs "hilbish/golibs/fs" - cmds "hilbish/golibs/commander" hooks "hilbish/golibs/bait" + cmds "hilbish/golibs/commander" + lfs "hilbish/golibs/fs" + "os" "github.com/yuin/gopher-lua" ) @@ -34,14 +34,14 @@ func LuaInit(confpath string) { commander := cmds.New() // When a command from Lua is added, register it for use commander.Events.On("commandRegister", - func (cmdName string, cmd *lua.LFunction) { - commands[cmdName] = true - l.SetField( - l.GetTable(l.GetGlobal("commanding"), - lua.LString("__commands")), - cmdName, - cmd) - }) + func(cmdName string, cmd *lua.LFunction) { + commands[cmdName] = true + l.SetField( + l.GetTable(l.GetGlobal("commanding"), + lua.LString("__commands")), + cmdName, + cmd) + }) l.PreloadModule("commander", commander.Loader) @@ -62,7 +62,7 @@ func LuaInit(confpath string) { err = l.DoFile("preload.lua") if err != nil { fmt.Fprintln(os.Stderr, - "Missing preload file, builtins may be missing.") + "Missing preload file, builtins may be missing.") } } @@ -70,7 +70,7 @@ func LuaInit(confpath string) { err = l.DoFile(confpath) if err != nil { fmt.Fprintln(os.Stderr, err, - "\nAn error has occured while loading your config! Falling back to minimal default config.\n") + "\nAn error has occured while loading your config! Falling back to minimal default config.\n") l.DoString(minimalconf) } diff --git a/main.go b/main.go index 6f40126..66d0181 100644 --- a/main.go +++ b/main.go @@ -2,27 +2,30 @@ package main import ( "fmt" - "os" - "os/user" - "os/signal" - "strings" - "io" hooks "hilbish/golibs/bait" + "io" + "os" + "os/signal" + "os/user" + "strings" "github.com/akamensky/argparse" "github.com/bobappleyard/readline" - "github.com/yuin/gopher-lua" + lua "github.com/yuin/gopher-lua" "golang.org/x/term" ) const version = "0.3.2" + var l *lua.LState + // User's prompt, this will get set when lua side is initialized var prompt string var multilinePrompt = "> " // Map of builtin/custom commands defined in the commander lua module var commands = map[string]bool{} + // Command aliases var aliases = map[string]string{} var bait hooks.Bait @@ -36,16 +39,16 @@ func main() { parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") verflag := parser.Flag("v", "version", &argparse.Options{ Required: false, - Help: "Prints Hilbish version", + Help: "Prints Hilbish version", }) setshflag := parser.Flag("S", "set-shell-env", &argparse.Options{ Required: false, - Help: "Sets $SHELL to Hilbish's executed path", + Help: "Sets $SHELL to Hilbish's executed path", }) configflag := parser.String("C", "config", &argparse.Options{ Required: false, - Help: "Sets the path to Hilbish's config", - Default: defaultconfpath, + Help: "Sets the path to Hilbish's config", + Default: defaultconfpath, }) err := parser.Parse(os.Args) @@ -62,7 +65,9 @@ func main() { } // Set $SHELL if the user wants to - if *setshflag { os.Setenv("SHELL", os.Args[0]) } + if *setshflag { + os.Setenv("SHELL", os.Args[0]) + } // If user's config doesn't exixt, if _, err := os.Stat(defaultconfpath); os.IsNotExist(err) { @@ -79,7 +84,7 @@ func main() { } // Create it using either default config we found - err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644) + err = os.WriteFile(homedir+"/.hilbishrc.lua", input, 0644) if err != nil { // If that fails, bail fmt.Println("Error creating config file") @@ -108,21 +113,27 @@ func main() { } input = strings.TrimSpace(input) - if len(input) == 0 { continue } + if len(input) == 0 { + continue + } if strings.HasSuffix(input, "\\") { for { input, err = ContinuePrompt(strings.TrimSuffix(input, "\\")) - if err != nil || !strings.HasSuffix(input, "\\") { break } + if err != nil || !strings.HasSuffix(input, "\\") { + break + } } } running = true RunInput(input) termwidth, _, err := term.GetSize(0) - if err != nil { continue } - fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r") + if err != nil { + continue + } + fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth-1) + "\r") } } @@ -152,7 +163,7 @@ func fmtPrompt() string { } for i, v := range args { - if i % 2 == 0 { + if i%2 == 0 { args[i] = "%" + v } } @@ -175,4 +186,3 @@ func HandleSignals() { } } } - diff --git a/shell.go b/shell.go index 44e2bce..e1ce7e6 100644 --- a/shell.go +++ b/shell.go @@ -1,11 +1,11 @@ package main import ( - "fmt" - "os" "bufio" "context" + "fmt" "io" + "os" "strings" "github.com/bobappleyard/readline" @@ -13,7 +13,6 @@ import ( "layeh.com/gopher-luar" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" - ) func RunInput(input string) { @@ -43,14 +42,16 @@ func RunInput(input string) { l.GetGlobal("commanding"), lua.LString("__commands")), cmdArgs[0]), - NRet: 0, + NRet: 0, Protect: true, }, luar.New(l, cmdArgs[1:])) if err != nil { fmt.Fprintln(os.Stderr, - "Error in command:\n\n" + err.Error()) + "Error in command:\n\n"+err.Error()) + } + if cmdArgs[0] != "exit" { + HandleHistory(cmdString) } - if cmdArgs[0] != "exit" { HandleHistory(cmdString) } return } @@ -61,9 +62,11 @@ func RunInput(input string) { if syntax.IsIncomplete(err) { for { cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\")) - if err != nil { break } + if err != nil { + break + } err = execCommand(cmdString) - if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") { + if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") { continue } else if code, ok := interp.IsExitStatus(err); ok { bait.Em.Emit("command.exit", code) @@ -76,7 +79,9 @@ func RunInput(input string) { } else { if code, ok := interp.IsExitStatus(err); ok { bait.Em.Emit("command.exit", code) - } else { fmt.Fprintln(os.Stderr, err) } + } else { + fmt.Fprintln(os.Stderr, err) + } } } else { bait.Em.Emit("command.exit", 0) @@ -160,7 +165,9 @@ func HandleHistory(cmd string) { func StartMultiline(prev string, sb *strings.Builder) bool { // sb fromt outside is passed so we can // save input from previous prompts - if sb.String() == "" { sb.WriteString(prev + "\n") } + if sb.String() == "" { + sb.WriteString(prev + "\n") + } fmt.Print(multilinePrompt) From 428db24831cbe637bebbc7f07ef9187d2a354791 Mon Sep 17 00:00:00 2001 From: sammy <38820196+TorchedSammy@users.noreply.github.com> Date: Tue, 20 Apr 2021 17:51:12 -0400 Subject: [PATCH 2/3] style: put all vars in 1 var() --- main.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 66d0181..404dfad 100644 --- a/main.go +++ b/main.go @@ -2,35 +2,36 @@ package main import ( "fmt" - hooks "hilbish/golibs/bait" "io" "os" "os/signal" "os/user" "strings" + hooks "hilbish/golibs/bait" + "github.com/akamensky/argparse" "github.com/bobappleyard/readline" - lua "github.com/yuin/gopher-lua" + "github.com/yuin/gopher-lua" "golang.org/x/term" ) const version = "0.3.2" -var l *lua.LState +var ( + l *lua.LState -// User's prompt, this will get set when lua side is initialized -var prompt string -var multilinePrompt = "> " + // User's prompt, this will get set when lua side is initialized + prompt string + multilinePrompt = "> " -// Map of builtin/custom commands defined in the commander lua module -var commands = map[string]bool{} + commands = map[string]bool{} + aliases = map[string]string{} -// Command aliases -var aliases = map[string]string{} -var bait hooks.Bait -var homedir string -var running bool + bait hooks.Bait + homedir string + running bool +) func main() { homedir, _ = os.UserHomeDir() From 08c56a54ca10fba5bac178e1c0d2051253888df9 Mon Sep 17 00:00:00 2001 From: sammy <38820196+TorchedSammy@users.noreply.github.com> Date: Tue, 20 Apr 2021 17:52:01 -0400 Subject: [PATCH 3/3] style: formatting --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 404dfad..f20d521 100644 --- a/main.go +++ b/main.go @@ -40,15 +40,15 @@ func main() { parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") verflag := parser.Flag("v", "version", &argparse.Options{ Required: false, - Help: "Prints Hilbish version", + Help: "Prints Hilbish version", }) setshflag := parser.Flag("S", "set-shell-env", &argparse.Options{ Required: false, - Help: "Sets $SHELL to Hilbish's executed path", + Help: "Sets $SHELL to Hilbish's executed path", }) configflag := parser.String("C", "config", &argparse.Options{ Required: false, - Help: "Sets the path to Hilbish's config", + Help: "Sets the path to Hilbish's config", Default: defaultconfpath, }) @@ -164,7 +164,7 @@ func fmtPrompt() string { } for i, v := range args { - if i%2 == 0 { + if i % 2 == 0 { args[i] = "%" + v } }