Added grep package by osmarks

This commit is contained in:
v 2021-01-30 14:14:11 +00:00
parent 9626270d58
commit 62be874275
3 changed files with 52 additions and 3 deletions

36
grep/bin/grep.lua Normal file
View 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
View File

@ -0,0 +1,12 @@
{
["version"] = "0.1.0",
["dependencies"] = {
},
["description"] = "Allows searching files for patterns.",
["files"] = {
["bin"] = {
"grep.lua"
}
}
}

View File

@ -2,9 +2,10 @@ local mt = {}
mt.newPool = function()
local pool = {}
pool.threads = {}
pool.namedThreads = {}
pool.threadcount = 0
pool.threads = {} -- indexed by coroutines, contains their metadata
pool.namedThreads = {} -- indexed by coroutine names, contains their metadata also.
-- 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.threads = {}