Compare commits

...

9 Commits

Author SHA1 Message Date
sammy d7104ec591
refactor(minor): reduce amount of renamed modules to 0 now 2021-04-28 19:22:11 -04:00
sammy e0cf87d272
refactor(minor): reduce the amount of module renaming 2021-04-28 18:57:28 -04:00
sammy a29eddb1bf
style: remove extra spaces at map; dont make them lineup 2021-04-28 18:54:16 -04:00
sammy dc9bd1864c
chore: remove redundant module renaming 2021-04-28 18:52:42 -04:00
sammy d551d0f2ab
style: add spaces between binary ops and rename appendpath function to be camelCase 2021-04-28 18:35:04 -04:00
Devin Singh 345ff65da8
fix: remove appendpath from preload.lua 2021-04-28 17:13:43 -05:00
Devin Singh 786b95c266
feat: appendpath function 2021-04-28 17:09:10 -05:00
Devin Singh 4476a96eec
feat(wip): add appendpath 2021-04-28 16:57:06 -05:00
gokul swaminathan bf11feb48b
fix: makefile login shell file fix (#38)
This fixes the problem where installing kept adding hilbish to
`/etc/shells` even if it was already there.

* fix: makefile login shell file fix

* style: remove extra space in makefile
2021-04-24 19:32:41 -04:00
5 changed files with 41 additions and 29 deletions

View File

@ -10,7 +10,7 @@ install:
@install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish" @install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish"
@mkdir -p "$(DESTDIR)$(LIBDIR)" @mkdir -p "$(DESTDIR)$(LIBDIR)"
@cp libs preload.lua .hilbishrc.lua "$(DESTDIR)$(LIBDIR)" -r @cp libs preload.lua .hilbishrc.lua "$(DESTDIR)$(LIBDIR)" -r
@echo "$(DESTDIR)$(BINDIR)/hilbish" >> /etc/shells @grep "$(DESTDIR)$(BINDIR)/hilbish" -qxF /etc/shells || echo "$(DESTDIR)$(BINDIR)/hilbish" >> /etc/shells
@echo "Hilbish Installed" @echo "Hilbish Installed"
uninstall: uninstall:

View File

@ -4,27 +4,26 @@ import (
"os" "os"
"strings" "strings"
"github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
) )
func Loader(L *lua.LState) int { func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod) L.Push(mod)
return 1 return 1
} }
func LuaErr(L *lua.LState, code int) { func LuaErr(L *lua.LState, code int) {
// TODO: Error with a table, with path and error code // TODO: Error with a table, with path and error code
L.Error(lua.LNumber(code), 2) L.Error(lua.LNumber(code), 2)
} }
var exports = map[string]lua.LGFunction{ var exports = map[string]lua.LGFunction{
"cd": cd, "cd": cd,
"mkdir": mkdir, "mkdir": mkdir,
"stat": stat, "stat": stat,
} }
func cd(L *lua.LState) int { func cd(L *lua.LState) int {

31
lua.go
View File

@ -2,9 +2,9 @@ package main
import ( import (
"fmt" "fmt"
hooks "hilbish/golibs/bait" "hilbish/golibs/bait"
cmds "hilbish/golibs/commander" "hilbish/golibs/commander"
lfs "hilbish/golibs/fs" "hilbish/golibs/fs"
"os" "os"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
@ -27,13 +27,14 @@ func LuaInit(confpath string) {
l.SetGlobal("prompt", l.NewFunction(hshprompt)) l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt)) l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias)) l.SetGlobal("alias", l.NewFunction(hshalias))
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
// Add fs module to Lua // Add fs module to Lua
l.PreloadModule("fs", lfs.Loader) l.PreloadModule("fs", fs.Loader)
commander := cmds.New() cmds := commander.New()
// When a command from Lua is added, register it for use // When a command from Lua is added, register it for use
commander.Events.On("commandRegister", cmds.Events.On("commandRegister",
func(cmdName string, cmd *lua.LFunction) { func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = true commands[cmdName] = true
l.SetField( l.SetField(
@ -43,10 +44,10 @@ func LuaInit(confpath string) {
cmd) cmd)
}) })
l.PreloadModule("commander", commander.Loader) l.PreloadModule("commander", cmds.Loader)
bait = hooks.New() hooks = bait.New()
l.PreloadModule("bait", bait.Loader) l.PreloadModule("bait", hooks.Loader)
// Add more paths that Lua can require from // Add more paths that Lua can require from
l.DoString(`package.path = package.path l.DoString(`package.path = package.path
@ -67,7 +68,9 @@ func LuaInit(confpath string) {
} }
// Run config // Run config
if !interactive { return } if !interactive {
return
}
err = l.DoFile(confpath) err = l.DoFile(confpath)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err, fmt.Fprintln(os.Stderr, err,
@ -97,3 +100,11 @@ func hshalias(L *lua.LState) int {
return 1 return 1
} }
func hshappendPath(L *lua.LState) int {
path := L.ToString(1)
os.Setenv("PATH", os.Getenv("PATH") + ":" + path)
return 0
}

View File

@ -8,7 +8,7 @@ import (
"os/user" "os/user"
"strings" "strings"
hooks "hilbish/golibs/bait" "hilbish/golibs/bait"
"github.com/akamensky/argparse" "github.com/akamensky/argparse"
"github.com/bobappleyard/readline" "github.com/bobappleyard/readline"
@ -28,7 +28,7 @@ var (
commands = map[string]bool{} commands = map[string]bool{}
aliases = map[string]string{} aliases = map[string]string{}
bait hooks.Bait hooks bait.Bait
homedir string homedir string
running bool running bool
interactive bool interactive bool

View File

@ -8,8 +8,8 @@ import (
"os" "os"
"strings" "strings"
"github.com/yuin/gopher-lua"
"github.com/bobappleyard/readline" "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" "layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
@ -23,7 +23,7 @@ func RunInput(input string) {
// If it succeeds, add to history and prompt again // If it succeeds, add to history and prompt again
HandleHistory(input) HandleHistory(input)
bait.Em.Emit("command.exit", 0) hooks.Em.Emit("command.exit", 0)
return return
} }
@ -71,22 +71,22 @@ func RunInput(input string) {
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") { if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
continue continue
} else if code, ok := interp.IsExitStatus(err); ok { } else if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code) hooks.Em.Emit("command.exit", code)
} else if err != nil { } else if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
bait.Em.Emit("command.exit", 1) hooks.Em.Emit("command.exit", 1)
} }
break break
} }
} else { } else {
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code) hooks.Em.Emit("command.exit", code)
} else { } else {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
} }
} else { } else {
bait.Em.Emit("command.exit", 0) hooks.Em.Emit("command.exit", 0)
} }
HandleHistory(cmdString) HandleHistory(cmdString)
} }
@ -167,7 +167,9 @@ func HandleHistory(cmd string) {
func StartMultiline(prev string, sb *strings.Builder) bool { func StartMultiline(prev string, sb *strings.Builder) bool {
// sb fromt outside is passed so we can // sb fromt outside is passed so we can
// save input from previous prompts // save input from previous prompts
if sb.String() == "" { sb.WriteString(prev + " ") } if sb.String() == "" {
sb.WriteString(prev + " ")
}
fmt.Print(multilinePrompt) fmt.Print(multilinePrompt)