Added grep package by osmarks
This commit is contained in:
parent
9626270d58
commit
62be874275
36
grep/bin/grep.lua
Normal file
36
grep/bin/grep.lua
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
local pattern, file = ...
|
||||||
|
if not pattern then error "At least a pattern is required" end
|
||||||
|
if file and not fs.exists(file) then error(("%s does not exist"):format(file)) end
|
||||||
|
if not file then file = "." end
|
||||||
|
|
||||||
|
local function scan_file(filepath)
|
||||||
|
local filepath = fs.combine(filepath, "")
|
||||||
|
if fs.isDir(filepath) then
|
||||||
|
for _, basename in pairs(fs.list(filepath)) do
|
||||||
|
scan_file(fs.combine(filepath, basename))
|
||||||
|
end
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local fh = fs.open(filepath, "r")
|
||||||
|
local count = 1
|
||||||
|
local has_printed_filename = false
|
||||||
|
while true do
|
||||||
|
local line = fh.readLine()
|
||||||
|
if line == nil then break end
|
||||||
|
if line:match(pattern) then
|
||||||
|
if not has_printed_filename then
|
||||||
|
if term.isColor() then term.setTextColor(colors.blue) end
|
||||||
|
print(filepath)
|
||||||
|
end
|
||||||
|
if term.isColor() then term.setTextColor(colors.lime) end
|
||||||
|
write(tostring(count) .. ": ")
|
||||||
|
if term.isColor() then term.setTextColor(colors.white) end
|
||||||
|
textutils.pagedPrint(line)
|
||||||
|
has_printed_filename = true
|
||||||
|
end
|
||||||
|
count = count + 1
|
||||||
|
end
|
||||||
|
fh.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
scan_file(file)
|
12
grep/pkgmeta.ltn
Normal file
12
grep/pkgmeta.ltn
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
["version"] = "0.1.0",
|
||||||
|
["dependencies"] = {
|
||||||
|
|
||||||
|
},
|
||||||
|
["description"] = "Allows searching files for patterns.",
|
||||||
|
["files"] = {
|
||||||
|
["bin"] = {
|
||||||
|
"grep.lua"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,9 +2,10 @@ local mt = {}
|
|||||||
|
|
||||||
mt.newPool = function()
|
mt.newPool = function()
|
||||||
local pool = {}
|
local pool = {}
|
||||||
pool.threads = {}
|
pool.threads = {} -- indexed by coroutines, contains their metadata
|
||||||
pool.namedThreads = {}
|
pool.namedThreads = {} -- indexed by coroutine names, contains their metadata also.
|
||||||
pool.threadcount = 0
|
-- coroutines are named if their metadata has a name field.
|
||||||
|
pool.threadcount = 0 -- used for detecting when all things in a pool are depleted
|
||||||
|
|
||||||
pool.clear = function()
|
pool.clear = function()
|
||||||
pool.threads = {}
|
pool.threads = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user