It is currently May 25th, 2019, 2:17 pm

Learning Lua.

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

Re: Learning Lua.

balala » September 27th, 2017, 4:01 pm

A few things about the posted lua code:
  • Unlike in Rainmeter, in lua we don't specify a variable between #. Eg in the last line of the Update() function, you don't have to refere to the inc variable with #inc# (SKIN:Bang('!SetVariable', 'OldVarr[color=#FF0000]#inc#[/color]', OldVar)). This makes no sense here (see below).
  • To use the OldVarr indexed variables in a Rainmeter code, you have to concatenate the OldVarr with the inc variable: SKIN:Bang('!SetVariable', [color=#FF0000]'OldVarr'..inc[/color], OldVar). Take care what are you including between apostrophes.
  • The OldVar = OldVar line of the if StartCheck ~= 0 then structure (under the Update() function) seem absolutely useless. Doesn't worth to atribute to a variable its own value.
At least for now, I didn't understand what you would like to do with this script.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 4:14 pm

jsmorley wrote:See here for some more specific help:

https://docs.rainmeter.net/snippets/#FileInputOutput
A little bit confused here. I have this:

Code: Select all

function ReadFile(FilePath)
	-- HANDLE RELATIVE PATH OPTIONS.
	FilePath = SKIN:MakePathAbsolute(FilePath)
	-- OPEN FILE.
	local File = io.open(FilePath)
	-- HANDLE ERROR OPENING FILE.
	if not File then
		print('ReadFile: unable to open file at ' .. FilePath)
		return
	end
	-- READ FILE CONTENTS AND CLOSE.
	local Contents = File:read('*all')
	File:close()
	return Contents
end
from the docs, what is being returned here, and where is it being returned? Is it the containing of the .ini file that called the script?
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 4:23 pm

balala wrote:A few things about the posted lua code:
  • Unlike in Rainmeter, in lua we don't specify a variable between #. Eg in the last line of the Update() function, you don't have to refere to the inc variable with #inc# (SKIN:Bang('!SetVariable', 'OldVarr[color=#FF0000]#inc#[/color]', OldVar)). This makes no sense here (see below).
  • To use the OldVarr indexed variables in a Rainmeter code, you have to concatenate the OldVarr with the inc variable: SKIN:Bang('!SetVariable', [color=#FF0000]'OldVarr'..inc[/color], OldVar). Take care what are you including between apostrophes.
  • The OldVar = OldVar line of the if StartCheck ~= 0 then structure (under the Update() function) seem absolutely useless. Doesn't worth to atribute to a variable its own value.
At least for now, I didn't understand what you would like to do with this script.
Yea, I had literally no idea what I was doing yesterday.... Today I have it a little bit more together. Here is the current skin.

The red box should create another Black box every time it is clicked, while the other red box under it, should remove one.

The way I'm doing the removal is bad at the moment as I'm trying to figure out how to edit files with lua.

(Also for some reason I don't understand, every time I hit one the red boxes, it will do the previous step. For example, to create the first black box, I have to click it twice, and to remove one, I need to have at least 2. But I suspect there is something wrong in the 'if" in my lua script.
Last edited by kyriakos876 on September 29th, 2017, 2:54 pm, edited 1 time in total.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 4:25 pm

The point of this is to try and make a skin that adds dynamically meters and removes them as well. I'm doing this, mainly, to learn lua through making skins.
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 4:47 pm

I strongly recommend against this. The biggest problem is that skin .ini files are often, in fact usually, encoded as UTF-16 Little Endian, which Lua can't read.

I just can't go with you on this journey. Trying to use Lua to create skins with dynamic content is just a Manhattan Project level of effort, and is so full of gotchas and pitfalls that I just can't wade into it.
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 4:54 pm

jsmorley wrote:I strongly recommend against this. The biggest problem is that skin .ini files are often, in fact usually, encoded as UTF-16 Little Endian, which Lua can't read.

I just can't go with you on this journey. Trying to use Lua to create skins with dynamic content is just a Manhattan Project level of effort, and is so full of gotchas and pitfalls that I just can't wade into it.
I see....I respect your opinion and you sure know more than I do on the difficulty level, but I am still going to try it anyway. Is it okay with you if I keep asking questions that are, this time, specific? For example, if lua can't read .ini files encoded the way skins are, can it read/write to .txt files or it can only read/write .lua scripts?
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 5:02 pm

kyriakos876 wrote:I see....I respect your opinion and you sure know more than I do on the difficulty level, but I am still going to try it anyway. Is it okay with you if I keep asking questions that are, this time, specific? For example, if lua can't read .ini files encoded the way skins are, can it read/write to .txt files or it can only read/write .lua scripts?
It can read / write to any text file that is encoded as ANSI or UTF-8.

If you have a skin file that is encoded as ANSI for instance, you can do this:

Skin:

Code: Select all

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

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

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

Code: Select all

function Initialize()

	mySkinFile = SKIN:GetVariable('CURRENTPATH')..SKIN:GetVariable('CURRENTFILE')
	
end

function Update()

	fileContent = {}
	
	local file = io.open(mySkinFile, "r");
	local fileContent = {}
	for line in file:lines() do
		table.insert (fileContent, line);
	end
	
	io.close(file)
	
	for index, value in ipairs(fileContent) do
		i, j = string.find(string.lower(value), '^text=')
		if j then
			print('Line '..index..' is: '..fileContent[index])
		end
	end
		
end
I'm reading the entire file into a table, then searching the table for "Text=" at the beginning of a line, and if found, printing the entire line and what line number I found it on.
1.png
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 19245
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Learning Lua.

jsmorley » September 27th, 2017, 5:05 pm

There is more help at:

https://docs.rainmeter.net/tips/unicode-in-rainmeter/

Scroll down to Using Unicode with Lua scripting
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 6:00 pm

jsmorley wrote:There is more help at:

https://docs.rainmeter.net/tips/unicode-in-rainmeter/

Scroll down to Using Unicode with Lua scripting
2 questions.

1) Why does the text in the i, j = string.find(string.lower(value), '^text=') line can't be with Capital "T" as it is in the .ini file?

2)Is it possible to write in a text file without delete everything in it? Because, at the moment, every time I write in a text file, the content of it is deleted and replaced with the line I wanted to add.

This is the example I'm using to write on a text file:

Code: Select all

	local FilePath = SKIN:MakePathAbsolute('TestOut.txt')
	local File = io.open(FilePath, 'w')
	if not File then
		print('LuaUTF8: unable to open file for write at ' .. FilePath)
		return
	end
	
	local BOM = '\239\187\191'
	File:write(BOM)
	File:write('Бешеные псы и англичане')
	File:close()
User avatar
kyriakos876
Posts: 914
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Learning Lua.

kyriakos876 » September 27th, 2017, 6:01 pm

You obviously don't have to answer all my questions... You've already helped me learn more than I would have learned on my own in 5 days...