mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "6bb2a2f8c80391b5cdf2fe7bc2f662b399b7842b" and "75d7c6acc668109e5cd17fcc6818a7a37c1d63de" have entirely different histories.
6bb2a2f8c8
...
75d7c6acc6
|
@ -1,3 +0,0 @@
|
||||||
[submodule "libs/lunacolors"]
|
|
||||||
path = libs/lunacolors
|
|
||||||
url = https://github.com/Hilbis/Lunacolors
|
|
|
@ -1,15 +1,16 @@
|
||||||
-- Default Hilbish config
|
-- Default Hilbish config
|
||||||
lunacolors = require 'lunacolors'
|
ansikit = require 'ansikit'
|
||||||
bait = require 'bait'
|
bait = require 'bait'
|
||||||
|
|
||||||
function doPrompt(fail)
|
function doPrompt(fail)
|
||||||
prompt(lunacolors.format(
|
prompt(ansikit.format(
|
||||||
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆ '
|
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆{reset} '
|
||||||
))
|
))
|
||||||
end
|
end
|
||||||
|
|
||||||
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. _user ..
|
print(ansikit.format('Welcome to {magenta}Hilbish{reset}, {cyan}' ..
|
||||||
'{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
|
os.getenv 'USER' .. '{reset}.\n' ..
|
||||||
|
'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
|
||||||
|
|
||||||
doPrompt()
|
doPrompt()
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ sudo zypper install readline-devel
|
||||||
|
|
||||||
### Install
|
### Install
|
||||||
```sh
|
```sh
|
||||||
git clone --recursive https://github.com/Hilbis/Hilbish
|
git clone https://github.com/Hilbis/Hilbish
|
||||||
cd Hilbish
|
cd Hilbish
|
||||||
make build
|
make build
|
||||||
sudo make install
|
sudo make install
|
||||||
|
@ -80,7 +80,6 @@ Make sure to read [CONTRIBUTING.md](CONTRIBUTING.md) before getting started.
|
||||||
|
|
||||||
### Special Thanks To
|
### Special Thanks To
|
||||||
Everyone here who has contributed:
|
Everyone here who has contributed:
|
||||||
|
|
||||||
<a href="https://github.com/Hilbis/Hilbish/graphs/contributors">
|
<a href="https://github.com/Hilbis/Hilbish/graphs/contributors">
|
||||||
<img src="https://contrib.rocks/image?repo=Hilbis/Hilbish" />
|
<img src="https://contrib.rocks/image?repo=Hilbis/Hilbish" />
|
||||||
</a>
|
</a>
|
||||||
|
|
|
@ -29,8 +29,8 @@ func (c *Commander) Loader(L *lua.LState) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Commander) register(L *lua.LState) int {
|
func (c *Commander) register(L *lua.LState) int {
|
||||||
cmdName := L.CheckString(1)
|
cmdName := L.ToString(1)
|
||||||
cmd := L.CheckFunction(2)
|
cmd := L.ToFunction(2)
|
||||||
|
|
||||||
c.Events.Emit("commandRegister", cmdName, cmd)
|
c.Events.Emit("commandRegister", cmdName, cmd)
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/yuin/gopher-lua"
|
lua "github.com/yuin/gopher-lua"
|
||||||
"layeh.com/gopher-luar"
|
luar "layeh.com/gopher-luar"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Loader(L *lua.LState) int {
|
func Loader(L *lua.LState) int {
|
||||||
|
@ -27,7 +27,7 @@ var exports = map[string]lua.LGFunction{
|
||||||
}
|
}
|
||||||
|
|
||||||
func cd(L *lua.LState) int {
|
func cd(L *lua.LState) int {
|
||||||
path := L.CheckString(1)
|
path := L.ToString(1)
|
||||||
|
|
||||||
err := os.Chdir(strings.TrimSpace(path))
|
err := os.Chdir(strings.TrimSpace(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -41,7 +41,7 @@ func cd(L *lua.LState) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func mkdir(L *lua.LState) int {
|
func mkdir(L *lua.LState) int {
|
||||||
dirname := L.CheckString(1)
|
dirname := L.ToString(1)
|
||||||
|
|
||||||
// TODO: handle error here
|
// TODO: handle error here
|
||||||
os.Mkdir(strings.TrimSpace(dirname), 0744)
|
os.Mkdir(strings.TrimSpace(dirname), 0744)
|
||||||
|
@ -50,7 +50,7 @@ func mkdir(L *lua.LState) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func stat(L *lua.LState) int {
|
func stat(L *lua.LState) int {
|
||||||
path := L.CheckString(1)
|
path := L.ToString(1)
|
||||||
|
|
||||||
// TODO: handle error here
|
// TODO: handle error here
|
||||||
pathinfo, _ := os.Stat(path)
|
pathinfo, _ := os.Stat(path)
|
||||||
|
|
|
@ -61,6 +61,49 @@ ansikit.cursorUp = function(y)
|
||||||
return ansikit.printCSI(y, 'A')
|
return ansikit.printCSI(y, 'A')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ansikit.format = function(text)
|
||||||
|
local colors = {
|
||||||
|
-- TODO: write codes manually instead of using functions
|
||||||
|
-- less function calls = faster ????????
|
||||||
|
reset = {'{reset}', ansikit.getCSI(0)},
|
||||||
|
bold = {'{bold}', ansikit.getCSI(1)},
|
||||||
|
dim = {'{dim}', ansikit.getCSI(2)},
|
||||||
|
italic = {'{italic}', ansikit.getCSI(3)},
|
||||||
|
underline = {'{underline}', ansikit.getCSI(4)},
|
||||||
|
invert = {'{invert}', ansikit.getCSI(7)},
|
||||||
|
bold_off = {'{bold-off}', ansikit.getCSI(22)},
|
||||||
|
underline_off = {'{underline-off}', ansikit.getCSI(24)},
|
||||||
|
black = {'{black}', ansikit.getCSI(30)},
|
||||||
|
red = {'{red}', ansikit.getCSI(31)},
|
||||||
|
green = {'{green}', ansikit.getCSI(32)},
|
||||||
|
yellow = {'{yellow}', ansikit.getCSI(33)},
|
||||||
|
blue = {'{blue}', ansikit.getCSI(34)},
|
||||||
|
magenta = {'{magenta}', ansikit.getCSI(35)},
|
||||||
|
cyan = {'{cyan}', ansikit.getCSI(36)},
|
||||||
|
white = {'{white}', ansikit.getCSI(37)},
|
||||||
|
red_bg = {'{red-bg}', ansikit.getCSI(41)},
|
||||||
|
green_bg = {'{green-bg}', ansikit.getCSI(42)},
|
||||||
|
yellow_bg = {'{green-bg}', ansikit.getCSI(43)},
|
||||||
|
blue_bg = {'{blue-bg}', ansikit.getCSI(44)},
|
||||||
|
magenta_bg = {'{magenta-bg}', ansikit.getCSI(45)},
|
||||||
|
cyan_bg = {'{cyan-bg}', ansikit.getCSI(46)},
|
||||||
|
white_bg = {'{white-bg}', ansikit.getCSI(47)},
|
||||||
|
gray = {'{gray}', ansikit.getCSI(90)},
|
||||||
|
bright_red = {'{bright-red}', ansikit.getCSI(91)},
|
||||||
|
bright_green = {'{bright-green}', ansikit.getCSI(92)},
|
||||||
|
bright_yellow = {'{bright-yellow}', ansikit.getCSI(93)},
|
||||||
|
bright_blue = {'{bright-blue}', ansikit.getCSI(94)},
|
||||||
|
bright_magenta = {'{bright-magenta}', ansikit.getCSI(95)},
|
||||||
|
bright_cyan = {'{bright-cyan}', ansikit.getCSI(96)}
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v in pairs(colors) do
|
||||||
|
text = text:gsub(v[1], v[2])
|
||||||
|
end
|
||||||
|
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
ansikit.getCode = function(code, terminate)
|
ansikit.getCode = function(code, terminate)
|
||||||
return string.char(0x001b) .. code ..
|
return string.char(0x001b) .. code ..
|
||||||
(terminate and string.char(0x001b) .. '\\' or '')
|
(terminate and string.char(0x001b) .. '\\' or '')
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit 004bca95d6e848f03e237c46127286f8f064bebc
|
|
39
lua.go
39
lua.go
|
@ -2,12 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
"hilbish/golibs/commander"
|
"hilbish/golibs/commander"
|
||||||
"hilbish/golibs/fs"
|
"hilbish/golibs/fs"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
@ -19,13 +17,12 @@ prompt(ansikit.format(
|
||||||
))
|
))
|
||||||
`
|
`
|
||||||
|
|
||||||
func LuaInit() {
|
func LuaInit(confpath string) {
|
||||||
l = lua.NewState()
|
l = lua.NewState()
|
||||||
|
|
||||||
l.OpenLibs()
|
l.OpenLibs()
|
||||||
|
|
||||||
l.SetGlobal("_ver", lua.LString(version))
|
l.SetGlobal("_ver", lua.LString(version))
|
||||||
l.SetGlobal("_user", lua.LString(curuser.Username))
|
|
||||||
|
|
||||||
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
||||||
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
|
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
|
||||||
|
@ -56,7 +53,6 @@ func LuaInit() {
|
||||||
l.DoString(`package.path = package.path
|
l.DoString(`package.path = package.path
|
||||||
.. ';./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
.. ';./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
||||||
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
||||||
.. ';/usr/share/hilbish/libs/?/?.lua;'
|
|
||||||
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/init.lua;'
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/init.lua;'
|
||||||
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/?.lua;'
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/?.lua;'
|
||||||
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua'
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua'
|
||||||
|
@ -70,12 +66,12 @@ func LuaInit() {
|
||||||
"Missing preload file, builtins may be missing.")
|
"Missing preload file, builtins may be missing.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
func RunConfig(confpath string) {
|
// Run config
|
||||||
if !interactive {
|
if !interactive {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := l.DoFile(confpath)
|
err = l.DoFile(confpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err,
|
fmt.Fprintln(os.Stderr, err,
|
||||||
"\nAn error has occured while loading your config! Falling back to minimal default config.\n")
|
"\nAn error has occured while loading your config! Falling back to minimal default config.\n")
|
||||||
|
@ -84,35 +80,21 @@ func RunConfig(confpath string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunLogin() {
|
|
||||||
if _, err := os.Stat(homedir + "/.hprofile.lua"); os.IsNotExist(err) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if !login {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err := l.DoFile(homedir + "/.hprofile.lua")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, err,
|
|
||||||
"\nAn error has occured while loading your login config!n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func hshprompt(L *lua.LState) int {
|
func hshprompt(L *lua.LState) int {
|
||||||
prompt = L.CheckString(1)
|
prompt = L.ToString(1)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func hshmlprompt(L *lua.LState) int {
|
func hshmlprompt(L *lua.LState) int {
|
||||||
multilinePrompt = L.CheckString(1)
|
multilinePrompt = L.ToString(1)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func hshalias(L *lua.LState) int {
|
func hshalias(L *lua.LState) int {
|
||||||
alias := L.CheckString(1)
|
alias := L.ToString(1)
|
||||||
source := L.CheckString(2)
|
source := L.ToString(2)
|
||||||
|
|
||||||
aliases[alias] = source
|
aliases[alias] = source
|
||||||
|
|
||||||
|
@ -120,8 +102,7 @@ func hshalias(L *lua.LState) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func hshappendPath(L *lua.LState) int {
|
func hshappendPath(L *lua.LState) int {
|
||||||
path := L.CheckString(1)
|
path := L.ToString(1)
|
||||||
path = strings.Replace(path, "~", curuser.HomeDir, 1)
|
|
||||||
|
|
||||||
os.Setenv("PATH", os.Getenv("PATH") + ":" + path)
|
os.Setenv("PATH", os.Getenv("PATH") + ":" + path)
|
||||||
|
|
||||||
|
|
33
main.go
33
main.go
|
@ -4,10 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"strings"
|
||||||
|
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
|
|
||||||
|
@ -17,29 +16,26 @@ import (
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "0.4.0"
|
const version = "0.4.0-dev.6"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
l *lua.LState
|
l *lua.LState
|
||||||
|
|
||||||
prompt string // User's prompt, this will get set when lua side is initialized
|
// User's prompt, this will get set when lua side is initialized
|
||||||
|
prompt string
|
||||||
multilinePrompt = "> "
|
multilinePrompt = "> "
|
||||||
|
|
||||||
commands = map[string]bool{}
|
commands = map[string]bool{}
|
||||||
aliases = map[string]string{}
|
aliases = map[string]string{}
|
||||||
|
|
||||||
homedir string
|
|
||||||
curuser *user.User
|
|
||||||
|
|
||||||
running bool // Is a command currently running
|
|
||||||
hooks bait.Bait
|
hooks bait.Bait
|
||||||
|
homedir string
|
||||||
|
running bool
|
||||||
interactive bool
|
interactive bool
|
||||||
login bool // Are we the login shell?
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
homedir, _ = os.UserHomeDir()
|
homedir, _ = os.UserHomeDir()
|
||||||
curuser, _ = user.Current()
|
|
||||||
defaultconfpath := homedir + "/.hilbishrc.lua"
|
defaultconfpath := homedir + "/.hilbishrc.lua"
|
||||||
|
|
||||||
// parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
|
// parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers")
|
||||||
|
@ -53,7 +49,6 @@ func main() {
|
||||||
_ = getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
|
_ = getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
|
||||||
|
|
||||||
getopt.Parse()
|
getopt.Parse()
|
||||||
loginshflag := getopt.Lookup('l').Seen()
|
|
||||||
interactiveflag := getopt.Lookup('i').Seen()
|
interactiveflag := getopt.Lookup('i').Seen()
|
||||||
|
|
||||||
if *cmdflag == "" || interactiveflag {
|
if *cmdflag == "" || interactiveflag {
|
||||||
|
@ -64,11 +59,6 @@ func main() {
|
||||||
interactive = false
|
interactive = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// first arg, first character
|
|
||||||
if loginshflag || os.Args[0][0] == '-' {
|
|
||||||
login = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if *verflag {
|
if *verflag {
|
||||||
fmt.Printf("Hilbish v%s\n", version)
|
fmt.Printf("Hilbish v%s\n", version)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -104,9 +94,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
go HandleSignals()
|
go HandleSignals()
|
||||||
LuaInit()
|
LuaInit(*configflag)
|
||||||
RunLogin()
|
|
||||||
RunConfig(*configflag)
|
|
||||||
|
|
||||||
readline.Completer = readline.FilenameCompleter
|
readline.Completer = readline.FilenameCompleter
|
||||||
readline.LoadHistory(homedir + "/.hilbish-history")
|
readline.LoadHistory(homedir + "/.hilbish-history")
|
||||||
|
@ -160,7 +148,6 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContinuePrompt(prev string) (string, error) {
|
func ContinuePrompt(prev string) (string, error) {
|
||||||
hooks.Em.Emit("multiline", nil)
|
|
||||||
cont, err := readline.String(multilinePrompt)
|
cont, err := readline.String(multilinePrompt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
@ -173,16 +160,16 @@ func ContinuePrompt(prev string) (string, error) {
|
||||||
|
|
||||||
// This semi cursed function formats our prompt (obviously)
|
// This semi cursed function formats our prompt (obviously)
|
||||||
func fmtPrompt() string {
|
func fmtPrompt() string {
|
||||||
|
user, _ := user.Current()
|
||||||
host, _ := os.Hostname()
|
host, _ := os.Hostname()
|
||||||
cwd, _ := os.Getwd()
|
cwd, _ := os.Getwd()
|
||||||
|
|
||||||
cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1)
|
cwd = strings.Replace(cwd, user.HomeDir, "~", 1)
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"d", cwd,
|
"d", cwd,
|
||||||
"D", filepath.Base(cwd),
|
|
||||||
"h", host,
|
"h", host,
|
||||||
"u", curuser.Username,
|
"u", user.Username,
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, v := range args {
|
for i, v := range args {
|
||||||
|
|
|
@ -12,8 +12,6 @@ commander.register('cd', function (args)
|
||||||
for i = 1, #args do
|
for i = 1, #args do
|
||||||
path = path .. tostring(args[i]) .. ' '
|
path = path .. tostring(args[i]) .. ' '
|
||||||
end
|
end
|
||||||
path = path:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
|
|
||||||
:gsub('$([%w_]+)', os.getenv):gsub('%z','$')
|
|
||||||
|
|
||||||
local ok, err = pcall(function() fs.cd(path) end)
|
local ok, err = pcall(function() fs.cd(path) end)
|
||||||
if not ok then
|
if not ok then
|
||||||
|
|
Loading…
Reference in New Issue