Compare commits

..

No commits in common. "3460df6863f9fff236744cad998f0692497c63c7" and "ef4975f984b739062eb6354adf66c5424f0534dd" have entirely different histories.

6 changed files with 50 additions and 152 deletions

View File

@ -3,19 +3,23 @@ ansikit = require 'ansikit'
bait = require 'bait'
function doPrompt(fail)
prompt(ansikit.format(
prompt(ansikit.text(
'{blue}%u {cyan}%d ' .. (fail and '{red}' or '{green}') .. '∆{reset} '
))
end
print(ansikit.format('Welcome {cyan}'.. os.getenv 'USER' ..
print(ansikit.text('Welcome {cyan}'.. os.getenv 'USER' ..
'{reset} to {magenta}Hilbish{reset},\n' ..
'the nice lil shell for {blue}Lua{reset} fanatics!\n'))
doPrompt()
bait.catch('command.exit', function(code)
doPrompt(code ~= 0)
bait.catch('command.fail', function()
doPrompt(true)
end)
bait.catch('command.success', function()
doPrompt()
end)
--hook("tab complete", function ())

View File

@ -4,78 +4,13 @@
local ansikit = {}
ansikit.clear = function(scrollback)
typ = (scrollback and 3 or 2)
return ansikit.printCSI(typ, 'J')
end
ansikit.clearFromPos = function(scrollback)
return ansikit.printCSI(0, 'J')
end
ansikit.clearLine = function()
return ansikit.printCSI(2, 'K')
end
ansikit.clearToPos = function()
return ansikit.printCSI(1, 'J')
end
ansikit.color256 = function(color)
color = (color and color or 0)
return ansikit.printCSI('38;5;' .. color)
end
ansikit.cursorDown = function(y)
y = (y and y or 1)
return ansikit.printCSI(y, 'B')
end
ansikit.cursorLeft = function(x)
x = (x and x or 1)
return ansikit.printCSI(x, 'D')
end
-- TODO: cursorPos
-- https://github.com/Luvella/AnsiKit/blob/master/lib/index.js#L90
ansikit.cursorRight = function(x)
x = (x and x or 1)
return ansikit.printCSI(x, 'C')
end
ansikit.cursorStyle = function(style)
style = (style and style or ansikit.underlineCursor)
if style > 6 or style < 1 then style = ansikit.underlineCursor end
return ansikit.printCSI(style, ' q')
end
ansikit.cursorTo = function(x, y)
x, y = (x and x or 1), (y and y or 1)
return ansikit.printCSI(x .. ';' .. y, 'H')
end
ansikit.cursorUp = function(y)
y = (y and y or 1)
return ansikit.printCSI(y, 'A')
end
ansikit.getCode = function(code, terminate)
endc = (endc and endc or 'm')
return string.char(0x001b) .. code ..
(terminate and string.char(0x001b) .. '\\' or '')
end
ansikit.getCSI = function(code, endc)
ansikit.getCSI = function (code, endc)
endc = (endc and endc or 'm')
return string.char(0x001b) .. '[' .. code .. endc
end
ansikit.format = function(text)
ansikit.text = 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)},
@ -90,22 +25,8 @@ ansikit.format = function(text)
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)}
cyan = {'{cyan}', ansikit.getCSI(36)}
-- TODO: Background, bright colors
}
for k, v in pairs(colors) do
@ -115,25 +36,5 @@ ansikit.format = function(text)
return text
end
ansikit.print = function(text)
io.write(ansikit.format(text))
return ansikit
end
ansikit.printCode = function(code, terminate)
io.write(ansikit.getCode(code, terminate))
return ansikit
end
ansikit.printCSI = function(code, endc)
io.write(ansikit.getCSI(code, endc))
return ansikit
end
ansikit.println = function(text)
print(ansikit.print(text))
return ansikit
end
return ansikit

11
lua.go
View File

@ -12,7 +12,7 @@ import (
var minimalconf = `
ansikit = require 'ansikit'
prompt(ansikit.format(
prompt(ansikit.text(
'{blue}%u {cyan}%d {green}{reset} '
))
`
@ -25,7 +25,6 @@ func LuaInit() {
l.SetGlobal("_ver", lua.LString(version))
l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
// Add fs module to Lua
@ -65,7 +64,7 @@ func LuaInit() {
err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil {
fmt.Fprintln(os.Stderr, err,
"\nAn error has occured while loading your config! Falling back to minimal default config.\n")
"An error has occured while loading your config! Falling back to minimal default config.\n")
l.DoString(minimalconf)
}
@ -77,12 +76,6 @@ func hshprompt(L *lua.LState) int {
return 0
}
func hshmlprompt(L *lua.LState) int {
multilinePrompt = L.ToString(1)
return 0
}
func hshalias(L *lua.LState) int {
alias := L.ToString(1)
source := L.ToString(2)

View File

@ -17,12 +17,10 @@ import (
)
const version = "0.3.0"
const version = "0.3.0-dev"
var l *lua.LState
// User's prompt, this will get set when lua side is initialized
var prompt string
var multilinePrompt = "> "
// Map of builtin/custom commands defined in the commander lua module
var commands = map[string]bool{}
// Command aliases
@ -115,7 +113,7 @@ func main() {
}
func ContinuePrompt(prev string) (string, error) {
fmt.Print(multilinePrompt)
fmt.Printf("> ")
reader := bufio.NewReader(os.Stdin)

View File

@ -18,16 +18,12 @@ commander.register('cd', function (args)
if err == 1 then
print('directory does not exist')
end
bait.throw('command.exit', err)
else bait.throw('command.exit', 0) end
bait.throw('command.fail', nil)
else bait.throw('command.success', nil) end
return
end
fs.cd(os.getenv 'HOME')
bait.throw('command.exit', 0)
end)
commander.register('exit', function()
os.exit(0)
bait.throw('command.success', nil)
end)
do

View File

@ -47,41 +47,47 @@ func RunInput(input string) {
Protect: true,
}, luar.New(l, cmdArgs[1:]))
if err != nil {
fmt.Fprintln(os.Stderr,
"Error in command:\n\n" + err.Error())
// TODO: dont panic
panic(err)
}
if cmdArgs[0] != "exit" { HandleHistory(cmdString) }
HandleHistory(cmdString)
return
}
// Last option: use sh interpreter
err = execCommand(cmdString)
if err != nil {
// If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) {
for {
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
if err != nil { break }
err = execCommand(cmdString)
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) {
for {
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
if err != nil { break }
err = execCommand(cmdString)
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
continue
} else if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code)
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
bait.Em.Emit("command.exit", 1)
continue
} else if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code)
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
bait.Em.Emit("command.exit", 1)
}
break
}
break
} else {
if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code)
} else { fmt.Fprintln(os.Stderr, err) }
}
} else {
if code, ok := interp.IsExitStatus(err); ok {
bait.Em.Emit("command.exit", code)
} else { fmt.Fprintln(os.Stderr, err) }
bait.Em.Emit("command.exit", 0)
}
} else {
bait.Em.Emit("command.exit", 0)
HandleHistory(cmdString)
}
HandleHistory(cmdString)
}
// Run command in sh interpreter
@ -162,7 +168,7 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
// save input from previous prompts
if sb.String() == "" { sb.WriteString(prev + "\n") }
fmt.Print(multilinePrompt)
fmt.Printf("sh> ")
reader := bufio.NewReader(os.Stdin)