forked from luvit/luvit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
141 lines (119 loc) · 3.72 KB
/
main.lua
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
--[[
Copyright 2014 The Luvit Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
-- Create a luvit powered main that does the luvit CLI interface
return require('./init')(function (...)
local luvi = require('luvi')
local uv = require('uv')
local utils = require('utils')
local package = require('./package.lua')
local startRepl = nil
local combo = nil
local script = nil
local extra = {}
local function usage()
print("Usage: " .. args[0] .. " [options] script.lua [arguments]"..[[
Options:
-h, --help Print this help screen.
-v, --version Print the version.
-e code_chunk Evaluate code chunk and print result.
-i, --interactive Enter interactive repl after executing script.
-n, --no-color Disable colors.
-c, --16-colors Use simple ANSI colors
-C, --256-colors Use 256-mode ANSI colors
(Note, if no script is provided, a repl is run instead.)
]])
startRepl = false
end
local function version()
print('luvit version: ' .. package.version)
print('luvi version: ' .. require('luvi').version)
for k, v in pairs(require('luvi').options) do
print(k .. ' version: ' .. tostring(v))
end
startRepl = false
end
local shorts = {
h = "help",
v = "version",
e = "eval",
i = "interactive",
n = "no-color",
c = "16-colors",
C = "256-colors",
}
local flags = {
help = usage,
version = version,
eval = function ()
local repl = require('repl')(utils.stdin, utils.stdout)
combo = repl.evaluateLine
startRepl = false
end,
interactive = function ()
startRepl = true
end,
["no-color"] = function ()
utils.loadColors(false)
end,
["16-colors"] = function ()
utils.loadColors(16)
end,
["256-colors"] = function ()
utils.loadColors(256)
end,
}
for i = 1, #args do
local arg = args[i]
if script then
extra[#extra + 1] = arg
elseif combo then
combo(arg)
combo = nil
elseif string.sub(arg, 1, 1) == "-" then
local flag
if (string.sub(arg, 2, 2) == "-") then
flag = string.sub(arg, 3)
else
arg = string.sub(arg, 2)
flag = shorts[arg] or arg
end
local fn = flags[flag] or usage
fn()
else
script = arg
end
end
if combo then error("Missing flag value") end
if startRepl == nil and not script then startRepl = true end
if script then
require(luvi.path.join(uv.cwd(), script))
end
if startRepl then
local env = require('env')
local pathJoin = require('luvi').path.join
local fs = require('fs')
local c = utils.color
local greeting = "Welcome to the " .. c("err") .. "L" .. c("quotes") .. "uv" .. c("table") .. "it" .. c() .. " repl!"
local historyFile
if require('ffi').os == "Windows" then
historyFile = pathJoin(env.get("APPDATA"), "luvit_history")
else
historyFile = pathJoin(env.get("HOME"), ".luvit_history")
end
local lines = fs.readFileSync(historyFile) or ""
local function saveHistory(lines)
fs.writeFileSync(historyFile, lines)
end
require('repl')(utils.stdin, utils.stdout, greeting, ...).start(lines, saveHistory)
end
end, ...)