Compare commits

..

8 Commits

Author SHA1 Message Date
sammyette bc4e0f835f
fix: revert "fix: reuse sh runner"
This reverts commit 822b2876e9.
this broke a few things with the shell so probably shouldnt do this yet
2021-09-25 22:46:06 -04:00
sammyette c95b0b55be
fix: runtime errors on linux
this fixes out of bounds error since we tried to split the username
for '\' which is only valid on windows, so i added it for windows only
2021-09-25 21:39:06 -04:00
sammyette 7c815022a9
chore: merge from dev 2021-09-18 22:49:33 -04:00
sammyette 2ef2998279
fix: make timeout() blocking
may or may not be temporary
2021-09-18 22:48:28 -04:00
sammyette 196526cc6a fix: rename unix vars to linux 2021-07-08 17:55:19 -07:00
sammyette 5d78a0c574 feat: add compile vars for windows 2021-07-08 17:49:51 -07:00
sammyette a3cc8160d3 fix: trim off hostname in %u verb for windows 2021-07-08 03:44:57 -07:00
sammyette 4caeb7ec91 feat: replace ~ in preloadPath and sampleConfPath with homedir 2021-07-08 03:44:11 -07:00
7 changed files with 98 additions and 70 deletions

View File

@ -5,6 +5,8 @@ package main
import ( import (
"os" "os"
"runtime"
"strings"
"github.com/pborman/getopt" "github.com/pborman/getopt"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
@ -21,9 +23,14 @@ func HilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
host, _ := os.Hostname() host, _ := os.Hostname()
username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
}
L.SetField(mod, "ver", lua.LString(version)) L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(curuser.Username)) L.SetField(mod, "user", lua.LString(username))
L.SetField(mod, "host", lua.LString(host)) L.SetField(mod, "host", lua.LString(host))
L.SetField(mod, "home", lua.LString(homedir)) L.SetField(mod, "home", lua.LString(homedir))

3
lua.go
View File

@ -152,7 +152,8 @@ func hshgoroutine(gofunc func()) {
func hshtimeout(timeoutfunc func(), ms int) { func hshtimeout(timeoutfunc func(), ms int) {
timeout := time.Duration(ms) * time.Millisecond timeout := time.Duration(ms) * time.Millisecond
time.AfterFunc(timeout, timeoutfunc) time.Sleep(timeout)
timeoutfunc()
} }
func hshinterval(L *lua.LState) int { func hshinterval(L *lua.LState) int {

65
main.go
View File

@ -2,22 +2,20 @@ package main
import ( import (
"bufio" "bufio"
"context"
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"strings" "strings"
"os/signal" "os/signal"
"os/user" "os/user"
"path/filepath" "path/filepath"
"time"
"hilbish/golibs/bait" "hilbish/golibs/bait"
"github.com/pborman/getopt" "github.com/pborman/getopt"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" "layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"golang.org/x/term" "golang.org/x/term"
) )
@ -33,13 +31,13 @@ var (
hooks bait.Bait hooks bait.Bait
defaultConfPath string defaultConfPath string
runner *interp.Runner
) )
func main() { func main() {
homedir, _ = os.UserHomeDir() homedir, _ = os.UserHomeDir()
curuser, _ = user.Current() curuser, _ = user.Current()
preloadPath = strings.Replace(preloadPath, "~", homedir, 1)
sampleConfPath = strings.Replace(sampleConfPath, "~", homedir, 1)
if defaultConfDir == "" { if defaultConfDir == "" {
// we'll add *our* default if its empty (wont be if its changed comptime) // we'll add *our* default if its empty (wont be if its changed comptime)
@ -151,56 +149,6 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// Execute handler for sh runner
exechandle := func(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx)
_, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias
if aliases[args[0]] != "" {
alias := aliases[args[0]]
argstring = alias + strings.TrimPrefix(argstring, args[0])
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
}
// If command is defined in Lua then run it
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[1:]))
luaexitcode := l.Get(-1)
var exitcode uint8 = 0
l.Pop(1)
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
exitcode = uint8(code)
}
if err != nil {
fmt.Fprintln(os.Stderr,
"Error in command:\n\n" + err.Error())
}
hooks.Em.Emit("command.exit", exitcode)
return interp.NewExitStatus(exitcode)
}
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
hooks.Em.Emit("command.not-found", args[0])
return interp.NewExitStatus(127)
}
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
}
// Setup sh runner outside of input label
runner, _ = interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
interp.ExecHandler(exechandle),
)
input: input:
for interactive { for interactive {
running = false running = false
@ -263,12 +211,17 @@ func fmtPrompt() string {
cwd, _ := os.Getwd() cwd, _ := os.Getwd()
cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1) cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1)
username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
}
args := []string{ args := []string{
"d", cwd, "d", cwd,
"D", filepath.Base(cwd), "D", filepath.Base(cwd),
"h", host, "h", host,
"u", curuser.Username, "u", username,
} }
for i, v := range args { for i, v := range args {

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"strings" "strings"
"time"
// "github.com/bobappleyard/readline" // "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
@ -114,6 +115,53 @@ func execCommand(cmd string) error {
return err return err
} }
exechandle := func(ctx context.Context, args []string) error {
hc := interp.HandlerCtx(ctx)
_, argstring := splitInput(strings.Join(args, " "))
// If alias was found, use command alias
if aliases[args[0]] != "" {
alias := aliases[args[0]]
argstring = alias + strings.TrimPrefix(argstring, args[0])
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
}
// If command is defined in Lua then run it
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[1:]))
luaexitcode := l.Get(-1)
var exitcode uint8 = 0
l.Pop(1)
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok {
exitcode = uint8(code)
}
if err != nil {
fmt.Fprintln(os.Stderr,
"Error in command:\n\n" + err.Error())
}
hooks.Em.Emit("command.exit", exitcode)
return interp.NewExitStatus(exitcode)
}
if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil {
hooks.Em.Emit("command.not-found", args[0])
return interp.NewExitStatus(127)
}
return interp.DefaultExecHandler(2 * time.Second)(ctx, args)
}
runner, _ := interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
interp.ExecHandler(exechandle),
)
err = runner.Run(context.TODO(), file) err = runner.Run(context.TODO(), file)
return err return err

