mirror of https://github.com/Hilbis/Hilbish
Compare commits
25 Commits
6b065dc035
...
a8ecd44efb
Author | SHA1 | Date |
---|---|---|
sammyette | a8ecd44efb | |
sammyette | ce4ba48da0 | |
sammyette | 7a4cbbddff | |
sammyette | 72194898ba | |
sammyette | 3bd71828fa | |
sammyette | 7615e56ffd | |
sammyette | d51ae7d310 | |
sammyette | 3042fce50e | |
sammyette | 9b6ca1faf4 | |
sammyette | b97e1efa8d | |
sammyette | 86013e6576 | |
sammyette | 51a68e24cf | |
sammyette | c95fb05fbf | |
sammyette | 006f0f986e | |
sammyette | 7769d68859 | |
sammyette | 586566c595 | |
sammyette | 1e76b1501f | |
sammyette | 7d9d3e4d72 | |
sammyette | 37610ad8b0 | |
sammyette | 6271a7fc18 | |
sammyette | ad698b1a03 | |
sammyette | 1b79abdab7 | |
sammyette | 7a3d9022e9 | |
sammyette | 3d53e85fc9 | |
sammyette | 85b347d5f3 |
|
@ -1,3 +1,9 @@
|
||||||
[submodule "libs/lunacolors"]
|
[submodule "libs/lunacolors"]
|
||||||
path = libs/lunacolors
|
path = libs/lunacolors
|
||||||
url = https://github.com/Hilbis/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
|
||||||
|
|
|
@ -2,6 +2,42 @@
|
||||||
|
|
||||||
This is the changelog for the Hilbish shell made in Go and Lua.
|
This is the changelog for the Hilbish shell made in Go and Lua.
|
||||||
|
|
||||||
|
## [0.6.0] - 2021-10-17
|
||||||
|
## Added
|
||||||
|
- Hilbish will expand `~` in the preloadPath and samplePathConf variables. These are for compile time.
|
||||||
|
- On Windows, the hostname in `%u` has been removed.
|
||||||
|
- Made it easier to compile on Windows by adding Windows-tailored vars and paths.
|
||||||
|
- Add require paths `./libs/?/?.lua`
|
||||||
|
- Hilbish will now respect $XDG_CONFIG_HOME and will load its config and history there first and use Lua libraries in there and $XDG_DATA_HOME if they are set. (#71)
|
||||||
|
- If not, Hilbish will still default to `~`
|
||||||
|
- Added some new hooks
|
||||||
|
- `command.precmd` is thrown right before Hilbish prompts for input
|
||||||
|
- `command.preexec` is thrown right before Hilbish executes a command. It passes 2 arguments: the command as the user typed, and what Hilbish will actually execute (resolved alias)
|
||||||
|
- `hilbish.dataDir` is now available to know the directory of Hilbish data files (default config, docs, preload, etc)
|
||||||
|
- A `docgen` program has been added to `cmd/docgen` in the GitHub repository, As the name suggests, it will output docs in a `docs` folder for functions implemented in Go
|
||||||
|
- All hilbish modules/libraries now have a `__doc` metatable entry which is simply a short description of the module.
|
||||||
|
- `fs.readdir(dir)` has been added. It will return a table of files in `dir`
|
||||||
|
- Errors in the `fs.mkdir` function are now handled.
|
||||||
|
- **Breaking Change:** `fs.cd` no longer returns a numeric code to indicate error. Instead, it returns an error message.
|
||||||
|
- The `doc` command has been added to document functions of Hilbish libraries. Run the command for more details.
|
||||||
|
- `link(url, text)` has been added to `ansikit`. It returns a string which can be printed to produce a hyperlink in a terminal. Note that not all terminals support this feature.
|
||||||
|
- The [Succulent](https://github.com/Rosettea/Succulent) library has been added. This includes more utility functions and expansions to the Lua standard library itself.
|
||||||
|
- The command string is now passed to the `command.exit` hook
|
||||||
|
|
||||||
|
# Changed
|
||||||
|
- Hilbish won't print an extra newline at exit with ctrl + d
|
||||||
|
- `command.exit` with 0 exit code will now be thrown if input is nothing
|
||||||
|
- **Breaking Change:** `fs.stat` has been made better. It returns a proper table instead of userdata, and has fields instead of functions
|
||||||
|
- It includes `name`, `mode` as a octal representation in a string, `isDir`, and `size`
|
||||||
|
|
||||||
|
# Fixed
|
||||||
|
- `timeout()` is now blocking
|
||||||
|
- Directories with spaces in them can now be `cd`'d to
|
||||||
|
- An alias with the same name as the command will now not cause a freeze (#73)
|
||||||
|
- Userdata is no longer returned in the following cases:
|
||||||
|
- Commander arguments
|
||||||
|
- `fs` functions
|
||||||
|
|
||||||
## [0.5.1] - 2021-06-16
|
## [0.5.1] - 2021-06-16
|
||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
@ -203,6 +239,7 @@ This input for example will prompt for more input to complete:
|
||||||
|
|
||||||
First "stable" release of Hilbish.
|
First "stable" release of Hilbish.
|
||||||
|
|
||||||
|
[0.6.0]: https://github.com/Rosettea/Hilbish/compare/v0.5.1...v0.6.0
|
||||||
[0.5.1]: https://github.com/Rosettea/Hilbish/compare/v0.5.0...v0.5.1
|
[0.5.1]: https://github.com/Rosettea/Hilbish/compare/v0.5.0...v0.5.1
|
||||||
[0.5.0]: https://github.com/Rosettea/Hilbish/compare/v0.4.0...v0.5.0
|
[0.5.0]: https://github.com/Rosettea/Hilbish/compare/v0.4.0...v0.5.0
|
||||||
[0.4.0]: https://github.com/Rosettea/Hilbish/compare/v0.3.2...v0.4.0
|
[0.4.0]: https://github.com/Rosettea/Hilbish/compare/v0.3.2...v0.4.0
|
|
@ -43,6 +43,7 @@ func main() {
|
||||||
"fs": "f",
|
"fs": "f",
|
||||||
"commander": "c",
|
"commander": "c",
|
||||||
"bait": "b",
|
"bait": "b",
|
||||||
|
"terminal": "term",
|
||||||
}
|
}
|
||||||
docs := make(map[string][]string)
|
docs := make(map[string][]string)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
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,7 +9,6 @@ import (
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
"layeh.com/gopher-luar"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Loader(L *lua.LState) int {
|
func Loader(L *lua.LState) int {
|
||||||
|
@ -23,10 +22,6 @@ addition to the Lua standard library's I/O and fs functions.`)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func luaErr(L *lua.LState, msg string) {
|
|
||||||
L.Error(lua.LString(msg), 2)
|
|
||||||
}
|
|
||||||
|
|
||||||
var exports = map[string]lua.LGFunction{
|
var exports = map[string]lua.LGFunction{
|
||||||
"cd": fcd,
|
"cd": fcd,
|
||||||
"mkdir": fmkdir,
|
"mkdir": fmkdir,
|
||||||
|
@ -42,7 +37,7 @@ func fcd(L *lua.LState) int {
|
||||||
err := os.Chdir(strings.TrimSpace(path))
|
err := os.Chdir(strings.TrimSpace(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e := err.(*os.PathError).Err.Error()
|
e := err.(*os.PathError).Err.Error()
|
||||||
luaErr(L, e)
|
L.RaiseError(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -62,7 +57,7 @@ func fmkdir(L *lua.LState) int {
|
||||||
err = os.Mkdir(path, 0744)
|
err = os.Mkdir(path, 0744)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
luaErr(L, err.Error())
|
L.RaiseError(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -75,7 +70,7 @@ func fstat(L *lua.LState) int {
|
||||||
|
|
||||||
pathinfo, err := os.Stat(path)
|
pathinfo, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
luaErr(L, err.Error())
|
L.RaiseError(err.Error())
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
statTbl := L.NewTable()
|
statTbl := L.NewTable()
|
||||||
|
@ -92,18 +87,18 @@ func fstat(L *lua.LState) int {
|
||||||
// Returns a table of files in `dir`
|
// Returns a table of files in `dir`
|
||||||
func freaddir(L *lua.LState) int {
|
func freaddir(L *lua.LState) int {
|
||||||
dir := L.CheckString(1)
|
dir := L.CheckString(1)
|
||||||
names := []string{}
|
names := L.NewTable()
|
||||||
|
|
||||||
dirEntries, err := os.ReadDir(dir)
|
dirEntries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
luaErr(L, err.Error())
|
L.RaiseError(err.Error())
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
for _, entry := range dirEntries {
|
for _, entry := range dirEntries {
|
||||||
names = append(names, entry.Name())
|
names.Append(lua.LString(entry.Name()))
|
||||||
}
|
}
|
||||||
|
|
||||||
L.Push(luar.New(L, names))
|
L.Push(names)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
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,6 +35,7 @@ func HilbishLoader(L *lua.LState) int {
|
||||||
L.SetField(mod, "user", lua.LString(username))
|
L.SetField(mod, "user", lua.LString(username))
|
||||||
L.SetField(mod, "host", lua.LString(host))
|
L.SetField(mod, "host", lua.LString(host))
|
||||||
L.SetField(mod, "home", lua.LString(homedir))
|
L.SetField(mod, "home", lua.LString(homedir))
|
||||||
|
L.SetField(mod, "dataDir", lua.LString(dataDir))
|
||||||
|
|
||||||
xdg := L.NewTable()
|
xdg := L.NewTable()
|
||||||
L.SetField(xdg, "config", lua.LString(confDir))
|
L.SetField(xdg, "config", lua.LString(confDir))
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
-- We're basically porting Ansikit to lua
|
-- We're basically porting Ansikit to lua
|
||||||
-- https://github.com/Luvella/AnsiKit/blob/master/lib/index.js
|
-- https://github.com/Luvella/AnsiKit/blob/master/lib/index.js
|
||||||
-- which is made by yours truly sammy :^)
|
-- which is made by yours truly sammy :^)
|
||||||
|
local lunacolors = require 'lunacolors'
|
||||||
local ansikit = {}
|
local ansikit = {}
|
||||||
|
|
||||||
ansikit.clear = function(scrollback)
|
ansikit.clear = function(scrollback)
|
||||||
|
@ -77,6 +77,12 @@ ansikit.hideCursor = function()
|
||||||
return ansikit.printCSI('?25', 'l')
|
return ansikit.printCSI('?25', 'l')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ansikit.link = function(url, text)
|
||||||
|
if not url then error 'ansikit: missing url for hyperlink' end
|
||||||
|
local text = (text and text or 'link')
|
||||||
|
return lunacolors.blue('\27]8;;' .. url .. '\27\\' .. text .. '\27]8;;\27\\\n')
|
||||||
|
end
|
||||||
|
|
||||||
ansikit.print = function(text)
|
ansikit.print = function(text)
|
||||||
io.write(ansikit.format(text))
|
io.write(ansikit.format(text))
|
||||||
return ansikit
|
return ansikit
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 96dc95c24989be6596ccb9f7f7244d37f03d4acd
|
4
lua.go
4
lua.go
|
@ -11,6 +11,7 @@ import (
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
"hilbish/golibs/commander"
|
"hilbish/golibs/commander"
|
||||||
"hilbish/golibs/fs"
|
"hilbish/golibs/fs"
|
||||||
|
"hilbish/golibs/terminal"
|
||||||
|
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
"layeh.com/gopher-luar"
|
"layeh.com/gopher-luar"
|
||||||
|
@ -40,8 +41,9 @@ func LuaInit() {
|
||||||
l.PreloadModule("hilbish", HilbishLoader)
|
l.PreloadModule("hilbish", HilbishLoader)
|
||||||
l.DoString("hilbish = require 'hilbish'")
|
l.DoString("hilbish = require 'hilbish'")
|
||||||
|
|
||||||
// Add fs module to Lua
|
// Add fs and terminal module module to Lua
|
||||||
l.PreloadModule("fs", fs.Loader)
|
l.PreloadModule("fs", fs.Loader)
|
||||||
|
l.PreloadModule("terminal", terminal.Loader)
|
||||||
|
|
||||||
cmds := commander.New()
|
cmds := commander.New()
|
||||||
// When a command from Lua is added, register it for use
|
// When a command from Lua is added, register it for use
|
||||||
|
|
1
main.go
1
main.go
|
@ -175,7 +175,6 @@ input:
|
||||||
|
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
// Exit if user presses ^D (ctrl + d)
|
// Exit if user presses ^D (ctrl + d)
|
||||||
fmt.Println("")
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
78
preload.lua
78
preload.lua
|
@ -3,19 +3,17 @@
|
||||||
local fs = require 'fs'
|
local fs = require 'fs'
|
||||||
local commander = require 'commander'
|
local commander = require 'commander'
|
||||||
local bait = require 'bait'
|
local bait = require 'bait'
|
||||||
|
require 'succulent' -- Function additions
|
||||||
local oldDir = hilbish.cwd()
|
local oldDir = hilbish.cwd()
|
||||||
|
|
||||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||||
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
|
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
|
||||||
|
|
||||||
-- Builtins
|
-- Builtins
|
||||||
|
local recentDirs = {}
|
||||||
commander.register('cd', function (args)
|
commander.register('cd', function (args)
|
||||||
if #args > 0 then
|
if #args > 0 then
|
||||||
local path = ''
|
local path = table.concat(args, ' '):gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
|
||||||
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')
|
:gsub('$([%w_]+)', os.getenv):gsub('%z','$'):gsub('^%s*(.-)%s*$', '%1')
|
||||||
|
|
||||||
if path == '-' then
|
if path == '-' then
|
||||||
|
@ -26,12 +24,15 @@ commander.register('cd', function (args)
|
||||||
|
|
||||||
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
|
||||||
if err == 1 then
|
print(err:sub(17))
|
||||||
print('directory does not exist')
|
return 1
|
||||||
end
|
|
||||||
return err
|
|
||||||
end
|
end
|
||||||
bait.throw('cd', path)
|
bait.throw('cd', path)
|
||||||
|
|
||||||
|
-- add to table of recent dirs
|
||||||
|
table.insert(recentDirs, 1, path)
|
||||||
|
recentDirs[11] = nil
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
fs.cd(hilbish.home)
|
fs.cd(hilbish.home)
|
||||||
|
@ -49,11 +50,7 @@ commander.register('doc', function(args)
|
||||||
local globalDesc = [[
|
local globalDesc = [[
|
||||||
These are the global Hilbish functions that are always available and not part of a module.]]
|
These are the global Hilbish functions that are always available and not part of a module.]]
|
||||||
if #args > 0 then
|
if #args > 0 then
|
||||||
local mod = ''
|
local mod = table.concat(args, ' '):gsub('^%s*(.-)%s*$', '%1')
|
||||||
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')
|
local f = io.open(moddocPath .. mod .. '.txt', 'rb')
|
||||||
if not f then
|
if not f then
|
||||||
|
@ -69,14 +66,16 @@ These are the global Hilbish functions that are always available and not part of
|
||||||
if backtickOccurence % 2 == 0 then
|
if backtickOccurence % 2 == 0 then
|
||||||
return '{reset}'
|
return '{reset}'
|
||||||
else
|
else
|
||||||
return '{invert}'
|
return '{underline}{green}'
|
||||||
end
|
end
|
||||||
end)))
|
end)))
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local modules = fs.readdir(moddocPath)
|
local modules = table.map(fs.readdir(moddocPath), function(f)
|
||||||
|
return lunacolors.underline(lunacolors.blue(f:sub(1, -5)))
|
||||||
|
end)
|
||||||
|
|
||||||
io.write [[
|
io.write [[
|
||||||
Welcome to Hilbish's doc tool! Here you can find documentation for builtin
|
Welcome to Hilbish's doc tool! Here you can find documentation for builtin
|
||||||
|
@ -86,11 +85,7 @@ Usage: doc <module>
|
||||||
|
|
||||||
Available modules: ]]
|
Available modules: ]]
|
||||||
|
|
||||||
local mods = ''
|
print(table.concat(modules, ', '))
|
||||||
for i = 1, #modules do
|
|
||||||
mods = mods .. tostring(modules[i]):gsub('.txt', '') .. ', '
|
|
||||||
end
|
|
||||||
print(mods)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
end)
|
end)
|
||||||
|
@ -131,23 +126,40 @@ do
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Function additions to Lua standard library
|
commander.register('cdr', function(args)
|
||||||
function string.split(str, delimiter)
|
if not args[1] then
|
||||||
local result = {}
|
print(lunacolors.format [[
|
||||||
local from = 1
|
cdr: change directory to one which has been recently visied
|
||||||
|
|
||||||
local delim_from, delim_to = string.find(str, delimiter, from)
|
usage: cdr <index>
|
||||||
|
|
||||||
while delim_from do
|
to get a list of recent directories, use {green}{underline}cdr list{reset}]])
|
||||||
table.insert(result, string.sub(str, from, delim_from - 1))
|
return
|
||||||
from = delim_to + 1
|
|
||||||
delim_from, delim_to = string.find(str, delimiter, from)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
table.insert(result, string.sub(str, from))
|
if args[1] == 'list' then
|
||||||
|
if #recentDirs == 0 then
|
||||||
return result
|
print 'No directories have been visited.'
|
||||||
|
return 1
|
||||||
end
|
end
|
||||||
|
print(table.concat(recentDirs, '\n'))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
-- Hook handles
|
-- Hook handles
|
||||||
bait.catch('command.not-found', function(cmd)
|
bait.catch('command.not-found', function(cmd)
|
||||||
|
|
43
shell.go
43
shell.go
|
@ -10,7 +10,6 @@ import (
|
||||||
// "github.com/bobappleyard/readline"
|
// "github.com/bobappleyard/readline"
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
// "github.com/yuin/gopher-lua/parse"
|
// "github.com/yuin/gopher-lua/parse"
|
||||||
"layeh.com/gopher-luar"
|
|
||||||
"mvdan.cc/sh/v3/interp"
|
"mvdan.cc/sh/v3/interp"
|
||||||
"mvdan.cc/sh/v3/syntax"
|
"mvdan.cc/sh/v3/syntax"
|
||||||
)
|
)
|
||||||
|
@ -51,19 +50,24 @@ func RunInput(input string) {
|
||||||
err = l.PCall(0, lua.MultRet, nil)
|
err = l.PCall(0, lua.MultRet, nil)
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
hooks.Em.Emit("command.exit", 0)
|
cmdFinish(0, cmdString)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if commands[cmdArgs[0]] != nil {
|
if commands[cmdArgs[0]] != nil {
|
||||||
|
luacmdArgs := l.NewTable()
|
||||||
|
for _, str := range cmdArgs[1:] {
|
||||||
|
luacmdArgs.Append(lua.LString(str))
|
||||||
|
}
|
||||||
|
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: commands[cmdArgs[0]],
|
Fn: commands[cmdArgs[0]],
|
||||||
NRet: 1,
|
NRet: 1,
|
||||||
Protect: true,
|
Protect: true,
|
||||||
}, luar.New(l, cmdArgs[1:]))
|
}, luacmdArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr,
|
fmt.Fprintln(os.Stderr,
|
||||||
"Error in command:\n\n" + err.Error())
|
"Error in command:\n\n" + err.Error())
|
||||||
hooks.Em.Emit("command.exit", 1)
|
cmdFinish(1, cmdString)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
luaexitcode := l.Get(-1)
|
luaexitcode := l.Get(-1)
|
||||||
|
@ -75,7 +79,7 @@ func RunInput(input string) {
|
||||||
exitcode = uint8(code)
|
exitcode = uint8(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
hooks.Em.Emit("command.exit", exitcode)
|
cmdFinish(exitcode, cmdString)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,22 +97,22 @@ func RunInput(input string) {
|
||||||
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 {
|
||||||
hooks.Em.Emit("command.exit", code)
|
cmdFinish(code, cmdString)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
hooks.Em.Emit("command.exit", 1)
|
cmdFinish(1, cmdString)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if code, ok := interp.IsExitStatus(err); ok {
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
hooks.Em.Emit("command.exit", code)
|
cmdFinish(code, cmdString)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hooks.Em.Emit("command.exit", 0)
|
cmdFinish(0, cmdString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,20 +128,32 @@ func execCommand(cmd string) error {
|
||||||
_, argstring := splitInput(strings.Join(args, " "))
|
_, argstring := splitInput(strings.Join(args, " "))
|
||||||
|
|
||||||
// If alias was found, use command alias
|
// If alias was found, use command alias
|
||||||
if aliases[args[0]] != "" {
|
for aliases[args[0]] != "" {
|
||||||
alias := aliases[args[0]]
|
alias := aliases[args[0]]
|
||||||
argstring = alias + strings.TrimPrefix(argstring, args[0])
|
argstring = alias + strings.TrimPrefix(argstring, args[0])
|
||||||
cmdArgs, _ := splitInput(argstring)
|
cmdArgs, _ := splitInput(argstring)
|
||||||
args = cmdArgs
|
args = cmdArgs
|
||||||
|
|
||||||
|
if aliases[args[0]] == alias {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if aliases[args[0]] != "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If command is defined in Lua then run it
|
// If command is defined in Lua then run it
|
||||||
|
luacmdArgs := l.NewTable()
|
||||||
|
for _, str := range args[1:] {
|
||||||
|
luacmdArgs.Append(lua.LString(str))
|
||||||
|
}
|
||||||
|
|
||||||
if commands[args[0]] != nil {
|
if commands[args[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: commands[args[0]],
|
Fn: commands[args[0]],
|
||||||
NRet: 1,
|
NRet: 1,
|
||||||
Protect: true,
|
Protect: true,
|
||||||
}, luar.New(l, args[1:]))
|
}, luacmdArgs)
|
||||||
luaexitcode := l.Get(-1)
|
luaexitcode := l.Get(-1)
|
||||||
var exitcode uint8 = 0
|
var exitcode uint8 = 0
|
||||||
|
|
||||||
|
@ -151,7 +167,7 @@ func execCommand(cmd string) error {
|
||||||
fmt.Fprintln(os.Stderr,
|
fmt.Fprintln(os.Stderr,
|
||||||
"Error in command:\n\n" + err.Error())
|
"Error in command:\n\n" + err.Error())
|
||||||
}
|
}
|
||||||
hooks.Em.Emit("command.exit", exitcode)
|
cmdFinish(exitcode, argstring)
|
||||||
return interp.NewExitStatus(exitcode)
|
return interp.NewExitStatus(exitcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,3 +240,6 @@ func splitInput(input string) ([]string, string) {
|
||||||
return cmdArgs, cmdstr.String()
|
return cmdArgs, cmdstr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cmdFinish(code uint8, cmdstr string) {
|
||||||
|
hooks.Em.Emit("command.exit", code, cmdstr)
|
||||||
|
}
|
||||||
|
|
3
vars.go
3
vars.go
|
@ -2,9 +2,10 @@ package main
|
||||||
|
|
||||||
// String vars that are free to be changed at compile time
|
// String vars that are free to be changed at compile time
|
||||||
var (
|
var (
|
||||||
version = "v0.5.1"
|
version = "v0.6.0"
|
||||||
defaultConfDir = "" // ~ will be substituted for home, path for user's default config
|
defaultConfDir = "" // ~ will be substituted for home, path for user's default config
|
||||||
defaultHistDir = ""
|
defaultHistDir = ""
|
||||||
|
commonRequirePaths = "';./libs/?/init.lua;./?/init.lua;./?/?.lua'"
|
||||||
|
|
||||||
prompt string // Prompt will always get changed anyway
|
prompt string // Prompt will always get changed anyway
|
||||||
multilinePrompt = "> "
|
multilinePrompt = "> "
|
||||||
|
|
|
@ -4,9 +4,10 @@ package main
|
||||||
|
|
||||||
// String vars that are free to be changed at compile time
|
// String vars that are free to be changed at compile time
|
||||||
var (
|
var (
|
||||||
requirePaths = `';./libs/?/?.lua;./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
requirePaths = commonRequirePaths + `
|
||||||
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
||||||
.. ';/usr/share/hilbish/libs/?/?.lua;'
|
.. ';/usr/share/hilbish/libs/?/?.lua;'` + linuxUserPaths
|
||||||
|
linuxUserPaths = `
|
||||||
.. hilbish.xdg.data .. '/hilbish/libs/?/init.lua;'
|
.. hilbish.xdg.data .. '/hilbish/libs/?/init.lua;'
|
||||||
.. hilbish.xdg.data .. '/hilbish/libs/?/?.lua;'
|
.. hilbish.xdg.data .. '/hilbish/libs/?/?.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
|
// String vars that are free to be changed at compile time
|
||||||
var (
|
var (
|
||||||
requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
requirePaths = commonRequirePaths + `
|
||||||
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;'
|
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;'
|
||||||
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'`
|
.. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'`
|
||||||
dataDir = "~\\Appdata\\Roaming\\Hilbish" // ~ and \ gonna cry?
|
dataDir = "~\\Appdata\\Roaming\\Hilbish" // ~ and \ gonna cry?
|
||||||
|
|
Loading…
Reference in New Issue