mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "09a4cb478758b7b591f171818aa2f1d426f8ed15" and "795a7d7fca020a76383ac44547c8069401fd4286" have entirely different histories.
09a4cb4787
...
795a7d7fca
|
@ -1,21 +1,6 @@
|
||||||
-- Default Hilbish config
|
-- Default Hilbish config
|
||||||
ansikit = require 'ansikit'
|
ansikit = require 'ansikit'
|
||||||
bait = require 'bait'
|
|
||||||
|
|
||||||
function doPrompt(fail)
|
prompt(ansikit.text('λ {bold}{cyan}'..os.getenv('USER')..' >{magenta}>{cyan}>{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 ())
|
||||||
|
|
16
TODO.md
16
TODO.md
|
@ -1,8 +1,8 @@
|
||||||
- [x] Configuration
|
[x] Configuration
|
||||||
- [x] Make default config if missing
|
[x] Make default config if missing
|
||||||
- [x] Tab completion
|
[x] Tab completion
|
||||||
- [x] Custom commands via the shell
|
[x] Custom commands via the shell
|
||||||
- [ ] Confirmed windows support
|
[ ] Confirmed windows support
|
||||||
- [ ] Readme badges
|
[ ] Readme badges
|
||||||
- [x] Hooks
|
[ ] Hooks
|
||||||
- [x] Aliases
|
[x] Aliases
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
package bait
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/chuckpreslar/emission"
|
|
||||||
"github.com/yuin/gopher-lua"
|
|
||||||
"layeh.com/gopher-luar"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Bait struct{
|
|
||||||
Em *emission.Emitter
|
|
||||||
}
|
|
||||||
|
|
||||||
func New() Bait {
|
|
||||||
return Bait{
|
|
||||||
Em: emission.NewEmitter(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bait) Loader(L *lua.LState) int {
|
|
||||||
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{})
|
|
||||||
L.SetField(mod, "throw", luar.New(L, b.throw))
|
|
||||||
L.SetField(mod, "catch", luar.New(L, b.catch))
|
|
||||||
|
|
||||||
L.Push(mod)
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
139
main.go
139
main.go
|
@ -4,7 +4,8 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
_ "os/exec"
|
||||||
|
_ "os/user"
|
||||||
"syscall"
|
"syscall"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,7 +13,6 @@ 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"
|
||||||
|
@ -23,31 +23,21 @@ import (
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "0.2.0"
|
const version = "0.1.2"
|
||||||
var l *lua.LState
|
var l *lua.LState
|
||||||
// User's prompt, this will get set when lua side is initialized
|
|
||||||
var prompt string
|
var prompt string
|
||||||
// Map of builtin/custom commands defined in the commander lua module
|
|
||||||
var commands = map[string]bool{}
|
var commands = map[string]bool{}
|
||||||
// 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")
|
||||||
verflag := parser.Flag("v", "version", &argparse.Options{
|
verflag := parser.Flag("v", "version", &argparse.Options{
|
||||||
Required: false,
|
Required: false,
|
||||||
Help: "Prints Hilbish version",
|
Help: "prints hilbish version",
|
||||||
})
|
|
||||||
setshflag := parser.Flag("S", "set-shell-env", &argparse.Options{
|
|
||||||
Required: false,
|
|
||||||
Help: "Sets $SHELL to Hilbish's executed path",
|
|
||||||
})
|
})
|
||||||
|
|
||||||
err := parser.Parse(os.Args)
|
err := parser.Parse(os.Args)
|
||||||
// If invalid flags or --help/-h,
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Print usage
|
|
||||||
fmt.Print(parser.Usage(err))
|
fmt.Print(parser.Usage(err))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
@ -57,17 +47,10 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set $SHELL if the user wants to
|
os.Setenv("SHELL", os.Args[0])
|
||||||
if *setshflag { os.Setenv("SHELL", os.Args[0]) }
|
|
||||||
|
|
||||||
homedir, _ := os.UserHomeDir()
|
|
||||||
// If user's config doesn't exixt,
|
|
||||||
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")
|
input, err := os.ReadFile(".hilbishrc.lua")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If it wasnt found, go to "real default"
|
|
||||||
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
|
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
|
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
|
||||||
|
@ -75,10 +58,10 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create it using either default config we found
|
homedir, _ := os.UserHomeDir()
|
||||||
|
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
|
||||||
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If that fails, bail
|
|
||||||
fmt.Println("Error creating config file")
|
fmt.Println("Error creating config file")
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -89,43 +72,41 @@ func main() {
|
||||||
LuaInit()
|
LuaInit()
|
||||||
|
|
||||||
readline.Completer = readline.FilenameCompleter
|
readline.Completer = readline.FilenameCompleter
|
||||||
|
|
||||||
for {
|
for {
|
||||||
cmdString, err := readline.String(fmtPrompt())
|
//user, _ := user.Current()
|
||||||
|
//dir, _ := os.Getwd()
|
||||||
|
//host, _ := os.Hostname()
|
||||||
|
|
||||||
|
//reader := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
|
//fmt.Printf(prompt)
|
||||||
|
|
||||||
|
cmdString, err := readline.String(prompt)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
// Exit if user presses ^D (ctrl + d)
|
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we get a completely random error, print
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// I have no idea if we need this anymore
|
|
||||||
cmdString = strings.TrimSuffix(cmdString, "\n")
|
cmdString = strings.TrimSuffix(cmdString, "\n")
|
||||||
// First try to run user input in Lua
|
|
||||||
err = l.DoString(cmdString)
|
err = l.DoString(cmdString)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// If it succeeds, add to history and prompt again
|
|
||||||
readline.AddHistory(cmdString)
|
readline.AddHistory(cmdString)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split up the input
|
cmdArgs := splitInput(cmdString)
|
||||||
cmdArgs, cmdString := splitInput(cmdString)
|
|
||||||
// If there's actually no input, prompt again
|
|
||||||
if len(cmdArgs) == 0 { continue }
|
if len(cmdArgs) == 0 { continue }
|
||||||
|
|
||||||
// If alias was found, use command alias
|
|
||||||
if aliases[cmdArgs[0]] != "" {
|
if aliases[cmdArgs[0]] != "" {
|
||||||
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
||||||
|
//cmdArgs := splitInput(cmdString)
|
||||||
execCommand(cmdString)
|
execCommand(cmdString)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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{
|
||||||
Fn: l.GetField(
|
Fn: l.GetField(
|
||||||
|
@ -143,15 +124,12 @@ func main() {
|
||||||
readline.AddHistory(cmdString)
|
readline.AddHistory(cmdString)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Last option: use sh interpreter
|
|
||||||
switch cmdArgs[0] {
|
switch cmdArgs[0] {
|
||||||
case "exit":
|
case "exit":
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
default:
|
default:
|
||||||
err := execCommand(cmdString)
|
err := execCommand(cmdString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If input is incomplete, start multiline prompting
|
|
||||||
if syntax.IsIncomplete(err) {
|
if syntax.IsIncomplete(err) {
|
||||||
sb := &strings.Builder{}
|
sb := &strings.Builder{}
|
||||||
for {
|
for {
|
||||||
|
@ -161,49 +139,14 @@ 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This semi cursed function formats our prompt (obviously)
|
|
||||||
func fmtPrompt() string {
|
|
||||||
user, _ := user.Current()
|
|
||||||
host, _ := os.Hostname()
|
|
||||||
cwd, _ := os.Getwd()
|
|
||||||
|
|
||||||
cwd = strings.Replace(cwd, user.HomeDir, "~", 1)
|
|
||||||
|
|
||||||
args := []string{
|
|
||||||
"d", cwd,
|
|
||||||
"h", host,
|
|
||||||
"u", user.Name,
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, v := range args {
|
|
||||||
if i % 2 == 0 {
|
|
||||||
args[i] = "%" + v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r := strings.NewReplacer(args...)
|
|
||||||
nprompt := r.Replace(prompt)
|
|
||||||
|
|
||||||
return nprompt
|
|
||||||
}
|
|
||||||
|
|
||||||
func StartMultiline(prev string, sb *strings.Builder) bool {
|
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 + "\n") }
|
if sb.String() == "" { sb.WriteString(prev + "\n") }
|
||||||
|
|
||||||
fmt.Printf("... ")
|
fmt.Printf("... ")
|
||||||
|
@ -212,7 +155,6 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
|
||||||
|
|
||||||
cont, err := reader.ReadString('\n')
|
cont, err := reader.ReadString('\n')
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
// Exit when ^D
|
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -227,61 +169,33 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitInput(input string) ([]string, string) {
|
func splitInput(input string) []string {
|
||||||
// end my suffering
|
|
||||||
// TODO: refactor this garbage
|
|
||||||
quoted := false
|
quoted := false
|
||||||
startlastcmd := false
|
|
||||||
lastcmddone := false
|
|
||||||
cmdArgs := []string{}
|
cmdArgs := []string{}
|
||||||
sb := &strings.Builder{}
|
sb := &strings.Builder{}
|
||||||
cmdstr := &strings.Builder{}
|
|
||||||
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
|
|
||||||
|
|
||||||
for _, r := range input {
|
for _, r := range input {
|
||||||
if r == '"' {
|
if r == '"' {
|
||||||
// start quoted input
|
|
||||||
// this determines if other runes are replaced
|
|
||||||
quoted = !quoted
|
quoted = !quoted
|
||||||
// dont add back quotes
|
// dont add back quotes
|
||||||
//sb.WriteRune(r)
|
//sb.WriteRune(r)
|
||||||
} else if !quoted && r == '~' {
|
} else if !quoted && r == '~' {
|
||||||
// if not in quotes and ~ is found then make it $HOME
|
|
||||||
sb.WriteString(os.Getenv("HOME"))
|
sb.WriteString(os.Getenv("HOME"))
|
||||||
} else if !quoted && r == ' ' {
|
} else if !quoted && r == ' ' {
|
||||||
// if not quoted and there's a space then add to cmdargs
|
|
||||||
cmdArgs = append(cmdArgs, sb.String())
|
cmdArgs = append(cmdArgs, sb.String())
|
||||||
sb.Reset()
|
sb.Reset()
|
||||||
} else if !quoted && r == '^' && startlastcmd && !lastcmddone {
|
|
||||||
// if ^ is found, isnt in quotes and is
|
|
||||||
// the second occurence of the character and is
|
|
||||||
// the first time "^^" has been used
|
|
||||||
cmdstr.WriteString(lastcmd)
|
|
||||||
sb.WriteString(lastcmd)
|
|
||||||
|
|
||||||
startlastcmd = !startlastcmd
|
|
||||||
lastcmddone = !lastcmddone
|
|
||||||
|
|
||||||
continue
|
|
||||||
} else if !quoted && r == '^' && !lastcmddone {
|
|
||||||
// if ^ is found, isnt in quotes and is the
|
|
||||||
// first time of starting "^^"
|
|
||||||
startlastcmd = !startlastcmd
|
|
||||||
continue
|
|
||||||
} else {
|
} else {
|
||||||
sb.WriteRune(r)
|
sb.WriteRune(r)
|
||||||
}
|
}
|
||||||
cmdstr.WriteRune(r)
|
|
||||||
}
|
}
|
||||||
if sb.Len() > 0 {
|
if sb.Len() > 0 {
|
||||||
cmdArgs = append(cmdArgs, sb.String())
|
cmdArgs = append(cmdArgs, sb.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
readline.AddHistory(input)
|
readline.AddHistory(input)
|
||||||
return cmdArgs, cmdstr.String()
|
return cmdArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
|
||||||
func execCommand(cmd string) error {
|
func execCommand(cmd string) error {
|
||||||
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -290,12 +204,11 @@ 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),
|
||||||
)
|
)
|
||||||
err = runner.Run(context.TODO(), file)
|
runner.Run(context.TODO(), file)
|
||||||
|
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// do i even have to say
|
|
||||||
func HandleSignals() {
|
func HandleSignals() {
|
||||||
c := make(chan os.Signal)
|
c := make(chan os.Signal)
|
||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
|
@ -305,7 +218,6 @@ func HandleSignals() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func LuaInit() {
|
func LuaInit() {
|
||||||
// TODO: Move to lua.go
|
|
||||||
l = lua.NewState()
|
l = lua.NewState()
|
||||||
|
|
||||||
l.OpenLibs()
|
l.OpenLibs()
|
||||||
|
@ -313,11 +225,9 @@ func LuaInit() {
|
||||||
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
||||||
l.SetGlobal("alias", l.NewFunction(hshalias))
|
l.SetGlobal("alias", l.NewFunction(hshalias))
|
||||||
|
|
||||||
// Add fs module to Lua
|
|
||||||
l.PreloadModule("fs", lfs.Loader)
|
l.PreloadModule("fs", lfs.Loader)
|
||||||
|
|
||||||
commander := cmds.New()
|
commander := cmds.New()
|
||||||
// When a command from Lua is added, register it for use
|
|
||||||
commander.Events.On("commandRegister",
|
commander.Events.On("commandRegister",
|
||||||
func (cmdName string, cmd *lua.LFunction) {
|
func (cmdName string, cmd *lua.LFunction) {
|
||||||
commands[cmdName] = true
|
commands[cmdName] = true
|
||||||
|
@ -330,10 +240,6 @@ 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
|
|
||||||
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'")
|
||||||
|
|
||||||
err := l.DoFile("/usr/share/hilbish/preload.lua")
|
err := l.DoFile("/usr/share/hilbish/preload.lua")
|
||||||
|
@ -346,7 +252,6 @@ func LuaInit() {
|
||||||
}
|
}
|
||||||
|
|
||||||
homedir, _ := os.UserHomeDir()
|
homedir, _ := os.UserHomeDir()
|
||||||
// Run config
|
|
||||||
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
Loading…
Reference in New Issue