-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.lua
More file actions
203 lines (180 loc) · 4.83 KB
/
code.lua
File metadata and controls
203 lines (180 loc) · 4.83 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
local OPTION_SHORT = 1
local OPTION_LONG = 2
local OPTION_PARAM = 3
local OPTION_TYPE = 4
local OPTION_HELP = 5
local OPTIONS = {
{ "u", "update", nil, nil, {
"Update to the latest version.",
} },
{ nil, "line", "index", "integer", {
"Start at the specified line.",
} },
{ "v", "version", nil, nil, {
"Print version (git commit hash).",
} },
{ "h", "help", nil, nil, {
"Print usage.",
} },
}
local optionMap = {}
for _, option in ipairs(OPTIONS) do
if option[OPTION_SHORT] then
optionMap[option[OPTION_SHORT]] = option
end
optionMap[option[OPTION_LONG]] = option
end
local function printUsage(asError)
local formattedOptions = {}
local maxWidth = 0
for _, option in ipairs(OPTIONS) do
local text = " "
if option[OPTION_SHORT] then
text = text .. "-" .. option[OPTION_SHORT] .. " "
end
text = text .. "--" .. option[OPTION_LONG]
if option[OPTION_PARAM] then
text = text .. " <" .. option[OPTION_PARAM] .. ">"
end
table.insert(formattedOptions, text)
maxWidth = math.max(maxWidth, #text)
end
-- add space between longest option and help text
maxWidth = maxWidth + 1
local defaultColor = asError and colors.red or colors.white
local highlightColor = asError and colors.pink or colors.lightBlue
term.setTextColor(defaultColor)
print("Usage:")
term.setTextColor(highlightColor)
print(" " .. arg[0] .. " [options] [filename]")
term.setTextColor(defaultColor)
print()
print("Options:")
for i, option in ipairs(OPTIONS) do
local helpLines = option[OPTION_HELP]
term.setTextColor(highlightColor)
write(("%-" .. maxWidth .. "s"):format(formattedOptions[i]))
term.setTextColor(defaultColor)
local lastPrintLines = print(helpLines[1])
for j = 2, #helpLines do
lastPrintLines = print((" "):rep(maxWidth) .. helpLines[j])
end
if lastPrintLines > 1 and i < #OPTIONS then
print()
end
end
end
local function parseArgs(...)
local raw = { ... }
local args = {}
local allValid = true
local i = 1
local function add(name, canSkip)
local option = optionMap[name]
if option then
local long = option[OPTION_LONG]
local optionType = option[OPTION_TYPE]
if optionType then
if not canSkip then
printError("Unknown option: " .. name)
allValid = false
else
i = i + 1
local param = raw[i]
if optionType == "integer" then
local number = tonumber(param)
local integer = number and number % 1 == 0 and number
if integer then
args[long] = integer
else
printError(name .. ": integer expected, found " .. param)
allValid = false
end
elseif optionType == "number" then
local number = tonumber(param)
if number then
args[long] = number
else
printError(name .. ": number expected, found " .. param)
allValid = false
end
elseif optionType == "string" then
args[long] = param
else
error("unexpected option type: " .. tostring(optionType))
end
end
else
args[long] = true
end
else
printError("Unknown option: " .. name)
allValid = false
end
end
while i <= #raw do
local arg = raw[i]
local long = arg:match("^%-%-(.*)")
if long == "" then
printError("\"--\" is not allowed")
allValid = false
elseif long then
add(long, true)
else
local shorts = arg:match("^%-(.*)")
if shorts == "" then
printError("\"-\" is not allowed")
allValid = false
elseif shorts then
for j = 1, #shorts do
add(shorts:sub(j, j), j == #shorts)
end
else
table.insert(args, arg)
end
end
i = i + 1
end
return allValid and args or nil
end
local function printVersion()
printError("not yet implemented: --version")
end
local function update()
shell.run("wget run https://raw.githubusercontent.com/Possseidon/cc-code/main/code/update.lua")
end
local function run(args)
local filename = args[1]
local code = require "code.Code" (filename)
if args.line then
code._editor:setCursor(1, args.line)
code._editor:makeCursorVisible()
end
local oldTextColor = term.getTextColor()
local oldBackgroundColor = term.getBackgroundColor()
code:run()
term.setTextColor(oldTextColor)
term.setBackgroundColor(oldBackgroundColor)
term.clear()
term.setCursorPos(1, 1)
if code._updateOnClose then
update()
end
end
local args = parseArgs(...)
if not args then
print()
printUsage(true)
return
end
if args.help then
printUsage()
elseif args.version then
printVersion()
elseif args.update then
update()
elseif #args == 1 then
run(args)
else
printUsage(true)
end