It is currently November 4th, 2024, 7:45 am

Lua for me.

Discuss the use of Lua in Script measures.
User avatar
balala
Rainmeter Sage
Posts: 16699
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua for me.

Post by balala »

kyriakos876 wrote:This just keeps refreshing the skin every 1 second... Did I do something wrong?
There is no reason for this and for me it doesn't happen. [MeasureDiskss] is updated ONLY when [MeasureRun] is executed (due to its UpdateDivider=-1 option). The bangs of its OnChangeAction are executed ONLY when the value of the measure changes. But this doesn't go on if the [MeasureRun] isn't ran.
How do you run this measure? What has to happen, to run it?
User avatar
jsmorley
Developer
Posts: 22898
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Lua for me.

Post by jsmorley »

I'm simply looking at this line:

FinishAction=[!UpdateMeasure "MeasureDiskss"]

I have not looked closely, but if in fact updating that measure leads to a refresh of the skin, that is an endless loop.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

jsmorley wrote:I'm simply looking at this line:

FinishAction=[!UpdateMeasure "MeasureDiskss"]

I have not looked closely, but if in fact updating that measure leads to a refresh of the skin, that is an endless loop.
Wow, certainly a lot has happened while I slept. I'll try to catch up on everything.

Anyway, I think you could get around this by saving the list of disks as a variable using WriteKeyVaoue, them only invoking the script if the saved list differs from the current one. That way it will automatically update when a new disk is added or removed, but will avoid the refresh loop.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

kyriakos876 wrote:I personally can only be offended from a family member, so unless you are my mysterious brother, you won't have to worry about that. Though it's 2018, people might get offended because you offered them just water instead of sparkling water so you're only normal if you're paranoid about that nowadays lol.

I'm gonna have to work on the separating thingy indeed... I have an idea thought which I tried and kinda worked so I moved on.
I see you are fluent in lua so maybe you can riddle me this:

Code: Select all

SKIN:Bang(string.format('!WriteKeyValue DiskAnimation%s "IconAnimationIn" "[!SetVariable Underliner_Width_%s (Clamp(#Underliner_Width_%s#+25,0,(#MaxWidth#-[MeterDisk%s_Name:X]*2)))][!UpdateMeter Underliner_%s][!UpdateMeasure #CurrentSection#][!Redraw]" "#@#FileToEdit.txt"',i,i,i,i,i))
Why does this bang won't write the #CurrentSection# pramater in the [!UpdateMeasure #CurrentSection#] section but instead it leaves it blank?

and in this:

Code: Select all

SKIN:Bang(string.format('!WriteKeyValue Background_Disk_%s "MouseLeaveAction" "[!CommandMeasure DiskAnimation%s "Stop 1"][!CommandMeasure DiskAnimation%s "Execute 2"]" "#@#FileToEdit.txt"',i,i,i))
there's some syntax stuff going on in the "Stop 1" and "Execute 2" parameters because it wont write the MouseLeaveAction section at all, but when I erase the above, it does write it.


In both case, I tried escaping some double quotes or some hashtags, but, I either didn't escape the correct things, or that's not what's going on....

If you could help me here, that'd be great.
To escape variables (including section variables), you use the #*name*# / [*MeasureName*] syntax. If you use the regular syntax with !WriteKeyValue then it will try to resolve the variables.

If you download the RMSKIN I posted, you'll find that I actually updated your script to escape the variables so I could have the proper template for my script (since yours was resolving all of the variables, my script wasn't working properly).
Last edited by raiguard on September 28th, 2018, 4:46 pm, edited 1 time in total.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

kyriakos876 wrote:Also, could you please explain what's going on in your .lua code on the newT( t ) function? I'm no lua expert as you must have figured out by now.
newT() can be found here. The idea is that since LUA tables don't preserve key order, you need to create a custom table that will be able to preserve the key order. That's what newT() does. It acts exactly like a regular table, but when you use the opairs() function you can iterate over the table in the order that keys were added to it.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

