mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "352c5f73a8f461d99b20456126ab2502dc798789" and "8b7c7e707a7d06e7e49d00c457d140e760488142" have entirely different histories.
352c5f73a8
...
8b7c7e707a
15
Makefile
15
Makefile
|
@ -1,5 +1,4 @@
|
|||
PREFIX ?= /usr
|
||||
DESTDIR ?=
|
||||
BINDIR ?= $(PREFIX)/bin
|
||||
LIBDIR ?= $(PREFIX)/share/hilbish
|
||||
|
||||
|
@ -7,17 +6,17 @@ build:
|
|||
@go build
|
||||
|
||||
install:
|
||||
@install -v -d "$(DESTDIR)$(BINDIR)/" && install -m 0755 -v hilbish "$(DESTDIR)$(BINDIR)/hilbish"
|
||||
@mkdir -p "$(DESTDIR)$(LIBDIR)"
|
||||
@cp libs preload.lua .hilbishrc.lua "$(DESTDIR)$(LIBDIR)" -r
|
||||
@echo "$(DESTDIR)$(BINDIR)/hilbish" >> /etc/shells
|
||||
@install -v -d "$(BINDIR)/" && install -m 0755 -v hilbish "$(BINDIR)/hilbish"
|
||||
@mkdir -p "$(LIBDIR)"
|
||||
@cp libs preload.lua .hilbishrc.lua "$(LIBDIR)" -r
|
||||
@echo /usr/bin/hilbish >> /etc/shells
|
||||
@echo "Hilbish Installed"
|
||||
|
||||
uninstall:
|
||||
@rm -vrf \
|
||||
"$(DESTDIR)$(BINDIR)/hilbish" \
|
||||
"$(DESTDIR)$(LIBDIR)"
|
||||
@sed -i '/hilbish/d' /etc/shells
|
||||
"$(BINDIR)/hilbish" \
|
||||
"$(LIBDIR)"
|
||||
@sed '/\/usr\/bin\/hilbish/d' /etc/shells
|
||||
@echo "Hilbish Uninstalled"
|
||||
|
||||
clean:
|
||||
|
|
|
@ -22,7 +22,7 @@ cd Hilbish
|
|||
make build
|
||||
sudo make install
|
||||
# Or
|
||||
sudo make all
|
||||
sudo make
|
||||
```
|
||||
|
||||
Alternativly, if you use Arch Linux, you can install Hilbish with an **(unofficial)** AUR package
|
||||
|
|
107
main.go
107
main.go
|
@ -4,7 +4,8 @@ import (
|
|||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
_ "os/exec"
|
||||
_ "os/user"
|
||||
"syscall"
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
@ -24,11 +25,8 @@ import (
|
|||
|
||||
const version = "0.2.0-dev"
|
||||
var l *lua.LState
|
||||
// User's prompt, this will get set when lua side is initialized
|
||||
var prompt string
|
||||
// Map of builtin/custom commands defined in the commander lua module
|
||||
var commands = map[string]bool{}
|
||||
// Command aliases
|
||||
var aliases = map[string]string{}
|
||||
|
||||
func main() {
|
||||
|
@ -43,9 +41,7 @@ func main() {
|
|||
})
|
||||
|
||||
err := parser.Parse(os.Args)
|
||||
// If invalid flags or --help/-h,
|
||||
if err != nil {
|
||||
// Print usage
|
||||
fmt.Print(parser.Usage(err))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
@ -55,14 +51,10 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Set $SHELL if the user wants to
|
||||
if *setshflag { os.Setenv("SHELL", os.Args[0]) }
|
||||
|
||||
// Read config 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")
|
||||
|
@ -71,12 +63,9 @@ func main() {
|
|||
}
|
||||
|
||||
homedir, _ := os.UserHomeDir()
|
||||
// If user's config doesn't exixt,
|
||||
if _, err := os.Stat(homedir + "/.hilbishrc.lua"); os.IsNotExist(err) {
|
||||
// Create it using either default config we found
|
||||
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
||||
if err != nil {
|
||||
// If that fails, bail
|
||||
fmt.Println("Error creating config file")
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
@ -87,43 +76,41 @@ func main() {
|
|||
LuaInit()
|
||||
|
||||
readline.Completer = readline.FilenameCompleter
|
||||
|
||||
for {
|
||||
//user, _ := user.Current()
|
||||
//dir, _ := os.Getwd()
|
||||
//host, _ := os.Hostname()
|
||||
|
||||
//reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
//fmt.Printf(prompt)
|
||||
|
||||
cmdString, err := readline.String(fmtPrompt())
|
||||
if err == io.EOF {
|
||||
// Exit if user presses ^D (ctrl + d)
|
||||
fmt.Println("")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
// If we get a completely random error, print
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
||||
// I have no idea if we need this anymore
|
||||
cmdString = strings.TrimSuffix(cmdString, "\n")
|
||||
// First try to run user input in Lua
|
||||
err = l.DoString(cmdString)
|
||||
|
||||
if err == nil {
|
||||
// If it succeeds, add to history and prompt again
|
||||
readline.AddHistory(cmdString)
|
||||
continue
|
||||
}
|
||||
|
||||
// Split up the input
|
||||
cmdArgs, cmdString := splitInput(cmdString)
|
||||
// If there's actually no input, prompt again
|
||||
cmdArgs := splitInput(cmdString)
|
||||
if len(cmdArgs) == 0 { continue }
|
||||
|
||||
// If alias was found, use command alias
|
||||
if aliases[cmdArgs[0]] != "" {
|
||||
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
||||
//cmdArgs := splitInput(cmdString)
|
||||
execCommand(cmdString)
|
||||
continue
|
||||
}
|
||||
|
||||
// If command is defined in Lua then run it
|
||||
if commands[cmdArgs[0]] {
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: l.GetField(
|
||||
|
@ -141,15 +128,12 @@ func main() {
|
|||
readline.AddHistory(cmdString)
|
||||
continue
|
||||
}
|
||||
|
||||
// Last option: use sh interpreter
|
||||
switch cmdArgs[0] {
|
||||
case "exit":
|
||||
os.Exit(0)
|
||||
default:
|
||||
err := execCommand(cmdString)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
sb := &strings.Builder{}
|
||||
for {
|
||||
|
@ -166,35 +150,11 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
return prompt
|
||||
}
|
||||
|
||||
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") }
|
||||
|
||||
fmt.Printf("... ")
|
||||
|
@ -203,7 +163,6 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
|
|||
|
||||
cont, err := reader.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
// Exit when ^D
|
||||
fmt.Println("")
|
||||
return true
|
||||
}
|
||||
|
@ -215,64 +174,36 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return true
|
||||
}
|
||||
|
||||
func splitInput(input string) ([]string, string) {
|
||||
// end my suffering
|
||||
// TODO: refactor this garbage
|
||||
func splitInput(input string) []string {
|
||||
quoted := false
|
||||
startlastcmd := false
|
||||
lastcmddone := false
|
||||
cmdArgs := []string{}
|
||||
sb := &strings.Builder{}
|
||||
cmdstr := &strings.Builder{}
|
||||
lastcmd := readline.GetHistory(readline.HistorySize() - 1)
|
||||
|
||||
for _, r := range input {
|
||||
if r == '"' {
|
||||
// start quoted input
|
||||
// this determines if other runes are replaced
|
||||
quoted = !quoted
|
||||
// dont add back quotes
|
||||
//sb.WriteRune(r)
|
||||
} else if !quoted && r == '~' {
|
||||
// if not in quotes and ~ is found then make it $HOME
|
||||
sb.WriteString(os.Getenv("HOME"))
|
||||
} else if !quoted && r == ' ' {
|
||||
// if not quoted and there's a space then add to cmdargs
|
||||
cmdArgs = append(cmdArgs, sb.String())
|
||||
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 {
|
||||
sb.WriteRune(r)
|
||||
}
|
||||
cmdstr.WriteRune(r)
|
||||
}
|
||||
if sb.Len() > 0 {
|
||||
cmdArgs = append(cmdArgs, sb.String())
|
||||
}
|
||||
|
||||
readline.AddHistory(input)
|
||||
return cmdArgs, cmdstr.String()
|
||||
return cmdArgs
|
||||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
func execCommand(cmd string) error {
|
||||
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||
if err != nil {
|
||||
|
@ -286,7 +217,6 @@ func execCommand(cmd string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// do i even have to say
|
||||
func HandleSignals() {
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
|
@ -296,7 +226,6 @@ func HandleSignals() {
|
|||
}
|
||||
|
||||
func LuaInit() {
|
||||
// TODO: Move to lua.go
|
||||
l = lua.NewState()
|
||||
|
||||
l.OpenLibs()
|
||||
|
@ -304,11 +233,9 @@ func LuaInit() {
|
|||
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
||||
l.SetGlobal("alias", l.NewFunction(hshalias))
|
||||
|
||||
// Add fs module to Lua
|
||||
l.PreloadModule("fs", lfs.Loader)
|
||||
|
||||
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
|
||||
|
@ -321,7 +248,6 @@ func LuaInit() {
|
|||
|
||||
l.PreloadModule("commander", commander.Loader)
|
||||
|
||||
// Add more paths that Lua can require from
|
||||
l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")
|
||||
|
||||
err := l.DoFile("/usr/share/hilbish/preload.lua")
|
||||
|
@ -334,7 +260,6 @@ func LuaInit() {
|
|||
}
|
||||
|
||||
homedir, _ := os.UserHomeDir()
|
||||
// Run config
|
||||
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
|
Loading…
Reference in New Issue