Compare commits

..

No commits in common. "09a4cb478758b7b591f171818aa2f1d426f8ed15" and "352c5f73a8f461d99b20456126ab2502dc798789" have entirely different histories.

4 changed files with 23 additions and 61 deletions

View File

@ -1,21 +1,8 @@
-- Default Hilbish config -- Default Hilbish config
ansikit = require 'ansikit' ansikit = require 'ansikit'
bait = require 'bait'
function doPrompt(fail)
prompt(ansikit.text( prompt(ansikit.text(
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆{reset} ' '{blue}%u {cyan}%d {green}∆{reset} '
)) ))
end
doPrompt()
bait.catch('command.fail', function()
doPrompt(true)
end)
bait.catch('command.success', function()
doPrompt()
end)
--hook("tab complete", function ()) --hook("tab complete", function ())

View File

@ -4,5 +4,5 @@
- [x] Custom commands via the shell - [x] Custom commands via the shell
- [ ] Confirmed windows support - [ ] Confirmed windows support
- [ ] Readme badges - [ ] Readme badges
- [x] Hooks - [ ] Hooks
- [x] Aliases - [x] Aliases

View File

@ -3,33 +3,20 @@ package bait
import ( import (
"github.com/chuckpreslar/emission" "github.com/chuckpreslar/emission"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
) )
type Bait struct{ type Bait struct{}
Em *emission.Emitter
}
func New() Bait { func New() Bait {
return Bait{ return Bait{}
Em: emission.NewEmitter(),
}
} }
func (b *Bait) Loader(L *lua.LState) int { func (b *Bait) Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{}) var exports = map[string]lua.LGFunction{}
L.SetField(mod, "throw", luar.New(L, b.throw)) mod := L.SetFuncs(L.NewTable(), exports)
L.SetField(mod, "catch", luar.New(L, b.catch))
L.Push(mod) L.Push(mod)
return 1 return 1
} }
func (b *Bait) throw(name string, args ...interface{}) {
b.Em.Emit(name, args...)
}
func (b *Bait) catch(name string, catcher func(interface{})) {
b.Em.On(name, catcher)
}

26
main.go
View File

@ -12,7 +12,6 @@ import (
"context" "context"
lfs "hilbish/golibs/fs" lfs "hilbish/golibs/fs"
cmds "hilbish/golibs/commander" cmds "hilbish/golibs/commander"
hooks "hilbish/golibs/bait"
"github.com/akamensky/argparse" "github.com/akamensky/argparse"
"github.com/bobappleyard/readline" "github.com/bobappleyard/readline"
@ -23,7 +22,7 @@ import (
) )
const version = "0.2.0" const version = "0.2.0-dev"
var l *lua.LState var l *lua.LState
// User's prompt, this will get set when lua side is initialized // User's prompt, this will get set when lua side is initialized
var prompt string var prompt string
@ -31,7 +30,6 @@ var prompt string
var commands = map[string]bool{} var commands = map[string]bool{}
// Command aliases // Command aliases
var aliases = map[string]string{} var aliases = map[string]string{}
var bait hooks.Bait
func main() { func main() {
parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
@ -60,10 +58,7 @@ func main() {
// Set $SHELL if the user wants to // Set $SHELL if the user wants to
if *setshflag { os.Setenv("SHELL", os.Args[0]) } if *setshflag { os.Setenv("SHELL", os.Args[0]) }
homedir, _ := os.UserHomeDir() // Read config from current directory
// If user's config doesn't exixt,
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
// Read default from current directory
// (this is assuming the current dir is Hilbish's git) // (this is assuming the current dir is Hilbish's git)
input, err := os.ReadFile(".hilbishrc.lua") input, err := os.ReadFile(".hilbishrc.lua")
if err != nil { if err != nil {
@ -75,6 +70,9 @@ func main() {
} }
} }
homedir, _ := os.UserHomeDir()
// If user's config doesn't exixt,
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
// Create it using either default config we found // 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 err != nil {
@ -161,15 +159,8 @@ func main() {
} }
} }
} else { } else {
if code, ok := interp.IsExitStatus(err); ok {
if code > 0 {
bait.Em.Emit("command.fail", code)
}
}
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
} else {
bait.Em.Emit("command.success", nil)
} }
} }
} }
@ -290,9 +281,9 @@ func execCommand(cmd string) error {
runner, _ := interp.New( runner, _ := interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr), interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
) )
err = runner.Run(context.TODO(), file) runner.Run(context.TODO(), file)
return err return nil
} }
// do i even have to say // do i even have to say
@ -330,9 +321,6 @@ func LuaInit() {
l.PreloadModule("commander", commander.Loader) l.PreloadModule("commander", commander.Loader)
bait = hooks.New()
l.PreloadModule("bait", bait.Loader)
// Add more paths that Lua can require from // Add more paths that Lua can require from
l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'") l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")