kyriakos876 wrote:That is true and one of my concerns. I'm studying raiguards code that could solve many of my problems though.
When I'm done eating breakfast I'll go back and simplify the script, removing all of the unnecessary baggage that is left behind from what I originally used it for. This way you won't need to worry about the things that you don't need (such as newT(), which I don't think is necessary here).
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
balala
Rainmeter Sage
Posts: 16699
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua for me.

Post by balala »

jsmorley wrote:I'm simply looking at this line:

FinishAction=[!UpdateMeasure "MeasureDiskss"]

I have not looked closely, but if in fact updating that measure leads to a refresh of the skin, that is an endless loop.
Not certainly, because the [MeasureRun] measure (which has the above FinishAction option) isn't automatically executed on refresh of the skin. Or at least it shouldn't have to be. That's why I asked kyriakos876
balala wrote:How do you run this measure?
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:Wow, certainly a lot has happened while I slept. I'll try to catch up on everything.

Anyway, I think you could get around this by saving the list of disks as a variable using WriteKeyVaoue, them only invoking the script if the saved list differs from the current one. That way it will automatically update when a new disk is added or removed, but will avoid the refresh loop.
That's what I wanted to do but I can't create this "saved list" because on the first update, my measure returns nothing, the second update returns the disks, so I would want something like "On the second update, do this"
Also, to make sure we are on the same page, I made some progress. Grab the skin from below
BMAdaptiveDisk_28.9.2018.2.rmskin
You do not have the required permissions to view the files attached to this post.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:To escape variables (including section variables), you use the #*name*# / [*MeasureName*] syntax. If you use the regular syntax with !WriteKeyValue then it will try to resolve the variables.

If you download the RMSKIN I posted, you'll find that I actually updated your script to escape the variables so I could have the proper template for my script (since yours was resolving all of the variables, my script wasn't working properly).
I totally missed that you uploaded an rmskin package... Yes, I see it now.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

So I have simplified my script immensely, here is the new code:

Code: Select all

identifierString = '%^1%^'
cTable = {}

function Initialize() end

function Update() end

function Split(allDisks)

  allDisks:gsub(".", function(c)
    table.insert(cTable, c)
    SKIN:Bang(string.format('!WriteKeyValue Variables "Underliner_Width_%s" "0" "#@#Variables.inc"',c))
  end)
  -- print(allDisks)
  Concat()
  SKIN:Bang('!Refresh')

end

function Concat()
  
  local rTable = {}
  local lines = {}
  for line in io.lines(SELF:GetOption('templateFilePath')) do
    table.insert(rTable, line)
  end
  for k,v in ipairs(cTable) do
    for l,line in pairs(rTable) do
      line = line:gsub(identifierString, v)
      table.insert(lines, line)
    end
  end

  outputFile = io.open(SELF:GetOption('outputFilePath'), 'w')
  outputFile:write(table.concat(lines, '\n'))
  outputFile:close()

end
Here is the template that the script is using:

Code: Select all

[MeasureTotalDisk^1^]
Measure=FreeDiskSpace
Drive=^1^:
Total=1
IgnoreRemovable=0
UpdateDivider=100

[MeasureUsedDisk^1^]
Measure=FreeDiskSpace
Drive=^1^:
InvertMeasure=1
IgnoreRemovable=0
UpdateDivider=100

