It is currently March 29th, 2024, 5:14 am

Using Regex to escape variables

Get help with creating, editing & fixing problems with skins
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Regex to escape variables

Post by raiguard »

But it's clearly not doing that. In my example I used !WriteKeyValue to write escaped variables, and when I reloaded the file, the variables had been written without the escape characters.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using Regex to escape variables

Post by jsmorley »

Ah.. Ok.

Skin:

Code: Select all

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

[Variables]

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

[MeasureSet]
Measure=Calc
OnUpdateAction=[!WriteKeyValue Variables myOtherVar "[Lua]"]
DynamicVariables=1
UpdateDivider=-1

[MeterOne]
Meter=String
MeasureName=Lua
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Lua:

Code: Select all

function Initialize()

end	

function Update()

	myVar = 'contentMarginRight=((#bgWidth# + #bgOffset#) - #contentMarginAbs# - 1)'
	
	return myVar

end
1.png
And what you then get in the skin:

Code: Select all

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

[Variables]
myOtherVar=contentMarginRight=((#bgWidth# + #bgOffset#) - #contentMarginAbs# - 1)

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

[MeasureSet]
Measure=Calc
OnUpdateAction=[!WriteKeyValue Variables myOtherVar "[Lua]"]
DynamicVariables=1
UpdateDivider=-1

[MeterOne]
Meter=String
MeasureName=Lua
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
So don't escape anything. They are seen as literal strings by Lua, and when used in a [SectionVariable] in the !WriteKeyValue bang, that is AFTER everything has been "resolved", and they will still be treated as literal strings. Of course if you want to "use" what you are writing to the file, to then "resolve" the variables where they are used, you will have to !Refresh the skin.
You do not have the required permissions to view the files attached to this post.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Regex to escape variables

Post by raiguard »

Refreshing isn't an issue, it already does that anyway.

My problem with your approach is that the !WriteKeyValue bangs need to happen from within the LUA script. The script parses both the backup file and the suite's file with ReadINI, then goes through the backup file's table, seeing if those options exist in the suite's file. If it does, then it performs a !WriteKeyValue bang to copy the value from the backup file into the suite's file. I don't see any non-convoluted way to do this using the script as a section variable.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using Regex to escape variables

Post by jsmorley »

raiguard wrote:Refreshing isn't an issue, it already does that anyway.

My problem with your approach is that the !WriteKeyValue bangs need to happen from within the LUA script. The script parses both the backup file and the suite's file with ReadINI, then goes through the backup file's table, seeing if those options exist in the suite's file. If it does, then it performs a !WriteKeyValue bang to copy the value from the backup file into the suite's file. I don't see any non-convoluted way to do this using the script as a section variable.
What if you did:

Skin:

Code: Select all

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

[Variables]

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

[MeterOne]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[Lua]
DynamicVariables=1
Lua:

Code: Select all

function Initialize()

end	

function Update()

	myVar = 'contentMarginRight=((#bgWidth# + #bgOffset#) - #contentMarginAbs# - 1)'
	myVar = myVar:gsub( '#(.-)#', '#\*%1\*#')
	SKIN:Bang('!WriteKeyValue', 'Variables', 'myVar', myVar)
	
	return myVar

end
Seems to me you then end up with:

Code: Select all

[Variables]
myVar=contentMarginRight=((#bgWidth# + #bgOffset#) - #contentMarginAbs# - 1)
So in short, I was wrong earlier. !WriteKeyValue does deal with the * escape character, but you have to be a bit careful "when" you introduce the escaped variable to the bang, must be before variables are "resolved". So if you use a [SectionVariable] in the !WriteKeyValue bang, the [SectionVariable] is "resolved", but it doesn't circle back and resolve the things that are produced by the [SectionVariable]. If that makes sense.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Regex to escape variables

Post by raiguard »

Awesome, sorry for getting so heated in that argument. That regex you used will work nicely, but it doesn't support nested variables syntax. That's not a problem though, because I have yet to actually use them anywhere but in Inline LUA. Anyway, thanks again.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using Regex to escape variables

Post by jsmorley »

raiguard wrote:Awesome, sorry for getting so heated in that argument. That regex you used will work nicely, but it doesn't support nested variables syntax. That's not a problem though, because I have yet to actually use them anywhere but in Inline LUA. Anyway, thanks again.
I didn't see you as heated, just pointed. That's perfectly fine, I tend to be the same way.

Using gsub to escape nested variables and section variables might take some thought...
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Using Regex to escape variables

Post by raiguard »

EDIT: Nevermind, it consistently does it with the keys as well.

So on a related note, I just noticed that the ReadINI function is dropping leading S'es. Sometimes it will on the keys, but it does it consistently on commands.

Code: Select all

function ReadIni(inputfile)
  local file = assert(io.open(inputfile, 'r'), 'Unable to open ' .. inputfile)
  local tbl, section = {}
  local num = 0
  for line in file:lines() do
    num = num + 1
    if not line:match('^%s-;') then
      local key, command = line:match('^([^=]+)=(.+)')
      if line:match('^%s-%[.+') then
        section = line:match('^%s-%[([^%]]+)'):lower()
        if not tbl[section] then tbl[section] = {} end
      elseif key and command and section then
        tbl[section][key:lower():match('^s*(%S*)%s*$')] = command:match('^s*(.-)%s*$'):gsub('#(.-)#', '#\*%1\*#')
      elseif #line > 0 and section and not key or command then
        print(num .. ': Invalid property or value.')
      end
    end
  end
  if not section then print('No sections found in ' .. inputfile) end
  file:close()
  return tbl
end
For example, I have lots of variables that begin with "show". Occasionally, it will drop all of the S'es in show in an entire file, but it doesn't always do it. For the commands, if I have something like test=sally, it will always end up as test=ally in the table. Is there a mistake in the regex somewhere?
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Using Regex to escape variables

Post by jsmorley »

Might be. I think we need smurfier to weigh in on this, he wrote that code snippet.
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Using Regex to escape variables

Post by ikarus1969 »

While waiting for smurfier could you try the following:
In the ReadINI function replace the %s with a space character.
As you can capture every character with the percent sign in LUA, maybe one (or more) of the %s will be treated not as a space but as the character s.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm

Re: Using Regex to escape variables

Post by SilverAzide »

raiguard wrote:For example, I have lots of variables that begin with "show". Occasionally, it will drop all of the S'es in show in an entire file, but it doesn't always do it. For the commands, if I have something like test=sally, it will always end up as test=ally in the table. Is there a mistake in the regex somewhere?
I'm no good at regex, but is there a couple missing "%" signs in this line:

tbl[section][key:lower():match('^s*(%S*)%s*$')] = command:match('^s*(.-)%s*$'):gsub('#(.-)#', '#\*%1\*#')

Is this suppose to be...?

tbl[section][key:lower():match('^[color=#FF0000]%[/color]s*(%S*)%s*$')] = command:match('^[color=#FF0000]%[/color]s*(.-)%s*$'):gsub('#(.-)#', '#\*%1\*#')
Gadgets Wiki GitHub More Gadgets...