diff --git a/grep/bin/grep.lua b/grep/bin/grep.lua new file mode 100644 index 0000000..f1c175b --- /dev/null +++ b/grep/bin/grep.lua @@ -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) diff --git a/grep/pkgmeta.ltn b/grep/pkgmeta.ltn new file mode 100644 index 0000000..5981621 --- /dev/null +++ b/grep/pkgmeta.ltn @@ -0,0 +1,12 @@ +{ + ["version"] = "0.1.0", + ["dependencies"] = { + + }, + ["description"] = "Allows searching files for patterns.", + ["files"] = { + ["bin"] = { + "grep.lua" + } + } +} diff --git a/multitask/lib/multitask.lua b/multitask/lib/multitask.lua index 86117a0..3b94db4 100644 --- a/multitask/lib/multitask.lua +++ b/multitask/lib/multitask.lua @@ -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 = {}