It is currently March 28th, 2024, 12:27 pm

Help with changing the text option with Lua

Discuss the use of Lua in Script measures.
Post Reply
KENDARRR
Posts: 5
Joined: December 15th, 2015, 1:08 am

Help with changing the text option with Lua

Post by KENDARRR »

I've downloaded Rainmeter skin and have been adding onto it as I think up different uses for it. My latest thought was a ping monitor. All I've set up the skin to, on a button press, run a batch file, write the contents to a .txt file, and lua to read and interpret the text file. I'm having trouble getting my lua script to update the skin properly though. I've managed to get it to update the skin with strings I've defined myself, but when I put it all together I can't get it to work. Here's what I have so far for my Lua script:

Code: Select all

function Initialize()
    i = 11
    ping = ""
    hat = io.open("pingtest.txt", "rb")
    hat:seek("end", -52)
    data = hat:read("*line")
    hat:close()
end

function Update()
    while (data:sub(i,i) ~= ",") do
        ping = ping .. data:sub(i,i)
        i = i + 1
    end
    form = string.format('[!setoption PV text %s]' , ping)
    SKIN:Bang(form)
end
And here's the pertinent part of my skin's .ini:

Code: Select all

[pingTest]
Measure=Plugin
Plugin=RunCommand
StartinFolder=C:\Users\Kendall\Documents\Rainmeter\Skins\transbar\bar_up_1680
Parameter=ech.bat
outputtype=ANSI
OutputFile=pingtest.txt
FinishAction=[!UpdateMeasure #CURRENTSECTION#][!SetOption Output MeasureName pingtest][!UpdateMeter Output][!Redraw]

[LUAS]
measure=script
scriptfile=mylua.lua

[pingLabel]
x=15r
y=2
meter=string
FontColor=ACACAC
FontFace=Tahoma
FontSize=7
Stringstyle=BOLD
AntiAlias=0
text="Ping: "
LeftMouseUpAction=[!CommandMeasure pingtest run][!UpdateMeasure pingtest][!Redraw]

[PV]
x=0r
y=14
meter=string
fontcolor=ACACAC
FontFace=Tahoma
FontSize=7
AntiAlias=0
text='0ms'
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with changing the text option with Lua

Post by balala »

Try to modify the last two lines of the Update() function:

Code: Select all

form = string.format('[!setoption PV text %s]' , ping)
SKIN:Bang(form)
to a this kind of command: SKIN:Bang('!SetOption', 'PV', 'Text', ping) or SKIN:Bang('!SetOption', 'PV', 'Text', string.format('%s' , ping)) (I must admit that I'm not sure how the string.format command will work in this context).
Optionally you can add after this line a new one to update the PV meter: SKIN:Bang('!UpdateMeter', 'PV').
KENDARRR
Posts: 5
Joined: December 15th, 2015, 1:08 am

Re: Help with changing the text option with Lua

Post by KENDARRR »

I got it to work with other variables like that, and the way you mentioned. For what ever reason it won't work if I put

Code: Select all

SKIN:Bang('!Setoption', 'PV', 'text ', ping)
either. I tried the format way because I suspected that Lua might have been interpreting ping incorrectly. The script works, I can verify that it works by running the script in the compiler, and checking with print statements, I can even get a script to use !SetOption correctly, but for what ever reason when I introduce ping into the !setOption bang it just stop working. I can't seem to figure out why it's not working. I really appreciate any help I can get on this, I've been trying to work this out for days now.

Edit:
Another problem I've come across is if I put the bang after the while loop it doesn't set the text to anything.So this doesn't work

Code: Select all

function Update()
	while (data:sub(i,i) ~= ",") do
		ping = ping .. data:sub(i,i)
		i = i + 1
	end
	SKIN:Bang('!SetOption', 'PV', 'Text', 'cats')
	SKIN:Bang('!UpdateMeter', 'PV')
end
But this does

Code: Select all

function Update()
	SKIN:Bang('!SetOption', 'PV', 'Text', 'cats')
	SKIN:Bang('!UpdateMeter', 'PV')
	while (data:sub(i,i) ~= ",") do
		ping = ping .. data:sub(i,i)
		i = i + 1
	end
end
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with changing the text option with Lua

Post by balala »

KENDARRR wrote:I got it to work with other variables like that, and the way you mentioned. For what ever reason it won't work if I put

Code: Select all

SKIN:Bang('!Setoption', 'PV', 'text ', ping)
either. I tried the format way because I suspected that Lua might have been interpreting ping incorrectly. The script works, I can verify that it works by running the script in the compiler, and checking with print statements, I can even get a script to use !SetOption correctly, but for what ever reason when I introduce ping into the !setOption bang it just stop working. I can't seem to figure out why it's not working. I really appreciate any help I can get on this, I've been trying to work this out for days now
What is the value of the ping variable? Depending on it, try a tostring(ping) function instead of the simple ping.
KENDARRR wrote:Another problem I've come across is if I put the bang after the while loop it doesn't set the text to anything.So this doesn't work

Code: Select all

function Update()
	while (data:sub(i,i) ~= ",") do
		ping = ping .. data:sub(i,i)
		i = i + 1
	end
	SKIN:Bang('!SetOption', 'PV', 'Text', 'cats')
	SKIN:Bang('!UpdateMeter', 'PV')
end
But this does

Code: Select all

function Update()
	SKIN:Bang('!SetOption', 'PV', 'Text', 'cats')
	SKIN:Bang('!UpdateMeter', 'PV')
	while (data:sub(i,i) ~= ",") do
		ping = ping .. data:sub(i,i)
		i = i + 1
	end
end
It seems that here's a problem with the data:sub(i,i) ~= "," function. I don't know what it should do, but for sure it is causing the error. If you replace that function, the script will work, no matter where are you putting the !SetOption and the !UpdateMeter bangs. I suppose that in the first form, because the while statement is wrong, the Bang commands are never reached and executed, but in the last one, the bangs are executed first and the script match that wrong while statement after that (for sure a good programmer could explain better what's happening). What is the essence of this function? Could you post a link, because I tried to find a description, but found nothing interesting.
KENDARRR
Posts: 5
Joined: December 15th, 2015, 1:08 am

Re: Help with changing the text option with Lua

Post by KENDARRR »

The goal of the script is to read a text file to which I have output the result of the ping command in command prompt.
The result looks like this:

Code: Select all


Pinging 104.160.131.1 with 32 bytes of data:
Reply from 104.160.131.1: bytes=32 time=69ms TTL=56

Ping statistics for 104.160.131.1:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 69ms, Maximum = 69ms, Average = 69ms
P
The only thing that will change is the numbers for the ping.

I just realized an error,

Code: Select all

hat:seek("end", -52)
should be

Code: Select all

hat:seek("end", -49)
. That shouldn't have any effect on whether or not it works though.

The initalize() function will read that text document -49 characters from the end. Which will be at the line

Code: Select all

Minimum = 69ms, Maximum = 69ms, Average = 69ms
without the spaces at the beginning. The while loop is supposed to check if what ever character in that line in the index 11 is a comma(In this case that first 6), then it appends it to ping and moves on to the next character until it hits a comma, then the loop should end.

And this is where I got the data:sub(i,i) ~= "," thing from.

Update: The code works as is. But if I add the SKIN:Bang it won't update the meter.
ech.jpg
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with changing the text option with Lua

Post by balala »

KENDARRR wrote:And this is where I got the data:sub(i,i) ~= "," thing from.
Ok, until now I used the sub function in a bit different form. I used it as string.sub(data,i,i) instead of data:sub(i,i) and that's why didn't realize what we are talking about. Sorry...
Now it's not very clear for me, but as I see you'd like to see on the screen the content of the pingtest.txt file, until the first comma. Maybe I'm wrong, but that's what I realized from the code. Am I right?
Anyway, in lua I read the content of a text file in a different way:

Code: Select all

function Read()	
	local FilePath = SKIN:MakePathAbsolute('pingtest.txt')
	local hat = io.open(FilePath, 'r')
	data = {}
	for Line in hat:lines() do
		table.insert(data, Line)
	end
	hat:close()
	return data
end
This is created as a stand-alone function, which must be called in the Initialize() or in the Update() function.
After that, you'll have the content of the pingtest.txt file into an array. But if I'm not wrong, you can't go through it as you did. Instead I tried the following code (that's the content of the whole mylua.lua file):

Code: Select all

function Read()	
	local FilePath = SKIN:MakePathAbsolute('pingtest.txt')
	local hat = io.open(FilePath, 'r')
	data = {}
	for Line in hat:lines() do
		table.insert(data, Line)
	end
	hat:close()
	return data
end

function Initialize()

end

function Update()
	MyData = Read()
	ping = ''
	j = 1
	repeat
		i = 1
		comma = false
		repeat
			if MyData[j]:sub(i,i) == ',' then
				comma = true
			else
				comma = false
			end
			ping = ping .. MyData[j]:sub(i,i)
			i = i + 1
		until ( comma ) or (i > #MyData[j])
		j = j + 1
	until ( comma ) or ( j > #MyData )
	SKIN:Bang('!SetOption', 'PV', 'Text', ping)
end
KENDARRR
Posts: 5
Joined: December 15th, 2015, 1:08 am

Re: Help with changing the text option with Lua

Post by KENDARRR »

I only want the "69ms" portion. I'll give this a shot though. Thanks. I'll post results when I'm done.

Update:
I had to make some tweaks, but I got it to do what I wanted.

Code: Select all

function Read()
	local FilePath = SKIN:MakePathAbsolute('pingtest.txt')
	local hat = io.open(FilePath, 'r')
	data = {}
	for Line in hat:lines() do
		table.insert(data, Line)
	end
	hat:close()
	return data
end

function Initialize()
end

function Update()
	MyData = Read()
	ping = ''
	j = 8
	i = 15
	comma = false
	repeat
		if MyData[j]:sub(i,i) == ',' then
			comma = true
		else
			comma = false
			ping = ping .. MyData[j]:sub(i,i)
			i = i + 1
		end
	until ( comma == true) or ( j > #MyData )
	SKIN:Bang('!SetOption', 'PV', 'Text', ping)
end
There's a few things I didn't understand, like the table and arrays, but I get it now. Thanks I really appreciate your help.

FInal Update:
I figured out why my first one didn't work... local FilePath = SKIN:MakePathAbsolute('pingtest.txt') I only had io.open(pingtest.txt, 'r'), which is good enough for Lua, but not when you incorporate Rainmeter. Thank you so much for your help.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with changing the text option with Lua

Post by balala »

KENDARRR wrote:I only want the "69ms" portion.
I'm not sure what this mean? You want the whole line, or just those numbers (69)?
KENDARRR wrote:There's a few things I didn't understand, like the table and arrays, but I get it now.
This is a very good manual for the lua programmers. I use it often. See the 11.1 section.
KENDARRR wrote:Thank you so much for your help.
You're welcome. Just let me (us) know if you have further questions.
KENDARRR
Posts: 5
Joined: December 15th, 2015, 1:08 am

Re: Help with changing the text option with Lua

Post by KENDARRR »

The text file I read from is rewritten with new information every time I run the ping command. So it won't be "69ms" everytime. It might be "20ms" it might me "566ms" I just need to read what ever number is there and update the meter with that value.

I'll be doing a lot of Rainmeter stuff in the future I imagine, so I'll read up on more Lua too. Thanks for the tip.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with changing the text option with Lua

Post by balala »

You're welcome and I'm glad if I could help.
Post Reply