Advertisement
Guest User

Untitled

a guest
May 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #!/usr/bin/env lua
  2.  
  3. local languages = {
  4. ["C#"] = true
  5. }
  6.  
  7. local lines_per_bowl = 100
  8.  
  9. local function split(str, delimeters)
  10. local elements = {}
  11. string.gsub(str, "([^,]+)", function(value)
  12. table.insert(elements, value)
  13. end)
  14. return elements
  15. end
  16.  
  17. local function read(dataFile)
  18. local fh = io.open(dataFile, "r")
  19. if not fh then return nil end
  20. local value = tonumber(fh:read("*a"))
  21. fh:close()
  22. return value
  23. end
  24.  
  25. local function write(dataFile, data)
  26. local fh = io.open(dataFile, "w")
  27. fh:write(data)
  28. fh:close()
  29. end
  30.  
  31. local function cloc(directory)
  32. local fh = io.popen("cloc --csv " .. directory)
  33. if not fh then error(directory) end
  34.  
  35. local count = 0
  36.  
  37. local line
  38. for i = 1, 6 do
  39. line = fh:read("*line")
  40. end
  41.  
  42. while line do
  43. local parts = split(line, ",")
  44. if languages[parts[2]] then
  45. count = count + tonumber(parts[5])
  46. end
  47. line = fh:read("*line")
  48. end
  49.  
  50. fh:close()
  51.  
  52. return count
  53. end
  54.  
  55. local function main(args)
  56. if #args ~= 2 then
  57. print("Usage: lfb <directory> <data-file>")
  58. return
  59. end
  60.  
  61. local directory = args[1]
  62. local dataFile = args[2]
  63.  
  64. local last = read(dataFile)
  65. local current = cloc(directory)
  66.  
  67. if last then
  68. local difference = last - current
  69.  
  70. print(difference .. " lines written/removed since last bowl.")
  71. if difference >= lines_per_bowl then
  72. print("Time for another bowl!")
  73. write(dataFile, current)
  74. else
  75. print("Keep coding! Only " .. (lines_per_bowl - difference) .. " more lines until another bowl!")
  76. end
  77. else
  78. print("Keep coding! Only " .. lines_per_bowl .. " more lines before another bowl!")
  79. write(dataFile, current)
  80. end
  81.  
  82.  
  83. end
  84.  
  85. main({ ... })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement