This repository was archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccsh.lua
More file actions
176 lines (154 loc) · 4.35 KB
/
ccsh.lua
File metadata and controls
176 lines (154 loc) · 4.35 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
local args = {...}
local bExit = false
local format = "§f&0{:if shellFail then return '§e' else return '&9' end:}[@{:=os.computerID():}{:if shell.dir() ~= '' then return ' ' end:}{PATH}]§f&0 "
local issue = "§f&0&4{:=os.version():} + &9ccsh {:=ccsh.version:}§f&0"
local history = {}
local cols = {}
for i = 0, 15 do -- enums?
cols[string.format("%x", i)] = 2^i
end
if not fs.exists(".ccsh") then
fs.makeDir(".ccsh")
fs.makeDir(".ccsh/bin")
end
if fs.exists(".ccsh/history") then
for line in io.lines(".ccsh/history") do
history[#history+1] = line
end
end
if not fs.exists(".ccsh/format") then
local f = fs.open(".ccsh/format", "w")
f.write(format)
f.close()
end
if not fs.exists(".ccsh/issue") then
local f = fs.open(".ccsh/issue", "w")
f.write(issue)
f.close()
end
local function replace(str, i, j, r)
return str:sub(1, i-1) .. tostring(r) .. str:sub(j+1)
end
local function writeStyle(style, env)
env = env or getfenv()
-- Parse variables
style = style:gsub("{PATH}", shell.dir())
style = style:gsub("{TIME}", textutils.formatTime(os.time()))
style = style:gsub("{TIME24}", textutils.formatTime(os.time(), true))
-- Execute Lua variables
repeat
local s, e = style:find("{:.-:}")
if s then
local code = style:sub(s+2, e-2)
code = code:gsub("^=", "return ")
local func, err = load(code, "=&e(ccsh lua)", "t", env)
if func then
local ok, ret = pcall(func)
if not ok then
ret = "&d" .. tostring(ret)
end
style = replace(style, s, e, ret or "")
else
style = replace(style, s, e, err .. "&e")
end
end
until not s
local escaped = false
local i = 1
while i <= #style do
local char = style:sub(i,i)
if escaped then
write(char)
escaped = false
elseif char == "&" then
local col = style:sub(i+1, i+1)
if cols[col] then
term.setTextColor(cols[col])
end
i = i + 1
elseif char == "\167" then
local col = style:sub(i+1, i+1)
if cols[col] then
term.setBackgroundColor(cols[col])
end
i = i + 1
elseif char == "\\" then
escaped = true
else
write(char)
end
i = i+1
end
end
local function parseCommand(str, tEnv)
-- Execute Lua variables
repeat
local s, e = str:find("{:.-:}")
if s then
local code = str:sub(s+2, e-2)
code = code:gsub("^=", "return ")
local func, err = load(code, "=(ccsh lua)", "t", env)
if func then
local ok, ret = pcall(func)
if not ok then
ret = tostring(ret)
end
str = replace(str, s, e, ret or "")
else
str = replace(str, s, e, err or "Unknown error")
end
end
until not s
return str
end
function shell.exit()
bExit = true
end
local tEnv = {
shellFail = false,
ccsh = {
version = "0.1",
args = args,
}
}
setmetatable(tEnv, {__index=getfenv()})
local f = fs.open(".ccsh/format", "r")
format = f.readLine()
f.close()
local f = fs.open(".ccsh/issue", "r")
issue = f.readLine()
f.close()
shell.setPath(shell.path() .. ":/.ccsh/bin")
writeStyle(issue, tEnv)
print()
if args[1] then
tEnv.shellFail = not shell.run(unpack(args))
else
if fs.exists(".ccsh/rc") then
for line in io.lines(".ccsh/rc") do
local parsed = parseCommand(line)
if line:match("%S") then
tEnv.shellFail = not shell.run(parsed)
end
end
end
end
while not bExit do
writeStyle(format, tEnv)
local input
if settings.get("shell.autocomplete") then
input = read(nil, history, shell.complete)
else
input = read(nil, history)
end
local parsed = parseCommand(input)
if input:match("%S") then
if history[#history] ~= input then
table.insert(history, input)
local f = fs.open("/.ccsh/history", "a")
f.write(input .. "\n")
f.close()
end
tEnv.shellFail = not shell.run(parsed)
end
end