chore: merge from master, resolve conflicts

pull/38/head
sammy 2021-04-20 19:31:19 -04:00
commit 17e12c8c2a
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
3 changed files with 58 additions and 44 deletions

26
lua.go
View File

@ -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)
}

53
main.go
View File

@ -2,11 +2,12 @@ package main
import (
"fmt"
"os"
"os/user"
"os/signal"
"strings"
"io"
"os"
"os/signal"
"os/user"
"strings"
hooks "hilbish/golibs/bait"
"github.com/akamensky/argparse"
@ -16,18 +17,21 @@ import (
)
const version = "0.4.0-dev.2+hilbiline"
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
var homedir string
var running bool
var (
l *lua.LState
// User's prompt, this will get set when lua side is initialized
prompt string
multilinePrompt = "> "
commands = map[string]bool{}
aliases = map[string]string{}
bait hooks.Bait
homedir string
running bool
)
func main() {
homedir, _ = os.UserHomeDir()
@ -45,7 +49,7 @@ func main() {
configflag := parser.String("C", "config", &argparse.Options{
Required: false,
Help: "Sets the path to Hilbish's config",
Default: defaultconfpath,
Default: defaultconfpath,
})
// loginshflag
// TODO: issue #37
@ -69,7 +73,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) {
@ -86,7 +92,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")
@ -125,15 +131,19 @@ func main() {
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")
}
}
@ -188,4 +198,3 @@ func HandleSignals() {
}
}
}

View File

@ -1,18 +1,17 @@
package main
import (
"fmt"
"os"
"bufio"
"context"
"fmt"
"io"
"os"
"strings"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
)
func RunInput(input string) {
@ -44,14 +43,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
}
@ -62,9 +63,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)
@ -77,7 +80,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)