aboutsummaryrefslogtreecommitdiff
path: root/.mbs/modules/shell.lua
blob: 035322dfb3cc785069467ed7279a130426211b62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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,
}