Compare commits

..

6 Commits

Author SHA1 Message Date
sammyette 0850247615
feat: recursively alias commands 2021-05-16 09:32:02 -04:00
sammyette 415e66ab9c
fix: trim prefix of alias instead of trim 2021-05-16 09:29:39 -04:00
sammyette ad14b98b1f
feat: lua code can now be aliased 2021-05-16 08:43:19 -04:00
sammyette 83591f9bba
fix: tryrun input from -c only if its provided 2021-05-16 08:42:20 -04:00
sammyette 83bcb35dcf
fix: remove unused imports 2021-05-16 06:58:48 -04:00
sammyette dfb8ec9a90
fix: remove unused function 2021-05-16 06:55:11 -04:00
2 changed files with 18 additions and 41 deletions

View File

@ -116,7 +116,10 @@ func main() {
readline.Completer = readline.FilenameCompleter readline.Completer = readline.FilenameCompleter
readline.LoadHistory(homedir + "/.hilbish-history") readline.LoadHistory(homedir + "/.hilbish-history")
if *cmdflag != "" {
RunInput(*cmdflag) RunInput(*cmdflag)
}
if getopt.NArgs() > 0 { if getopt.NArgs() > 0 {
l.SetGlobal("args", luar.New(l, getopt.Args())) l.SetGlobal("args", luar.New(l, getopt.Args()))
err := l.DoFile(getopt.Arg(0)) err := l.DoFile(getopt.Arg(0))

View File

@ -1,10 +1,8 @@
package main package main
import ( import (
"bufio"
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
@ -16,8 +14,21 @@ import (
) )
func RunInput(input string) { func RunInput(input string) {
cmdArgs, cmdString := splitInput(input)
// If alias was found, use command alias
for aliases[cmdArgs[0]] != "" {
alias := aliases[cmdArgs[0]]
cmdString = alias + strings.TrimPrefix(cmdString, cmdArgs[0])
cmdArgs, cmdString = splitInput(cmdString)
if aliases[cmdArgs[0]] != "" {
continue
}
}
// First try to load input, essentially compiling to bytecode // First try to load input, essentially compiling to bytecode
fn, err := l.LoadString(input) fn, err := l.LoadString(cmdString)
if err != nil && noexecute { if err != nil && noexecute {
fmt.Println(err) fmt.Println(err)
return return
@ -35,15 +46,6 @@ func RunInput(input string) {
return return
} }
cmdArgs, cmdString := splitInput(input)
// If alias was found, use command alias
if aliases[cmdArgs[0]] != "" {
alias := aliases[cmdArgs[0]]
cmdString = alias + strings.Trim(cmdString, cmdArgs[0])
cmdArgs, cmdString = splitInput(cmdString)
}
// If command is defined in Lua then run it // If command is defined in Lua then run it
if commands[cmdArgs[0]] { if commands[cmdArgs[0]] {
err := l.CallByParam(lua.P{ err := l.CallByParam(lua.P{
@ -181,31 +183,3 @@ func HandleHistory(cmd string) {
// TODO: load history again (history shared between sessions like this ye) // TODO: load history again (history shared between sessions like this ye)
} }
func StartMultiline(prev string, sb *strings.Builder) bool {
// sb fromt outside is passed so we can
// save input from previous prompts
if sb.String() == "" {
sb.WriteString(prev + " ")
}
fmt.Print(multilinePrompt)
reader := bufio.NewReader(os.Stdin)
cont, err := reader.ReadString('\n')
cont = strings.TrimSuffix(cont, "\n") + " "
if err == io.EOF {
// Exit when ^D
fmt.Println("")
return true
}
sb.WriteString(cont)
err = execCommand(sb.String())
if err != nil && syntax.IsIncomplete(err) {
return false
}
return true
}