[DiskAnimation^1^]
Measure=Plugin
Plugin=ActionTimer
ActionList1=UpdateOnce | Wait 16 | Repeat IconAnimationIn, 16, 16
UpdateOnce=[!Update]
IconAnimationIn=[!SetVariable Underliner_Width_^1^ (Clamp(#Underliner_Width_^1^#+25,0,(#MaxWidth#-[MeterDisk^1^_Name:X]*2)))][!UpdateMeter Underliner_^1^][!UpdateMeasure ][!Redraw]
ActionList2=UpdateOnce | Wait 25 | Repeat IconAnimationOut, 16, 13
IconAnimationOut=[!SetVariable Underliner_Width_^1^ (Clamp(#Underliner_Width_^1^#-20,0,(#MaxWidth#-[MeterDisk^1^_Name:X]*2)))][!UpdateMeter Underliner_^1^][!UpdateMeasure ][!Redraw]
ActionList3=UpdateOnce | Wait 25 | Repeat MouseUp, 16, 6
MouseUp=[!SetVariable Underliner_Width_^1^ (Clamp(#Underliner_Width_^1^#-20,130,(#MaxWidth#-[MeterDisk^1^_Name:X]*2)))][!UpdateMeter Underliner_^1^][!UpdateMeasure ][!Redraw]

[MeterDisk^1^_Name]
Meter=String
MeterStyle=NormalLeft
X=#XFromTheWall#
Y=20r
DynamicVariables=1
UpdateDivider=-1

[MeterDisk^1^_Value]
Meter=String
MeasureName=MeasureUsedDisk^1^
MeasureName2=MeasureTotalDisk^1^
MeterStyle=NormalRight
X=(#MaxWidth#-#XFromTheWall#)
Y=r
Text=%1B/%2B
NumOfDecimals=1
AutoScale=1
UpdateDivider=100

[Background_Disk_^1^]
Meter=Shape
X=0
Y=([MeterDisk^1^_Name:Y]-[MeterDisk^1^_Name:W]/2)
Shape=Rectangle 0,0,#MaxWidth#,([MeterDisk^1^_Name:H]*1.75) | Fill Color 0,0,0,0 | StrokeWidth 0
LeftMouseUpAction=[^1^:]
DynamicVariables=1
UpdateDivider=-1

[Underliner_^1^]
Meter=Shape
X=(#MaxWidth#/2-0/2)
Y=-5R
Shape=Rectangle 0,0,#Underliner_Width_^1^#,1 | Fill Color 66,133,244 | StrokeWidth 0
DynamicVariables=1
UpdateDivider=-1

And here is the skin INI code I'm using:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
OnUpdateAction=[!CommandMeasure MeasureRun "Run"]
MouseScrollDownAction=[!Refresh]
BackgroundMode=2
Solidcolor=0,0,0
[Metadata]
Name=
Author=
Information=
Version=
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]
@Include=#@#Variables.inc
@Include2=#@#ScriptOutput.ini
AllDisks=CDE
StringLenght=0

[MeasureLua]
Measure=Script
ScriptFile=#@#Concat.lua
TemplateFilePath=#@#Template.ini
OutputFilePath=#@#ScriptOutput.ini
DynamicVariables=1

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Parameter=wmic logicaldisk get caption
OutputType=ANSI
RegExpSubstitute=1
Substitute="Caption":""," ":"",":":"","Name.*#CRLF#":"","#CRLF#":"
DynamicVariables=1
FinishAction=[!EnableMeasure MeasureComparison][!UpdateMeasure MeasureComparison]

[MeasureComparison]
Measure=String
String=[MeasureRun]
IfMatch=^#AllDisks#$
IfNotMatchAction=[!WriteKeyValue Variables AllDisks "[MeasureRun]"][!DisableMeasure MeasureComparison][!CommandMeasure MeasureLua "Split('[MeasureRun]')"]
DynamicVariables=1
Disabled=1

[MeterDummy]
Meter=Image
SolidColor=10,10,10
W=20
H=20
You will see that I added a new measure, [MeasureComparison]. When [MeasureRun] finishes, it enables [MeasureComparison] which will compare the value of [MeasureRun] to the value of the #allDisks# variable. This way, the skin will automatically update when disks are added or removed, and automatically refresh, but avoids the aforementioned refresh loop:
RMAdaptiveDisks2.gif
You can see here that despite needing to write a file and refresh the skin, it is quite fast. Disks Meter is only there to demonstrate which disks are being added and removed.

I have also package an RMSKIN for you so you can check it out in its full glory. With this method, rather than having to worry about escaping variables and getting quotes to work in a !WriteKeyValue bang, all you need to do is edit the template as you would edit a skin, and it'll work. Final note: If you want the script to leave a blank line between iterations of the template, then you need to leave two empty lines at the end of the template.
You do not have the required permissions to view the files attached to this post.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017