Compare commits

..

No commits in common. "6bb2a2f8c80391b5cdf2fe7bc2f662b399b7842b" and "75d7c6acc668109e5cd17fcc6818a7a37c1d63de" have entirely different histories.

11 changed files with 78 additions and 73 deletions

3
.gitmodules vendored
View File

@ -1,3 +0,0 @@
[submodule "libs/lunacolors"]
path = libs/lunacolors
url = https://github.com/Hilbis/Lunacolors

View File

@ -1,15 +1,16 @@
-- Default Hilbish config
lunacolors = require 'lunacolors'
ansikit = require 'ansikit'
bait = require 'bait'
function doPrompt(fail)
prompt(lunacolors.format(
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. ' '
prompt(ansikit.format(
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '{reset} '
))
end
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. _user ..
'{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
print(ansikit.format('Welcome to {magenta}Hilbish{reset}, {cyan}' ..
os.getenv 'USER' .. '{reset}.\n' ..
'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
doPrompt()

View File

@ -52,7 +52,7 @@ sudo zypper install readline-devel
### Install
```sh
git clone --recursive https://github.com/Hilbis/Hilbish
git clone https://github.com/Hilbis/Hilbish
cd Hilbish
make build
sudo make install
@ -80,7 +80,6 @@ Make sure to read [CONTRIBUTING.md](CONTRIBUTING.md) before getting started.
### Special Thanks To
Everyone here who has contributed:
<a href="https://github.com/Hilbis/Hilbish/graphs/contributors">
<img src="https://contrib.rocks/image?repo=Hilbis/Hilbish" />
</a>

View File

@ -29,8 +29,8 @@ func (c *Commander) Loader(L *lua.LState) int {
}
func (c *Commander) register(L *lua.LState) int {
cmdName := L.CheckString(1)
cmd := L.CheckFunction(2)
cmdName := L.ToString(1)
cmd := L.ToFunction(2)
c.Events.Emit("commandRegister", cmdName, cmd)

View File

@ -4,8 +4,8 @@ import (
"os"
"strings"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
lua "github.com/yuin/gopher-lua"
luar "layeh.com/gopher-luar"
)
func Loader(L *lua.LState) int {
@ -27,7 +27,7 @@ var exports = map[string]lua.LGFunction{
}
func cd(L *lua.LState) int {
path := L.CheckString(1)
path := L.ToString(1)
err := os.Chdir(strings.TrimSpace(path))
if err != nil {
@ -41,7 +41,7 @@ func cd(L *lua.LState) int {
}
func mkdir(L *lua.LState) int {
dirname := L.CheckString(1)
dirname := L.ToString(1)
// TODO: handle error here
os.Mkdir(strings.TrimSpace(dirname), 0744)
@ -50,7 +50,7 @@ func mkdir(L *lua.LState) int {
}
func stat(L *lua.LState) int {
path := L.CheckString(1)
path := L.ToString(1)
// TODO: handle error here
pathinfo, _ := os.Stat(path)

View File

@ -61,6 +61,49 @@ ansikit.cursorUp = function(y)
return ansikit.printCSI(y, 'A')
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)
return string.char(0x001b) .. code ..
(terminate and string.char(0x001b) .. '\\' or '')

@ -1 +0,0 @@
Subproject commit 004bca95d6e848f03e237c46127286f8f064bebc

39
lua.go
View File

@ -2,12 +2,10 @@ package main
import (
"fmt"
"os"
"strings"
"hilbish/golibs/bait"
"hilbish/golibs/commander"
"hilbish/golibs/fs"
"os"
"github.com/yuin/gopher-lua"
)
@ -19,13 +17,12 @@ prompt(ansikit.format(
))
`
func LuaInit() {
func LuaInit(confpath string) {
l = lua.NewState()
l.OpenLibs()
l.SetGlobal("_ver", lua.LString(version))
l.SetGlobal("_user", lua.LString(curuser.Username))
l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
@ -56,7 +53,6 @@ func LuaInit() {
l.DoString(`package.path = package.path
.. ';./libs/?/init.lua;./?/init.lua;./?/?.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/?/?.lua;'
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua'
@ -70,12 +66,12 @@ func LuaInit() {
"Missing preload file, builtins may be missing.")
}
}
}
func RunConfig(confpath string) {
// Run config
if !interactive {
return
}
err := l.DoFile(confpath)
err = l.DoFile(confpath)
if err != nil {
fmt.Fprintln(os.Stderr, err,
"\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 {
prompt = L.CheckString(1)
prompt = L.ToString(1)
return 0
}
func hshmlprompt(L *lua.LState) int {
multilinePrompt = L.CheckString(1)
multilinePrompt = L.ToString(1)
return 0
}
func hshalias(L *lua.LState) int {
alias := L.CheckString(1)
source := L.CheckString(2)
alias := L.ToString(1)
source := L.ToString(2)
aliases[alias] = source
@ -120,8 +102,7 @@ func hshalias(L *lua.LState) int {
}
func hshappendPath(L *lua.LState) int {
path := L.CheckString(1)
path = strings.Replace(path, "~", curuser.HomeDir, 1)
path := L.ToString(1)
os.Setenv("PATH", os.Getenv("PATH") + ":" + path)

33
main.go
View File

@ -4,10 +4,9 @@ import (
"fmt"
"io"
"os"
"strings"
"os/signal"
"os/user"
"path/filepath"
"strings"
"hilbish/golibs/bait"
@ -17,29 +16,26 @@ import (
"golang.org/x/term"
)
const version = "0.4.0"
const version = "0.4.0-dev.6"
var (
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 = "> "
commands = map[string]bool{}
aliases = map[string]string{}
homedir string
curuser *user.User
running bool // Is a command currently running
hooks bait.Bait
homedir string
running bool
interactive bool
login bool // Are we the login shell?
)
func main() {
homedir, _ = os.UserHomeDir()
curuser, _ = user.Current()
defaultconfpath := homedir + "/.hilbishrc.lua"
// 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.Parse()
loginshflag := getopt.Lookup('l').Seen()
interactiveflag := getopt.Lookup('i').Seen()
if *cmdflag == "" || interactiveflag {
@ -64,11 +59,6 @@ func main() {
interactive = false
}
// first arg, first character
if loginshflag || os.Args[0][0] == '-' {
login = true
}
if *verflag {
fmt.Printf("Hilbish v%s\n", version)
os.Exit(0)
@ -104,9 +94,7 @@ func main() {
}
go HandleSignals()
LuaInit()
RunLogin()
RunConfig(*configflag)
LuaInit(*configflag)
readline.Completer = readline.FilenameCompleter
readline.LoadHistory(homedir + "/.hilbish-history")
@ -160,7 +148,6 @@ func main() {
}
func ContinuePrompt(prev string) (string, error) {
hooks.Em.Emit("multiline", nil)
cont, err := readline.String(multilinePrompt)
if err != nil {
fmt.Println("")
@ -173,16 +160,16 @@ func ContinuePrompt(prev string) (string, error) {
// This semi cursed function formats our prompt (obviously)
func fmtPrompt() string {
user, _ := user.Current()
host, _ := os.Hostname()
cwd, _ := os.Getwd()
cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1)
cwd = strings.Replace(cwd, user.HomeDir, "~", 1)
args := []string{
"d", cwd,
"D", filepath.Base(cwd),
"h", host,
"u", curuser.Username,
"u", user.Username,
}
for i, v := range args {

View File

@ -12,8 +12,6 @@ commander.register('cd', function (args)
for i = 1, #args do
path = path .. tostring(args[i]) .. ' '
end
path = path:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
:gsub('$([%w_]+)', os.getenv):gsub('%z','$')
local ok, err = pcall(function() fs.cd(path) end)
if not ok then