Compare commits

...

9 Commits

Author SHA1 Message Date
TorchedSammy 3460df6863 fix: make cd only throw command.exit hooks 2021-04-05 18:31:59 -04:00
TorchedSammy fad4282345 chore: bump version 2021-04-05 18:25:55 -04:00
TorchedSammy febd6ba2cd chore: merge from master 2021-04-05 18:19:08 -04:00
TorchedSammy 84d55a38b0 feat: change multiline prompt via multiprompt function (closes #13) 2021-04-05 18:09:21 -04:00
TorchedSammy 4c63009f56 feat!: add more functions to ansikit, change text function to format 2021-04-05 17:35:43 -04:00
TorchedSammy 898f8816ff fix: update to work with latest hilbish 2021-04-05 17:27:55 -04:00
TorchedSammy 271ea946eb fix: update to work with latest ansikit 2021-04-05 17:26:55 -04:00
TorchedSammy 807ec15faa fix: cleanup and move exit command to lua side 2021-04-05 15:21:44 -04:00
TorchedSammy 453d04983a chore: add readline as prerequisite 2021-04-04 13:14:30 -04:00
6 changed files with 152 additions and 50 deletions

View File

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

View File

@ -4,13 +4,78 @@
local ansikit = {} 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') endc = (endc and endc or 'm')
return string.char(0x001b) .. '[' .. code .. endc return string.char(0x001b) .. '[' .. code .. endc
end end
ansikit.text = function (text) ansikit.format = function(text)
local colors = { local colors = {
-- TODO: write codes manually instead of using functions
-- less function calls = faster ????????
reset = {'{reset}', ansikit.getCSI(0)}, reset = {'{reset}', ansikit.getCSI(0)},
bold = {'{bold}', ansikit.getCSI(1)}, bold = {'{bold}', ansikit.getCSI(1)},
dim = {'{dim}', ansikit.getCSI(2)}, dim = {'{dim}', ansikit.getCSI(2)},
@ -25,8 +90,22 @@ ansikit.text = function (text)
yellow = {'{yellow}', ansikit.getCSI(33)}, yellow = {'{yellow}', ansikit.getCSI(33)},
blue = {'{blue}', ansikit.getCSI(34)}, blue = {'{blue}', ansikit.getCSI(34)},
magenta = {'{magenta}', ansikit.getCSI(35)}, magenta = {'{magenta}', ansikit.getCSI(35)},
cyan = {'{cyan}', ansikit.getCSI(36)} cyan = {'{cyan}', ansikit.getCSI(36)},
-- TODO: Background, bright colors 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 for k, v in pairs(colors) do
@ -36,5 +115,25 @@ ansikit.text = function (text)
return text return text
end 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 return ansikit

11
lua.go
View File

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

View File

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

View File

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

View File

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