It is currently June 25th, 2019, 6:28 am

I can't seem to replicate the "--" that is used in lua files for a notes skin...

Discuss the use of Lua in Script measures.
User avatar
JosephB2000
Posts: 152
Joined: July 29th, 2014, 7:02 pm

I can't seem to replicate the "--" that is used in lua files for a notes skin...

JosephB2000 » July 26th, 2016, 5:47 pm

When in a lua file, you can type two dashes (--) in order to type a comment after the two dashes that is ignored when the script is activated. I want to replicate this feature for a normal .txt file used for a notes skin, I currently have this:

Code: Select all

function Initialize()
	
	NotesFilePath = SKIN:MakePathAbsolute(SELF:GetOption('FilePath'))
	NotesBlockComment = SELF:GetOption('BlockComment')
	NotesDivider = SELF:GetOption('Divider')

end -- Initialise

function Update()

	NotesFile = io.open(NotesFilePath, 'r')
	
	NotesFileText = NotesFile:read('*a')
	NotesDividedString = NotesFileText:gsub(NotesDivider .. '.*', '')
	NotesString = NotesDividedString:gsub(NotesBlockComment .. '.*' .. NotesBlockComment, '')
	
	SKIN:Bang('!SetOption', 'NotesText', 'Text', '' .. NotesString)
	
end -- Update
There's a bit more to the script, but they all work perfectly so they should not be the problem.

Here is the measure from the .ini file:

Code: Select all

[MeasureNotes]
Measure=Script
ScriptFile=Notes.lua
FilePath=#NotesPath#
Title=#NotesTitle#
BlockComment=--
Divider={HiddenNotes}
What should happen is that whenever someone types in the .txt file "-- (Blah blah) --", everything from the first dash to the fourth dash should be substituted out of the string. Hwoever, all that is returned is "0".

I have no idea what the problem is...

And when I remove the NotesString = NotesDividedString:gsub(NotesBlockComment .. '.*' .. NotesBlockComment, '') from the lua script and replace NotesDividedString = NotesFileText:gsub(NotesDivider .. '.*', '') with NotesString = NotesFileText:gsub(NotesDivider .. '.*', ''), everything works perfectly...
[Insert Joke Here]
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: I can't seem to replicate the "--" that is used in lua files for a notes skin...

jsmorley » July 26th, 2016, 5:57 pm

Although it isn't the issue, you really want to consider .- instead of .* to indicate "zero or more", as .* is "greedy" and will for instance replace:

This is the first line
--This is the first comment--
This is the second line
--This is the second comment--
This is the third line

With just

This is the first line
This is the third line

This is due to it matching everything between the first -- and the LAST --

If you use .- instead, which is "ungreedy", it should return:

This is the first line
This is the second line
This is the third line
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: I can't seem to replicate the "--" that is used in lua files for a notes skin...

jsmorley » July 26th, 2016, 6:35 pm

So there is the problem with .* vs. .- noted above, and there is one other thing to be careful about:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[MeasureScript]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=-1
Divider=--
Lua:

Code: Select all

function Initialize()

	divider = SELF:GetOption('Divider')
	divider = divider:gsub('-', '%%-')
	
end

function Update()

	myString = 'This is line 1\n--This is comment 1--\nThis is line 2\n--This is comment 2--\nThis is line 3'
	
	newString = myString:gsub(divider..'.-'..divider, '')
	
	print(newString)
	
	SKIN:Bang('!SetOption', 'MeterOne', 'Text', newString)
		
end
Be aware that the "-" (minus) character is a reserved character in pattern matching as it means "ungreedy zero or more characters". It is perfectly fine to use "-" in a "string", but not in a "pattern" as a literal.

To use it as a literal in a pattern, you must escape the "-" character with "%". Of course "%" is ALSO a reserved character, so you must also escape it... So you end up with divider = divider:gsub('-', '%%-')

Then my example works as expected...
1.jpg
2.jpg
Reserved (or "magic") characters in Lua pattern matching are:

() . % + - * ? [ ^ $

The meaning of "repetition characters" in pattern matching are:

Code: Select all

+	1 or more repetitions
*	0 or more repetitions
-	also 0 or more repetitions
?	optional (0 or 1 occurrence)
https://www.lua.org/pil/20.2.html

It can get a bit involved with the difference between a "control character" sequence like \n that has a special meaning everywhere, and "reserved characters" which are only special in a pattern. That might be a conversation for another day.
You do not have the required permissions to view the files attached to this post.