From fe08446d84e0aa939780ad013f0545778703da6d Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Fri, 19 Jul 2019 14:10:39 -0400 Subject: MBS as default shell --- .mbs/modules/lua.lua | 58 +++++++++++++++++++++++++++++++++ .mbs/modules/pager.lua | 48 ++++++++++++++++++++++++++++ .mbs/modules/readline.lua | 66 ++++++++++++++++++++++++++++++++++++++ .mbs/modules/shell.lua | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 .mbs/modules/lua.lua create mode 100644 .mbs/modules/pager.lua create mode 100644 .mbs/modules/readline.lua create mode 100644 .mbs/modules/shell.lua (limited to '.mbs/modules') diff --git a/.mbs/modules/lua.lua b/.mbs/modules/lua.lua new file mode 100644 index 0000000..d9453e6 --- /dev/null +++ b/.mbs/modules/lua.lua @@ -0,0 +1,58 @@ +local function lib_load(path, name) + if not _G[name] then + os.loadAPI(fs.combine(path, "lib/" .. name .. ".lua")) + if not _G[name] then _G[name] = _G[name .. ".lua"] end + end +end + +return { + description = "Replaces the Lua REPL with an advanced version.", + + dependencies = { + "bin/lua.lua", + "lib/stack_trace.lua" + }, + + -- When updating the defaults, one should also update bin/lua.lua + settings = { + { + name = "mbs.lua.enabled", + description = "Whether the extended Lua REPL is enabled.", + default = true, + }, + { + name = "mbs.lua.history_file", + description = "The file to save history to. Set to false to disable.", + default = ".lua_history", + }, + { + name = "mbs.lua.history_max", + description = "The maximum size of the history file", + default = 1e4, + }, + { + name = "mbs.lua.traceback", + description = "Show an error traceback when an input errors", + default = true, + }, + { + name= "mbs.lua.pretty_height", + description = "The height to fit the pretty-printer output to. Set to " + .. "false to disable, true to use the terminal height or a number for a constant height.", + default = true, + }, + { + name= "mbs.lua.highlight", + description = "Whether to apply syntax highlighting to the REPL's input.", + default = true, + }, + }, + + enabled = function() return settings.get("mbs.lua.enabled") end, + + setup = function(path) + lib_load(path, "stack_trace") + + shell.setAlias("lua", "/" .. fs.combine(path, "bin/lua.lua")) + end +} \ No newline at end of file diff --git a/.mbs/modules/pager.lua b/.mbs/modules/pager.lua new file mode 100644 index 0000000..7c25afb --- /dev/null +++ b/.mbs/modules/pager.lua @@ -0,0 +1,48 @@ +return { + description = "Replaces the textutils pagers with something akin to less", + + dependencies = { + "bin/help.lua" + }, + + settings = { + { + name = "mbs.pager.enabled", + description = "Whether the alternative pager is enabled.", + default = true, + }, + { + name = "mbs.pager.mode", + description = "The mode for the alternative pager.", + default = "default", + } + }, + + enabled = function() return settings.get("mbs.pager.enabled") end, + + setup = function(path) + shell.setAlias("help", "/" .. fs.combine(path, "bin/help.lua")) + shell.setCompletionFunction(fs.combine(path, "bin/help.lua"), function(shell, index, text, previous) + if index == 1 then return help.completeTopic(text) end + end) + + local native_pprint, native_ptabulate = textutils.pagedPrint, textutils.pagedTabulate + textutils.pagedPrint = function(text, free_lines) + local mode = settings.get("mbs.pager.mode") + if mode == "none" then + return io.write(text .. "\n") + else + return native_pprint(text, free_lines) + end + end + + textutils.pagedTabulate = function(...) + local mode = settings.get("mbs.pager.mode") + if mode == "none" then + return textutils.tabulate(...) + else + return native_ptabulate(...) + end + end + end +} \ No newline at end of file diff --git a/.mbs/modules/readline.lua b/.mbs/modules/readline.lua new file mode 100644 index 0000000..4dea9eb --- /dev/null +++ b/.mbs/modules/readline.lua @@ -0,0 +1,66 @@ +--- Additional readline + +local function lib_load(path, name) + if not _G[name] then + os.loadAPI(fs.combine(path, "lib/" .. name .. ".lua")) + if not _G[name] then _G[name] = _G[name .. ".lua"] end + end +end + +return { + description = + "This module extends the default read function, adding keybindings similar to " .. + "those provided by Emacs or GNU readline as well as additional configuration options.", + + dependencies = { + "lib/readline.lua", + }, + + settings = { + { + name = "mbs.readline.enabled", + description = "Whether the readline module is enabled.", + default = true, + }, + { + name = "mbs.readline.complete_bg", + description = "The background colour for completions.", + default = "none", + }, + { + name = "mbs.readline.complete_fg", + description = "The foreground colour for completions.", + default = "grey", + } + }, + + enabled = function() return settings.get("mbs.readline.enabled") end, + + setup = function(path) + lib_load(path, "readline") + + -- Replace the default read function + _G.read = function(replace_char, history, complete, default) + if replace_char ~= nil and type(replace_char) ~= "string" then + error("bad argument #1 (expected string, got " .. type(replace_char) .. ")", 2) + end + if history ~= nil and type(history) ~= "table" then + error("bad argument #2 (expected table, got " .. type(history) .. ")", 2) + end + if complete ~= nil and type(complete) ~= "function" then + error("bad argument #3 (expected function, got " .. type(complete) .. ")", 2) + end + if default ~= nil and type(default) ~= "string" then + error("bad argument #4 (expected string, got " .. type(default) .. ")", 2) + end + + return readline.read { + replace_char = replace_char, + history = history, + complete = complete, + default = default, + } + end + + end, +} \ No newline at end of file diff --git a/.mbs/modules/shell.lua b/.mbs/modules/shell.lua new file mode 100644 index 0000000..035322d --- /dev/null +++ b/.mbs/modules/shell.lua @@ -0,0 +1,81 @@ +local function lib_load(path, name) + if not _G[name] then + os.loadAPI(fs.combine(path, "lib/" .. name .. ".lua")) + if not _G[name] then _G[name] = _G[name .. ".lua"] end + end +end + +return { + description = "Replaces the shell with an advanced version.", + + dependencies = { + "bin/clear.lua", + "bin/shell.lua", + "lib/blit_window.lua", + "lib/scroll_window.lua", + "lib/stack_trace.lua", + }, + + -- When updating the defaults, one should also update bin/shell.lua + settings = { + { + name = "mbs.shell.enabled", + description = "Whether the extended shell is enabled.", + default = true, + }, + { + name = "mbs.shell.history_file", + description = "The file to save history to. Set to false to disable.", + default = ".shell_history", + }, + { + name = "mbs.shell.history_max", + description = "The maximum size of the history file", + default = 1e4, + }, + { + name = "mbs.shell.scroll_max", + description = "The maximum size of the scrollback", + default = 1e3, + }, + { + name = "mbs.shell.traceback", + description = "Show an error traceback when a program errors", + default = true, + }, + { + name = "mbs.shell.require_path", + description = "The path from that require will use by default. Set to false to use the CraftOS default.", + default = false, + }, + { + name = "mbs.shell.strict_globals", + description = "When set to true the shell will throw errors when programs attempt to define new globals in their environment. If you really want globals then you should use _G instead.", + default = false, + }, + }, + + enabled = function() return settings.get("mbs.shell.enabled") end, + + setup = function(path) + lib_load(path, "scroll_window") + lib_load(path, "blit_window") + lib_load(path, "stack_trace") + + shell.setAlias("shell", "/" .. fs.combine(path, "bin/shell.lua")) + shell.setAlias("clear", "/" .. fs.combine(path, "bin/clear.lua")) + + shell.setCompletionFunction(fs.combine(path, "bin/shell.lua"), function(shell, index, text, previous) + if index == 1 then return shell.completeProgram(text) end + end) + end, + + startup = function(path) + local fn, err = loadfile(fs.combine(path, "bin/shell.lua"), _ENV) + if not fn then error(err) end + + fn() + + shell.exit() + end, +} \ No newline at end of file -- cgit v1.2.3