-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.lua
More file actions
173 lines (153 loc) · 5.21 KB
/
settings.lua
File metadata and controls
173 lines (153 loc) · 5.21 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local expect = require "cc.expect"
local fs = require "fs"
local textutils = require "textutils"
local settings = {}
local store = {}
local definitions = {}
function settings.get(name, default)
expect(1, name, "string")
if store[name] ~= nil then return store[name]
elseif default ~= nil then return default
else return definitions[name] and definitions[name].default end
end
function settings.set(name, value)
expect(1, name, "string")
if value == nil then error("bad argument #2 (expected value, got nil)", 2) end
if definitions[name] and definitions[name].type then expect(2, value, definitions[name].value) end
store[name] = value
end
function settings.unset(name)
expect(1, name, "string")
store[name] = nil
end
function settings.define(name, opts)
expect(1, name, "string")
if opts ~= nil then
expect(2, opts, "table")
expect.field(opts, "description", "string", "nil")
expect.field(opts, "type", "string", "nil")
if opts.type then expect.field(opts, "default", opts.type, "nil") end
end
definitions[name] = opts or {}
end
function settings.undefine(name)
expect(1, name, "string")
definitions[name] = nil
end
function settings.getDetails(name)
expect(1, name, "string")
local t = definitions[name]
if t then return {description = t.description, default = t.default, type = t.type, value = store[name]}
else return {value = store[name]} end
end
function settings.clear()
store = {}
end
function settings.getNames()
local retval = {}
for k in pairs(definitions) do retval[#retval+1] = k end
table.sort(retval)
return retval
end
function settings.load(path)
path = expect(1, path, "string", "nil") or "/etc/settings"
if not fs.exists(path) then return false end
local file = fs.open(path, "r")
if not file then return false end
local ok, data = pcall(textutils.unserialize, file.readAll())
file.close()
if not ok or type(data) ~= "table" then return false end
for k, v in pairs(data) do store[k] = v end
return true
end
function settings.save(path)
path = expect(1, path, "string", "nil") or "/etc/settings"
local file = fs.open(path, "r")
if not file then return false end
file.write(textutils.serialize(store))
file.close()
return true
end
-- Set default settings
settings.define("shell.allow_startup", {
default = true,
description = "Run startup files when the computer turns on.",
type = "boolean",
})
settings.define("shell.allow_disk_startup", {
default = true,
description = "Run startup files from disk drives when the computer turns on.",
type = "boolean",
})
settings.define("shell.autocomplete", {
default = false,
description = "Autocomplete program and arguments in the shell.",
type = "boolean",
})
settings.define("edit.autocomplete", {
default = false,
description = "Autocomplete API and function names in the editor.",
type = "boolean",
})
settings.define("lua.autocomplete", {
default = false,
description = "Autocomplete API and function names in the Lua REPL.",
type = "boolean",
})
settings.define("edit.default_extension", {
default = "lua",
description = [[The file extension the editor will use if none is given. Set to "" to disable.]],
type = "string",
})
settings.define("paint.default_extension", {
default = "nfp",
description = [[The file extension the paint program will use if none is given. Set to "" to disable.]],
type = "string",
})
settings.define("list.show_hidden", {
default = false,
description = [[Show hidden files (those starting with "." in the Lua REPL).]],
type = "boolean",
})
settings.define("motd.enable", {
default = true,
description = "Display a random message when the computer starts up.",
type = "boolean",
})
settings.define("motd.path", {
default = "/rom/motd.txt:/motd.txt",
description = [[The path to load random messages from. Should be a colon (":") separated string of file paths.]],
type = "string",
})
settings.define("lua.warn_against_use_of_local", {
default = true,
description = [[Print a message when input in the Lua REPL starts with the word 'local'. Local variables defined in the Lua REPL are be inaccessable on the next input.]],
type = "boolean",
})
settings.define("lua.function_args", {
default = true,
description = "Show function arguments when printing functions.",
type = "boolean",
})
settings.define("lua.function_source", {
default = false,
description = "Show where a function was defined when printing functions.",
type = "boolean",
})
settings.define("bios.strict_globals", {
default = false,
description = "Prevents assigning variables into a program's environment. Make sure you use the local keyword or assign to _G explicitly.",
type = "boolean",
})
settings.define("shell.autocomplete_hidden", {
default = false,
description = [[Autocomplete hidden files and folders (those starting with ".").]],
type = "boolean",
})
settings.define("bios.use_multishell", {
default = true,
description = [[Allow running multiple programs at once, through the use of the "fg" and "bg" programs.]],
type = "boolean",
})
settings.load()
return settings