2021-03-19 19:11:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-19 02:09:27 +00:00
|
|
|
"io"
|
2021-03-19 19:11:59 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-04-19 02:09:27 +00:00
|
|
|
"os/user"
|
2021-03-19 19:11:59 +00:00
|
|
|
"strings"
|
2021-03-20 04:01:24 +00:00
|
|
|
|
2021-03-30 23:47:02 +00:00
|
|
|
hooks "hilbish/golibs/bait"
|
2021-03-20 04:01:24 +00:00
|
|
|
|
|
|
|
"github.com/akamensky/argparse"
|
2021-04-14 23:55:03 +00:00
|
|
|
"github.com/Hilbis/Hilbiline"
|
2021-03-19 23:03:11 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
2021-04-07 01:12:19 +00:00
|
|
|
"golang.org/x/term"
|
2021-03-19 19:11:59 +00:00
|
|
|
)
|
|
|
|
|
2021-04-20 02:08:16 +00:00
|
|
|
const version = "0.4.0-dev.2+hilbiline"
|
2021-04-19 02:09:27 +00:00
|
|
|
|
2021-04-20 21:51:12 +00:00
|
|
|
var (
|
|
|
|
l *lua.LState
|
2021-04-19 02:09:27 +00:00
|
|
|
|
2021-04-20 21:51:12 +00:00
|
|
|
// User's prompt, this will get set when lua side is initialized
|
|
|
|
prompt string
|
|
|
|
multilinePrompt = "> "
|
2021-04-05 22:07:47 +00:00
|
|
|
|
2021-04-20 21:51:12 +00:00
|
|
|
commands = map[string]bool{}
|
|
|
|
aliases = map[string]string{}
|
2021-04-19 02:09:27 +00:00
|
|
|
|
2021-04-20 21:51:12 +00:00
|
|
|
bait hooks.Bait
|
|
|
|
homedir string
|
|
|
|
running bool
|
|
|
|
)
|
2021-03-19 23:03:11 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
func main() {
|
2021-04-07 12:40:40 +00:00
|
|
|
homedir, _ = os.UserHomeDir()
|
|
|
|
defaultconfpath := homedir + "/.hilbishrc.lua"
|
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
|
|
|
|
verflag := parser.Flag("v", "version", &argparse.Options{
|
|
|
|
Required: false,
|
2021-03-25 00:58:31 +00:00
|
|
|
Help: "Prints Hilbish version",
|
|
|
|
})
|
|
|
|
setshflag := parser.Flag("S", "set-shell-env", &argparse.Options{
|
|
|
|
Required: false,
|
|
|
|
Help: "Sets $SHELL to Hilbish's executed path",
|
2021-03-20 04:01:24 +00:00
|
|
|
})
|
2021-04-07 12:40:40 +00:00
|
|
|
configflag := parser.String("C", "config", &argparse.Options{
|
|
|
|
Required: false,
|
|
|
|
Help: "Sets the path to Hilbish's config",
|
2021-04-19 02:09:27 +00:00
|
|
|
Default: defaultconfpath,
|
2021-04-07 12:40:40 +00:00
|
|
|
})
|
2021-04-18 22:53:22 +00:00
|
|
|
// loginshflag
|
|
|
|
// TODO: issue #37
|
2021-04-18 23:01:55 +00:00
|
|
|
_ = parser.Flag("l", "login", &argparse.Options{
|
2021-04-18 22:50:18 +00:00
|
|
|
Required: false,
|
|
|
|
Help: "Makes Hilbish act like a login shell",
|
|
|
|
})
|
|
|
|
|
2021-03-20 04:01:24 +00:00
|
|
|
|
|
|
|
err := parser.Parse(os.Args)
|
2021-03-28 22:58:58 +00:00
|
|
|
// If invalid flags or --help/-h,
|
2021-03-20 04:01:24 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// Print usage
|
2021-03-20 04:01:24 +00:00
|
|
|
fmt.Print(parser.Usage(err))
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *verflag {
|
|
|
|
fmt.Printf("Hilbish v%s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Set $SHELL if the user wants to
|
2021-04-19 02:09:27 +00:00
|
|
|
if *setshflag {
|
|
|
|
os.Setenv("SHELL", os.Args[0])
|
|
|
|
}
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// If user's config doesn't exixt,
|
2021-04-07 12:40:40 +00:00
|
|
|
if _, err := os.Stat(defaultconfpath); os.IsNotExist(err) {
|
2021-03-30 23:47:02 +00:00
|
|
|
// Read default from current directory
|
|
|
|
// (this is assuming the current dir is Hilbish's git)
|
|
|
|
input, err := os.ReadFile(".hilbishrc.lua")
|
|
|
|
if err != nil {
|
|
|
|
// If it wasnt found, go to "real default"
|
|
|
|
input, err = os.ReadFile("/usr/share/hilbish/.hilbishrc.lua")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not find .hilbishrc.lua or /usr/share/hilbish/.hilbishrc.lua")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// Create it using either default config we found
|
2021-04-19 02:09:27 +00:00
|
|
|
err = os.WriteFile(homedir+"/.hilbishrc.lua", input, 0644)
|
2021-03-25 16:07:51 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If that fails, bail
|
2021-03-25 16:07:51 +00:00
|
|
|
fmt.Println("Error creating config file")
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 07:51:44 +00:00
|
|
|
|
2021-04-14 17:20:03 +00:00
|
|
|
go HandleSignals()
|
2021-04-07 12:40:40 +00:00
|
|
|
LuaInit(*configflag)
|
2021-03-19 19:11:59 +00:00
|
|
|
|
2021-04-16 15:36:23 +00:00
|
|
|
hl := hilbiline.New("")
|
2021-04-14 23:55:03 +00:00
|
|
|
//readline.Completer = readline.FilenameCompleter
|
|
|
|
//readline.LoadHistory(homedir + "/.hilbish-history")
|
2021-03-28 21:11:57 +00:00
|
|
|
|
2021-03-19 19:11:59 +00:00
|
|
|
for {
|
2021-04-14 17:20:03 +00:00
|
|
|
running = false
|
2021-04-16 14:27:11 +00:00
|
|
|
|
2021-04-14 23:55:03 +00:00
|
|
|
hl.SetPrompt(fmtPrompt())
|
|
|
|
input, err := hl.Read()
|
2021-04-16 14:27:11 +00:00
|
|
|
|
2021-03-20 16:57:18 +00:00
|
|
|
if err == io.EOF {
|
2021-03-28 22:58:58 +00:00
|
|
|
// Exit if user presses ^D (ctrl + d)
|
2021-03-20 16:57:18 +00:00
|
|
|
fmt.Println("")
|
|
|
|
break
|
|
|
|
}
|
2021-03-19 19:11:59 +00:00
|
|
|
if err != nil {
|
2021-03-28 22:58:58 +00:00
|
|
|
// If we get a completely random error, print
|
2021-03-19 19:11:59 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-04-05 00:30:03 +00:00
|
|
|
input = strings.TrimSpace(input)
|
2021-04-16 14:27:11 +00:00
|
|
|
if len(input) == 0 { fmt.Print("\n"); continue }
|
2021-04-05 00:30:03 +00:00
|
|
|
|
2021-04-04 22:42:56 +00:00
|
|
|
if strings.HasSuffix(input, "\\") {
|
|
|
|
for {
|
|
|
|
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
|
|
|
|
|
2021-04-19 02:09:27 +00:00
|
|
|
if err != nil || !strings.HasSuffix(input, "\\") {
|
|
|
|
break
|
|
|
|
}
|
2021-04-04 22:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 17:20:03 +00:00
|
|
|
running = true
|
2021-04-04 22:42:56 +00:00
|
|
|
RunInput(input)
|
2021-04-07 01:12:19 +00:00
|
|
|
|
|
|
|
termwidth, _, err := term.GetSize(0)
|
2021-04-19 02:09:27 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth-1) + "\r")
|
2021-04-04 22:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContinuePrompt(prev string) (string, error) {
|
2021-04-18 19:41:37 +00:00
|
|
|
hl := hilbiline.New(multilinePrompt)
|
|
|
|
cont, err := hl.Read()
|
2021-04-04 22:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("")
|
|
|
|
return "", err
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
2021-04-04 22:42:56 +00:00
|
|
|
cont = strings.TrimSpace(cont)
|
|
|
|
|
2021-04-20 02:08:16 +00:00
|
|
|
return prev + " " + strings.TrimSuffix(cont, "\n"), nil
|
2021-03-19 19:11:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// This semi cursed function formats our prompt (obviously)
|
2021-03-26 05:06:14 +00:00
|
|
|
func fmtPrompt() string {
|
2021-03-28 21:11:57 +00:00
|
|
|
user, _ := user.Current()
|
|
|
|
host, _ := os.Hostname()
|
|
|
|
cwd, _ := os.Getwd()
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
cwd = strings.Replace(cwd, user.HomeDir, "~", 1)
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
args := []string{
|
2021-03-28 22:58:58 +00:00
|
|
|
"d", cwd,
|
|
|
|
"h", host,
|
2021-04-06 03:01:21 +00:00
|
|
|
"u", user.Username,
|
2021-03-28 21:11:57 +00:00
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
for i, v := range args {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
args[i] = "%" + v
|
|
|
|
}
|
|
|
|
}
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
r := strings.NewReplacer(args...)
|
|
|
|
nprompt := r.Replace(prompt)
|
2021-03-28 22:58:58 +00:00
|
|
|
|
2021-03-28 21:11:57 +00:00
|
|
|
return nprompt
|
2021-03-26 05:06:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-28 22:58:58 +00:00
|
|
|
// do i even have to say
|
2021-03-19 19:11:59 +00:00
|
|
|
func HandleSignals() {
|
2021-03-20 04:01:24 +00:00
|
|
|
c := make(chan os.Signal)
|
2021-04-14 17:20:03 +00:00
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
|
|
|
for range c {
|
|
|
|
if !running {
|
2021-04-14 23:55:03 +00:00
|
|
|
//fmt.Println(" // interrupt")
|
|
|
|
//readline.ReplaceLine("", 0)
|
|
|
|
//readline.RefreshLine()
|
2021-04-14 17:20:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 23:03:11 +00:00
|
|
|
}
|