Skip to content

Instantly share code, notes, and snippets.

@ggcrunchy
Created December 19, 2017 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggcrunchy/c02adb80221173b4884e96379ba0002b to your computer and use it in GitHub Desktop.
Save ggcrunchy/c02adb80221173b4884e96379ba0002b to your computer and use it in GitHub Desktop.
Little LOC counter
local lfs = require("lfs")
local N, NFiles, NSpaces = 0, 0, 0
local doc_path = system.pathForFile( "", system.ResourceDirectory )
local function About (name, file, indent)
print(indent, file)
local n, nspaces = 0, 0
for line in io.lines(name) do
n = n + 1
if not line:find("%S") then
nspaces = nspaces + 1
end
end
print(indent, "# lines: " .. n, "# being blank: ", nspaces)
print(indent)
N, NFiles, NSpaces = N + n, NFiles + 1, NSpaces + nspaces
end
local function HasLua (dir)
for file in lfs.dir(dir) do
if file ~= "." and file ~= ".." then
local name = dir .. "/" .. file
local mode = lfs.attributes(name, "mode")
if mode == "directory" and HasLua(name) then
return true
elseif mode == "file" and name:sub(-3) == "lua" then
return true
end
end
end
end
local function VisitDir (dir, indent)
if HasLua(dir) then
for file in lfs.dir(dir) do
if file ~= "." and file ~= ".." then
local name = dir .. "/" .. file
local mode = lfs.attributes(name, "mode")
if mode == "directory" then
print(indent, file .. "/")
print(indent)
VisitDir(name, indent .. " ")
elseif mode == "file" and name:sub(-3) == "lua" then
About(name, file, indent)
end
end
end
end
end
VisitDir(doc_path, "")
print("# lines: " .. N, "# being spaces: " .. NSpaces, "in # files: " .. NFiles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment