Compare commits

..

4 Commits

Author SHA1 Message Date
TorchedSammy 09a4cb4787 chore: update version 2021-03-30 19:50:35 -04:00
TorchedSammy c5a58b1d01 chore: update todo 2021-03-30 19:50:02 -04:00
TorchedSammy cb6f17c8fa details: make triangle in prompt change color based on command result 2021-03-30 19:48:37 -04:00
TorchedSammy f20be5153f feat: add hooks/events 2021-03-30 19:47:02 -04:00
4 changed files with 61 additions and 23 deletions

View File

@ -1,8 +1,21 @@
-- Default Hilbish config -- Default Hilbish config
ansikit = require 'ansikit' ansikit = require 'ansikit'
bait = require 'bait'
prompt(ansikit.text( function doPrompt(fail)
'{blue}%u {cyan}%d {green}∆{reset} ' prompt(ansikit.text(
)) '{blue}%u {cyan}%d ' .. (fail and '{red}' or '{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
- [ ] Hooks - [x] Hooks
- [x] Aliases - [x] Aliases

View File

@ -3,20 +3,33 @@ 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 {
var exports = map[string]lua.LGFunction{} mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{})
mod := L.SetFuncs(L.NewTable(), exports) L.SetField(mod, "throw", luar.New(L, b.throw))
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)
}

42
main.go
View File

@ -12,6 +12,7 @@ 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"
@ -22,7 +23,7 @@ import (
) )
const version = "0.2.0-dev" const version = "0.2.0"
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
@ -30,6 +31,7 @@ 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")
@ -58,21 +60,21 @@ 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]) }
// Read config from current directory
// (this is assuming the current dir is Hilbish's git)
input, err := os.ReadFile(".hilbishrc.lua")
if err != nil {
// If it wasnt found, go to "real default"
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
if err != nil {
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
return
}
}
homedir, _ := os.UserHomeDir() homedir, _ := os.UserHomeDir()
// If user's config doesn't exixt, // If user's config doesn't exixt,
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) { 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)
input, err := os.ReadFile(".hilbishrc.lua")
if err != nil {
// If it wasnt found, go to "real default"
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
if err != nil {
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
return
}
}
// 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 {
@ -159,8 +161,15 @@ 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)
} }
} }
} }
@ -281,9 +290,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),
) )
runner.Run(context.TODO(), file) err = runner.Run(context.TODO(), file)
return nil return err
} }
// do i even have to say // do i even have to say
@ -321,6 +330,9 @@ 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'")