Page 1 of 1
I can't seem to replicate the "--" that is used in lua files for a notes skin...
Posted: July 26th, 2016, 5:47 pm
by JosephB2000
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:
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:
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...
Re: I can't seem to replicate the "--" that is used in lua files for a notes skin...
Posted: July 26th, 2016, 5:57 pm
by jsmorley
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
Re: I can't seem to replicate the "--" that is used in lua files for a notes skin...
Posted: July 26th, 2016, 6:35 pm
by jsmorley
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.