mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "72194898bac87c725c10ad7a8d10d02830af5297" and "6b065dc035da73f5f80d60dca121c925b6f55acf" have entirely different histories.
72194898ba
...
6b065dc035
|
@ -1,9 +1,3 @@
|
|||
[submodule "libs/lunacolors"]
|
||||
path = libs/lunacolors
|
||||
url = https://github.com/Hilbis/Lunacolors
|
||||
[submodule "libs/succulent"]
|
||||
path = libs/succulent
|
||||
url = https://github.com/Rosettea/Succulent
|
||||
[submodule "libs/inspect"]
|
||||
path = libs/inspect
|
||||
url = https://github.com/kikito/inspect.lua
|
||||
|
|
|
@ -43,7 +43,6 @@ func main() {
|
|||
"fs": "f",
|
||||
"commander": "c",
|
||||
"bait": "b",
|
||||
"terminal": "term",
|
||||
}
|
||||
docs := make(map[string][]string)
|
||||
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
setRaw() > Puts the terminal in raw mode
|
||||
|
||||
restoreState() > Restores the last saved state of the terminal
|
||||
|
||||
saveState() > Saves the current state of the terminal
|
||||
|
||||
size() > Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
||||
Note: this is not the size in relation to the dimensions of the display
|
||||
|
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"hilbish/util"
|
||||
"github.com/yuin/gopher-lua"
|
||||
"layeh.com/gopher-luar"
|
||||
)
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
|
@ -22,6 +23,10 @@ addition to the Lua standard library's I/O and fs functions.`)
|
|||
return 1
|
||||
}
|
||||
|
||||
func luaErr(L *lua.LState, msg string) {
|
||||
L.Error(lua.LString(msg), 2)
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"cd": fcd,
|
||||
"mkdir": fmkdir,
|
||||
|
@ -37,7 +42,7 @@ func fcd(L *lua.LState) int {
|
|||
err := os.Chdir(strings.TrimSpace(path))
|
||||
if err != nil {
|
||||
e := err.(*os.PathError).Err.Error()
|
||||
L.RaiseError(e)
|
||||
luaErr(L, e)
|
||||
}
|
||||
|
||||
return 0
|
||||
|
@ -57,7 +62,7 @@ func fmkdir(L *lua.LState) int {
|
|||
err = os.Mkdir(path, 0744)
|
||||
}
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
luaErr(L, err.Error())
|
||||
}
|
||||
|
||||
return 0
|
||||
|
@ -70,7 +75,7 @@ func fstat(L *lua.LState) int {
|
|||
|
||||
pathinfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
luaErr(L, err.Error())
|
||||
return 0
|
||||
}
|
||||
statTbl := L.NewTable()
|
||||
|
@ -87,18 +92,18 @@ func fstat(L *lua.LState) int {
|
|||
// Returns a table of files in `dir`
|
||||
func freaddir(L *lua.LState) int {
|
||||
dir := L.CheckString(1)
|
||||
names := L.NewTable()
|
||||
names := []string{}
|
||||
|
||||
dirEntries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
luaErr(L, err.Error())
|
||||
return 0
|
||||
}
|
||||
for _, entry := range dirEntries {
|
||||
names.Append(lua.LString(entry.Name()))
|
||||
names = append(names, entry.Name())
|
||||
}
|
||||
|
||||
L.Push(names)
|
||||
L.Push(luar.New(L, names))
|
||||
|
||||
return 1
|
||||
}
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
package terminal
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"hilbish/util"
|
||||
|
||||
"golang.org/x/term"
|
||||
"github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
var termState *term.State
|
||||
|
||||
func Loader(L *lua.LState) int {
|
||||
mod := L.SetFuncs(L.NewTable(), exports)
|
||||
util.Document(L, mod, "The terminal library is a simple and lower level library for certain terminal interactions.")
|
||||
|
||||
L.Push(mod)
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
var exports = map[string]lua.LGFunction{
|
||||
"setRaw": termraw,
|
||||
"restoreState": termrestoreState,
|
||||
"size": termsize,
|
||||
"saveState": termsaveState,
|
||||
}
|
||||
|
||||
// size()
|
||||
// Gets the dimensions of the terminal. Returns a table with `width` and `height`
|
||||
// Note: this is not the size in relation to the dimensions of the display
|
||||
func termsize(L *lua.LState) int {
|
||||
w, h, err := term.GetSize(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
return 0
|
||||
}
|
||||
dimensions := L.NewTable()
|
||||
L.SetField(dimensions, "width", lua.LNumber(w))
|
||||
L.SetField(dimensions, "height", lua.LNumber(h))
|
||||
|
||||
L.Push(dimensions)
|
||||
return 1
|
||||
}
|
||||
|
||||
// saveState()
|
||||
// Saves the current state of the terminal
|
||||
func termsaveState(L *lua.LState) int {
|
||||
state, err := term.GetState(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
return 0
|
||||
}
|
||||
|
||||
termState = state
|
||||
return 0
|
||||
}
|
||||
|
||||
// restoreState()
|
||||
// Restores the last saved state of the terminal
|
||||
func termrestoreState(L *lua.LState) int {
|
||||
err := term.Restore(int(os.Stdin.Fd()), termState)
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
// setRaw()
|
||||
// Puts the terminal in raw mode
|
||||
func termraw(L *lua.LState) int {
|
||||
_, err := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
if err != nil {
|
||||
L.RaiseError(err.Error())
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
|
@ -35,7 +35,6 @@ func HilbishLoader(L *lua.LState) int {
|
|||
L.SetField(mod, "user", lua.LString(username))
|
||||
L.SetField(mod, "host", lua.LString(host))
|
||||
L.SetField(mod, "home", lua.LString(homedir))
|
||||
L.SetField(mod, "dataDir", lua.LString(dataDir))
|
||||
|
||||
xdg := L.NewTable()
|
||||
L.SetField(xdg, "config", lua.LString(confDir))
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
-- We're basically porting Ansikit to lua
|
||||
-- https://github.com/Luvella/AnsiKit/blob/master/lib/index.js
|
||||
-- which is made by yours truly sammy :^)
|
||||
local lunacolors = require 'lunacolors'
|
||||
|
||||
local ansikit = {}
|
||||
|
||||
ansikit.clear = function(scrollback)
|
||||
|
@ -77,12 +77,6 @@ ansikit.hideCursor = function()
|
|||
return ansikit.printCSI('?25', 'l')
|
||||
end
|
||||
|
||||
ansikit.link = function(url, text)
|
||||
if not url then error 'ansikit: missing url for hyperlink' end
|
||||
local text = (text and text or 'link')
|
||||
io.write(lunacolors.blue('\27]8;;' .. url .. '\27\\' .. text .. '\27]8;;\27\\\n'))
|
||||
end
|
||||
|
||||
ansikit.print = function(text)
|
||||
io.write(ansikit.format(text))
|
||||
return ansikit
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
Subproject commit 96dc95c24989be6596ccb9f7f7244d37f03d4acd
|
4
lua.go
4
lua.go
|
@ -11,7 +11,6 @@ import (
|
|||
"hilbish/golibs/bait"
|
||||
"hilbish/golibs/commander"
|
||||
"hilbish/golibs/fs"
|
||||
"hilbish/golibs/terminal"
|
||||
|
||||
"github.com/yuin/gopher-lua"
|
||||
"layeh.com/gopher-luar"
|
||||
|
@ -41,9 +40,8 @@ func LuaInit() {
|
|||
l.PreloadModule("hilbish", HilbishLoader)
|
||||
l.DoString("hilbish = require 'hilbish'")
|
||||
|
||||
// Add fs and terminal module module to Lua
|
||||
// Add fs module to Lua
|
||||
l.PreloadModule("fs", fs.Loader)
|
||||
l.PreloadModule("terminal", terminal.Loader)
|
||||
|
||||
cmds := commander.New()
|
||||
// When a command from Lua is added, register it for use
|
||||
|
|
1
main.go
1
main.go
|
@ -175,6 +175,7 @@ input:
|
|||
|
||||
if err == io.EOF {
|
||||
// Exit if user presses ^D (ctrl + d)
|
||||
fmt.Println("")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
|
|
78
preload.lua
78
preload.lua
|
@ -3,17 +3,19 @@
|
|||
local fs = require 'fs'
|
||||
local commander = require 'commander'
|
||||
local bait = require 'bait'
|
||||
require 'succulent' -- Function additions
|
||||
local oldDir = hilbish.cwd()
|
||||
|
||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
|
||||
|
||||
-- Builtins
|
||||
local recentDirs = {}
|
||||
commander.register('cd', function (args)
|
||||
if #args > 0 then
|
||||
local path = table.concat(args, ' '):gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
|
||||
local path = ''
|
||||
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','$'):gsub('^%s*(.-)%s*$', '%1')
|
||||
|
||||
if path == '-' then
|
||||
|
@ -24,15 +26,12 @@ commander.register('cd', function (args)
|
|||
|
||||
local ok, err = pcall(function() fs.cd(path) end)
|
||||
if not ok then
|
||||
print(err:sub(17))
|
||||
return 1
|
||||
if err == 1 then
|
||||
print('directory does not exist')
|
||||
end
|
||||
return err
|
||||
end
|
||||
bait.throw('cd', path)
|
||||
|
||||
-- add to table of recent dirs
|
||||
table.insert(recentDirs, 1, path)
|
||||
recentDirs[11] = nil
|
||||
|
||||
return
|
||||
end
|
||||
fs.cd(hilbish.home)
|
||||
|
@ -50,7 +49,11 @@ commander.register('doc', function(args)
|
|||
local globalDesc = [[
|
||||
These are the global Hilbish functions that are always available and not part of a module.]]
|
||||
if #args > 0 then
|
||||
local mod = table.concat(args, ' '):gsub('^%s*(.-)%s*$', '%1')
|
||||
local mod = ''
|
||||
for i = 1, #args do
|
||||
mod = mod .. tostring(args[i]) .. ' '
|
||||
end
|
||||
mod = mod:gsub('^%s*(.-)%s*$', '%1')
|
||||
|
||||
local f = io.open(moddocPath .. mod .. '.txt', 'rb')
|
||||
if not f then
|
||||
|
@ -66,16 +69,14 @@ These are the global Hilbish functions that are always available and not part of
|
|||
if backtickOccurence % 2 == 0 then
|
||||
return '{reset}'
|
||||
else
|
||||
return '{underline}{green}'
|
||||
return '{invert}'
|
||||
end
|
||||
end)))
|
||||
f:close()
|
||||
|
||||
return
|
||||
end
|
||||
local modules = table.map(fs.readdir(moddocPath), function(f)
|
||||
return lunacolors.underline(lunacolors.blue(f:sub(1, -5)))
|
||||
end)
|
||||
local modules = fs.readdir(moddocPath)
|
||||
|
||||
io.write [[
|
||||
Welcome to Hilbish's doc tool! Here you can find documentation for builtin
|
||||
|
@ -85,7 +86,11 @@ Usage: doc <module>
|
|||
|
||||
Available modules: ]]
|
||||
|
||||
print(table.concat(modules, ', '))
|
||||
local mods = ''
|
||||
for i = 1, #modules do
|
||||
mods = mods .. tostring(modules[i]):gsub('.txt', '') .. ', '
|
||||
end
|
||||
print(mods)
|
||||
|
||||
return
|
||||
end)
|
||||
|
@ -126,40 +131,23 @@ do
|
|||
end)
|
||||
end
|
||||
|
||||
commander.register('cdr', function(args)
|
||||
if not args[1] then
|
||||
print(lunacolors.format [[
|
||||
cdr: change directory to one which has been recently visied
|
||||
-- Function additions to Lua standard library
|
||||
function string.split(str, delimiter)
|
||||
local result = {}
|
||||
local from = 1
|
||||
|
||||
usage: cdr <index>
|
||||
local delim_from, delim_to = string.find(str, delimiter, from)
|
||||
|
||||
to get a list of recent directories, use {green}{underline}cdr list{reset}]])
|
||||
return
|
||||
while delim_from do
|
||||
table.insert(result, string.sub(str, from, delim_from - 1))
|
||||
from = delim_to + 1
|
||||
delim_from, delim_to = string.find(str, delimiter, from)
|
||||
end
|
||||
|
||||
if args[1] == 'list' then
|
||||
if #recentDirs == 0 then
|
||||
print 'No directories have been visited.'
|
||||
return 1
|
||||
end
|
||||
print(table.concat(recentDirs, '\n'))
|
||||
return
|
||||
end
|
||||
table.insert(result, string.sub(str, from))
|
||||
|
||||
local index = tonumber(args[1])
|
||||
if not index then
|
||||
print(string.format('received %s as index, which isn\'t a number', index))
|
||||
return 1
|
||||
end
|
||||
|
||||
if not recentDirs[index] then
|
||||
print(string.format('no recent directory found at index %s', index))
|
||||
return 1
|
||||
end
|
||||
|
||||
fs.cd(recentDirs[index])
|
||||
return
|
||||
end)
|
||||
return result
|
||||
end
|
||||
|
||||
-- Hook handles
|
||||
bait.catch('command.not-found', function(cmd)
|
||||
|
|
7
shell.go
7
shell.go
|
@ -55,16 +55,11 @@ func RunInput(input string) {
|
|||
return
|
||||
}
|
||||
if commands[cmdArgs[0]] != nil {
|
||||
luacmdArgs := l.NewTable()
|
||||
for _, str := range cmdArgs[1:] {
|
||||
luacmdArgs.Append(lua.LString(str))
|
||||
}
|
||||
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: commands[cmdArgs[0]],
|
||||
NRet: 1,
|
||||
Protect: true,
|
||||
}, luacmdArgs)
|
||||
}, luar.New(l, cmdArgs[1:]))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr,
|
||||
"Error in command:\n\n" + err.Error())
|
||||
|
|
3
vars.go
3
vars.go
|
@ -2,10 +2,9 @@ package main
|
|||
|
||||
// String vars that are free to be changed at compile time
|
||||
var (
|
||||
version = "v0.6.0"
|
||||
version = "v0.5.1"
|
||||
defaultConfDir = "" // ~ will be substituted for home, path for user's default config
|
||||
defaultHistDir = ""
|
||||
commonRequirePaths = "';./libs/?/init.lua;./?/init.lua;./?/?.lua'"
|
||||
|
||||
prompt string // Prompt will always get changed anyway
|
||||
multilinePrompt = "> "
|
||||
|
|
|
@ -4,10 +4,9 @@ package main
|
|||
|
||||
// String vars that are free to be changed at compile time
|
||||
var (
|
||||
requirePaths = commonRequirePaths + `
|
||||
requirePaths = `';./libs/?/?.lua;./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
||||
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
||||
.. ';/usr/share/hilbish/libs/?/?.lua;'` + linuxUserPaths
|
||||
linuxUserPaths = `
|
||||
.. ';/usr/share/hilbish/libs/?/?.lua;'
|
||||
.. hilbish.xdg.data .. '/hilbish/libs/?/init.lua;'
|
||||
.. hilbish.xdg.data .. '/hilbish/libs/?/?.lua;'
|
||||
.. hilbish.xdg.data .. '/hilbish/libs/?.lua'
|
||||
|
|
|
@ -4,7 +4,7 @@ package main
|
|||
|
||||
// String vars that are free to be changed at compile time
|
||||
var (
|
||||
requirePaths = commonRequirePaths + `
|
||||
requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
||||
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;'
|
||||
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'`
|
||||
dataDir = "~\\Appdata\\Roaming\\Hilbish" // ~ and \ gonna cry?
|
||||
|
|
Loading…
Reference in New Issue