11
vars.go
View File

@ -3,18 +3,7 @@ package main
// String vars that are free to be changed at compile time // String vars that are free to be changed at compile time
var ( var (
version = "v0.5.1" version = "v0.5.1"
requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua'
.. ';/usr/share/hilbish/libs/?/init.lua;'
.. ';/usr/share/hilbish/libs/?/?.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?/init.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?/?.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?.lua'
.. hilbish.home .. '/.config/hilbish/?/init.lua'
.. hilbish.home .. '/.config/hilbish/?/?.lua'
.. hilbish.home .. '/.config/hilbish/?.lua'`
preloadPath = "/usr/share/hilbish/preload.lua"
defaultConfDir = "" // ~ will be substituted for home, path for user's default config defaultConfDir = "" // ~ will be substituted for home, path for user's default config
sampleConfPath = "/usr/share/hilbish/.hilbishrc.lua" // Path to default/sample config
prompt string // Prompt will always get changed anyway prompt string // Prompt will always get changed anyway
multilinePrompt = "> " multilinePrompt = "> "

18
vars_linux.go 100644
View File

@ -0,0 +1,18 @@
// +build linux
package main
// String vars that are free to be changed at compile time
var (
requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua'
.. ';/usr/share/hilbish/libs/?/init.lua;'
.. ';/usr/share/hilbish/libs/?/?.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?/init.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?/?.lua;'
.. hilbish.home .. '/.local/share/hilbish/libs/?.lua'
.. hilbish.home .. '/.config/hilbish/?/init.lua'
.. hilbish.home .. '/.config/hilbish/?/?.lua'
.. hilbish.home .. '/.config/hilbish/?.lua'`
preloadPath = "/usr/share/hilbish/preload.lua"
sampleConfPath = "/usr/share/hilbish/.hilbishrc.lua" // Path to default/sample config
)

12
vars_windows.go 100644
View File

@ -0,0 +1,12 @@
// +build windows
package main
// String vars that are free to be changed at compile time
var (
requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua'
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;'
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'`
preloadPath = "~\\Appdata\\Roaming\\Hilbish\\preload.lua" // ~ and \ gonna cry?
sampleConfPath = "~\\Appdata\\Roaming\\Hilbish\\hilbishrc.lua" // Path to default/sample config
)