mirror of https://github.com/Hilbis/Hilbish
Compare commits
8 Commits
0850247615
...
1ff3ff854b
Author | SHA1 | Date |
---|---|---|
sammyette | 1ff3ff854b | |
sammyette | 12828d197a | |
sammyette | 4b2891b503 | |
sammyette | ef1ced4a04 | |
sammyette | c6aa5fa387 | |
sammyette | 88e117974e | |
sammyette | e29ba6a0d5 | |
sammyette | 9415c5193e |
|
@ -8,8 +8,8 @@ function doPrompt(fail)
|
||||||
))
|
))
|
||||||
end
|
end
|
||||||
|
|
||||||
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. _user ..
|
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. hilbish.user
|
||||||
'{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
|
.. '{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
|
||||||
|
|
||||||
doPrompt()
|
doPrompt()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
// Here is the core api for the hilbi shell itself
|
||||||
|
// Basically, stuff about the shell itself and other functions
|
||||||
|
// go here.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pborman/getopt"
|
||||||
|
"github.com/yuin/gopher-lua"
|
||||||
|
"mvdan.cc/sh/v3/interp"
|
||||||
|
)
|
||||||
|
|
||||||
|
var exports = map[string]lua.LGFunction {
|
||||||
|
"run": run,
|
||||||
|
"flag": flag,
|
||||||
|
}
|
||||||
|
|
||||||
|
func HilbishLoader(L *lua.LState) int {
|
||||||
|
mod := L.SetFuncs(L.NewTable(), exports)
|
||||||
|
|
||||||
|
host, _ := os.Hostname()
|
||||||
|
|
||||||
|
L.SetField(mod, "ver", lua.LString(version))
|
||||||
|
L.SetField(mod, "user", lua.LString(curuser.Username))
|
||||||
|
L.SetField(mod, "host", lua.LString(host))
|
||||||
|
|
||||||
|
L.Push(mod)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Runs a command
|
||||||
|
func run(L *lua.LState) int {
|
||||||
|
var exitcode uint8 = 0
|
||||||
|
cmd := L.CheckString(1)
|
||||||
|
err := execCommand(cmd)
|
||||||
|
|
||||||
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
|
exitcode = code
|
||||||
|
} else if err != nil {
|
||||||
|
exitcode = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
L.Push(lua.LNumber(exitcode))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func flag(L *lua.LState) int {
|
||||||
|
flagchar := L.CheckString(1)
|
||||||
|
|
||||||
|
L.Push(lua.LBool(getopt.Lookup([]rune(flagchar)[0]).Seen()))
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
11
lua.go
11
lua.go
|
@ -23,18 +23,18 @@ prompt(ansikit.format(
|
||||||
|
|
||||||
func LuaInit() {
|
func LuaInit() {
|
||||||
l = lua.NewState()
|
l = lua.NewState()
|
||||||
|
|
||||||
l.OpenLibs()
|
l.OpenLibs()
|
||||||
|
|
||||||
l.SetGlobal("_ver", lua.LString(version))
|
|
||||||
l.SetGlobal("_user", lua.LString(curuser.Username))
|
|
||||||
|
|
||||||
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))
|
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
|
||||||
l.SetGlobal("exec", l.NewFunction(hshexec))
|
l.SetGlobal("exec", l.NewFunction(hshexec))
|
||||||
|
|
||||||
|
// yes this is stupid, i know
|
||||||
|
l.PreloadModule("hilbish", HilbishLoader)
|
||||||
|
l.DoString("hilbish = require 'hilbish'")
|
||||||
|
|
||||||
// Add fs module to Lua
|
// Add fs module to Lua
|
||||||
l.PreloadModule("fs", fs.Loader)
|
l.PreloadModule("fs", fs.Loader)
|
||||||
|
|
||||||
|
@ -147,6 +147,7 @@ func hshexec(L *lua.LState) int {
|
||||||
|
|
||||||
// syscall.Exec requires an absolute path to a binary
|
// syscall.Exec requires an absolute path to a binary
|
||||||
// path, args, string slice of environments
|
// path, args, string slice of environments
|
||||||
|
// TODO: alternative for windows
|
||||||
syscall.Exec(cmdPath, cmdArgs, os.Environ())
|
syscall.Exec(cmdPath, cmdArgs, os.Environ())
|
||||||
return 0
|
return 0 // random thought: does this ever return?
|
||||||
}
|
}
|
||||||
|
|
10
main.go
10
main.go
|
@ -18,9 +18,9 @@ import (
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "v0.4.0"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
version = "v0.4.0"
|
||||||
l *lua.LState
|
l *lua.LState
|
||||||
|
|
||||||
prompt string // User's prompt, this will get set when lua side is initialized
|
prompt string // User's prompt, this will get set when lua side is initialized
|
||||||
|
@ -158,6 +158,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
running = true
|
running = true
|
||||||
|
HandleHistory(input)
|
||||||
RunInput(input)
|
RunInput(input)
|
||||||
|
|
||||||
termwidth, _, err := term.GetSize(0)
|
termwidth, _, err := term.GetSize(0)
|
||||||
|
@ -218,3 +219,10 @@ func HandleSignals() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleHistory(cmd string) {
|
||||||
|
readline.AddHistory(cmd)
|
||||||
|
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||||
|
// TODO: load history again (history shared between sessions like this ye)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
19
shell.go
19
shell.go
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/bobappleyard/readline"
|
"github.com/bobappleyard/readline"
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
|
"github.com/yuin/gopher-lua/parse"
|
||||||
"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"
|
||||||
|
@ -31,6 +32,11 @@ func RunInput(input string) {
|
||||||
fn, err := l.LoadString(cmdString)
|
fn, err := l.LoadString(cmdString)
|
||||||
if err != nil && noexecute {
|
if err != nil && noexecute {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
if lerr, ok := err.(*lua.ApiError); ok {
|
||||||
|
if perr, ok := lerr.Cause.(*parse.Error); ok {
|
||||||
|
print(perr.Pos.Line == parse.EOF)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// And if there's no syntax errors and -n isnt provided, run
|
// And if there's no syntax errors and -n isnt provided, run
|
||||||
|
@ -39,9 +45,6 @@ func RunInput(input string) {
|
||||||
err = l.PCall(0, lua.MultRet, nil)
|
err = l.PCall(0, lua.MultRet, nil)
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// If it succeeds, add to history and prompt again
|
|
||||||
HandleHistory(input)
|
|
||||||
|
|
||||||
hooks.Em.Emit("command.exit", 0)
|
hooks.Em.Emit("command.exit", 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -69,9 +72,6 @@ func RunInput(input string) {
|
||||||
fmt.Fprintln(os.Stderr,
|
fmt.Fprintln(os.Stderr,
|
||||||
"Error in command:\n\n" + err.Error())
|
"Error in command:\n\n" + err.Error())
|
||||||
}
|
}
|
||||||
if cmdArgs[0] != "exit" {
|
|
||||||
HandleHistory(cmdString)
|
|
||||||
}
|
|
||||||
hooks.Em.Emit("command.exit", exitcode)
|
hooks.Em.Emit("command.exit", exitcode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,6 @@ func RunInput(input string) {
|
||||||
} else {
|
} else {
|
||||||
hooks.Em.Emit("command.exit", 0)
|
hooks.Em.Emit("command.exit", 0)
|
||||||
}
|
}
|
||||||
HandleHistory(cmdString)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
// Run command in sh interpreter
|
||||||
|
@ -177,9 +176,3 @@ func splitInput(input string) ([]string, string) {
|
||||||
return cmdArgs, cmdstr.String()
|
return cmdArgs, cmdstr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleHistory(cmd string) {
|
|
||||||
readline.AddHistory(cmd)
|
|
||||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
|
||||||
// TODO: load history again (history shared between sessions like this ye)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue