How can I get the lines of code a script has from a plugin?

Hello developers!

I am making a script library plugin with a few of my favorite scripts, and I was wondering how I can get the amount of lines that a script has from a plugin?

Thank you!

Every LuaSourceContainer has a Source property that represents the script’s contents as a string. You can count how many newlines exist in that string with a pattern and that’ll be the number of lines the script has. You can dissect a script line counter plugin to see how they count lines - take Crazyman32’s Lines Of Code plugin for example.

local ignoreBlankLines = false

local function TrimString(str)
	return str:match("^%s*(.-)%s*$")

local function CountLines(str)
	local loc = 1
	if (ignoreBlankLines) then
		for line in str:gmatch("(.-)\n") do
			if (TrimString(line) ~= "") then
				loc = (loc + 1)
		loc = (select(2, str:gsub("\n", "")) + 1)
	return loc

(ignoreBlankLines is a boolean variable)


How do I get the source contents? And would I use the code provided like this:

Yes, you would just call CountLines on the source property.

Hello. This is a great method. But cam you kindly tell me where to find documentation for lua’s string patterns? Thank you very much.

String patterns on the Developer Hub. This is a pretty sufficient documentation source in itself and one that I reference often, but you might be able to find more resources on the web with different explanations.