It is currently April 20th, 2024, 12:12 pm

Sometimes the text become 0

Discuss the use of Lua in Script measures.
konan
Posts: 3
Joined: March 10th, 2020, 3:48 am

Sometimes the text become 0

Post by konan »

I try to make a todo list, but sometimes the text become 0. I didn't change the text. It seems to happen at random. I have uploaded the normal ang error pictures. I don't know where the problem is?
;By the way, I install the Cacheman, I don't know is it something relative?

Here is the code:

todo.ini

Code: Select all

[LoadLua]
Measure=Script
ScriptFile=todo.lua

[MeterBackground]
Meter=Image
W=350
H=100
SolidColor=60,60,60,255

[TextStyle]
FontFace=Inconsolata
FontColor=255,255,255
SolidColor=0,0,0,1
StringAlign=Left
AntiAlias=1

[TodoList]
Meter=String
MeterStyle=TextStyle
MeasureName=LoadLua
X=0
Y=0
W=350
H=100
FontSize=12

todo.lua

Code: Select all

function Update()
    io.input("C:\\Users\\admin\\Documents\\Rainmeter\\Skins\\cc\\todo.md")
    t = io.read("*all")
    return t
end

todo.md is the text, not a number 0.
You do not have the required permissions to view the files attached to this post.
Last edited by Brian on March 10th, 2020, 5:10 am, edited 1 time in total.
Reason: Please use code tags. Its the </> button.
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sometimes the text become 0

Post by balala »

konan wrote: March 10th, 2020, 4:36 am I try to make a todo list, but sometimes the text become 0. I didn't change the text. It seems to happen at random. I have uploaded the normal ang error pictures. I don't know where the problem is?
You get 0 from time to time, when you're refreshing the skin? On some refreshes you get 0 and other times you get the text?
If so, not sure what to say, because I just tried out your code and always get the text. One single suggestion I have, try to replace the io.input("C:\\Users\\admin\\Documents\\Rainmeter\\Skins\\cc\\todo.md") line of the todo.lua script file with io.input("C://Users//admin//Documents//Rainmeter//Skins//cc//todo.md"). I doubt this will help, but anyway probably it worth a try.
If doesn't help, please pack the whole cc config (Skins\cc\) and upload the package, to can give it a try.
konan wrote: March 10th, 2020, 4:36 am ;By the way, I install the Cacheman, I don't know is it something relative?
What Cacheman is? Because I don't know. Post a link please on what have you installed.
konan
Posts: 3
Joined: March 10th, 2020, 3:48 am

Re: Sometimes the text become 0

Post by konan »

balala wrote: March 10th, 2020, 8:30 am You get 0 from time to time, when you're refreshing the skin? On some refreshes you get 0 and other times you get the text?
When I refrech the skin, the text is normal. But wait a minute, the text become 0. It usually need several minutes. I found it happened while I was working, playing games, or doing nothing.

balala wrote: March 10th, 2020, 8:30 am One single suggestion I have, try to replace the io.input("C:\\Users\\admin\\Documents\\Rainmeter\\Skins\\cc\\todo.md") line of the todo.lua script file with io.input("C://Users//admin//Documents//Rainmeter//Skins//cc//todo.md"). I doubt this will help, but anyway probably it worth a try.
I tried it but it didn't work. The 0 still happen.

balala wrote: March 10th, 2020, 8:30 am What Cacheman is? Because I don't know. Post a link please on what have you installed.
The application I recently installed is Cacheman. It seems a little complicated. You can Google it or take a look at their website: https://www.outertech.com
But the first thing is the code. Maybe the application has nothing to do with it. I have upload the package. It is the whole project.
You do not have the required permissions to view the files attached to this post.
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Sometimes the text become 0

Post by Yincognito »

konan wrote: March 10th, 2020, 1:09 pm When I refrech the skin, the text is normal. But wait a minute, the text become 0. It usually need several minutes. I found it happened while I was working, playing games, or doing nothing.

I tried it but it didn't work. The 0 still happen.
I'm not an expert in Lua, but I tried your skin and didn't have any problems: for more than 5 minutes the text remained the same. However, maybe it would help if you try this slightly modified version of your meter:

Code: Select all

[TodoList]
Meter=String
MeterStyle=TextStyle
X=0
Y=0
W=350
H=100
FontSize=12
Text="[LoadLua]"
DynamicVariables=1
What this does is specifically get the string value of [LoadLua] measure using section variables, so, however unlikely, if at any point in the process the skin "forgets" that it has to return a string value (i.e. the text) and not a number value (i.e. 0), this should fix it. No guarantees though... ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sometimes the text become 0

Post by balala »

Yincognito wrote: March 10th, 2020, 6:36 pm but I tried your skin and didn't have any problems: for more than 5 minutes the text remained the same.
But I had. Left it running for a longer time, for instance half an hour. After 5 minutes the skin still shows the string, but if you left it for 30 minutes, then yep, the string indeed become "0".
konan wrote: March 10th, 2020, 4:36 am I don't know where the problem is?
Probably the solution is to change the encoding of both files, cathy.ini and todo.lua. Both are encoded az UTF-8, but they should be UTF-16. Details: https://forum.rainmeter.net/viewtopic.php?f=5&t=34523&start=10#p170918
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Sometimes the text become 0

Post by Yincognito »

balala wrote: March 10th, 2020, 9:30 pm But I had. Left it running for a longer time, for instance half an hour. After 5 minutes the skin still shows the string, but if you left it for 30 minutes, then yep, the string indeed become "0".
Oh, really? Well, I had no idea that you experienced the same issue, since you didn't mentioned that in your posts. If I knew, I would have let my version run for 30+ minutes, to see if it changes anything. Oh, and converting everything to UCS-2 LE BOM actually made things worse: no text, no 0, only some nasty characters there.

UPDATE: I encountered the same issue, after letting the skins (both the original and the UCS-2 LE BOM converted one) run for like an hour or so. The conclusion is that neither the UTF-16 or using section variables to specifically display the string value of the measure solve the issue. Thankfully, as I don't use Lua, I'm not even potentially affected, but for sure the OP is. One workaround that I know will work is to drop Lua for this and just parse the local file using WebParser (and the appropriate CodePage= in its options). If there will also be writes to that file, using an .ini / .inc like structure might be recommended, since the only native way for Rainmeter to write to a file is using the !WriteKeyValue bang (which by default expects such a structure). Personally, I used this method for years without problems.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
Brian
Developer
Posts: 2679
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Sometimes the text become 0

Post by Brian »

Besides the encoding and path separators issues, I assume no one looked at the log where you will see what the problem is. (You may have to enable Debug mode.)

The error is:
Script: todo.lua:2: bad argument #1 to 'input' (C:\Users\admin\Documents\Rainmeter\Skins\cc\todo.md: Too many open files)

You opened a file, but did not close it. Eventually too many open file handles occurred and an error was raised and the file was not opened.

We have a nice little snippet for opening files you can use: https://docs.rainmeter.net/snippets/read-write-file/#ReadFile

So here is how I fixed it (some of this was already mentioned by others above):
  1. Change the encoding of cathy.ini and todo.lua to UTF-16LE (or UCS-2 LE).
  2. Change the encoding of todo.md to UTF-8 BOM. Lua has trouble using UTF-16 encoded text in some situations.
  3. Utilizing the snippet link above, change the contents of your lua file to 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
    
    function Update()
    	return ReadFile("C:\\Users\\admin\\Documents\\Rainmeter\\Skins\\cc\\todo.md") -- You can use a relative path here as well
    end
    
Hopefully that clears things up.

-Brian
User avatar
balala
Rainmeter Sage
Posts: 16148
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Sometimes the text become 0

Post by balala »

Brian wrote: March 11th, 2020, 7:01 am Hopefully that clears things up.
Yep, it definitely does. I saw that error message, but didn't figure out what it does mean. Now it's clear. Thanks for the info.
So, yes, the solution is to close the todo.md file, using the File:close() command at the end of the todo.lua script file, as suggested by Brian above.
konan
Posts: 3
Joined: March 10th, 2020, 3:48 am

Re: Sometimes the text become 0

Post by konan »

I just understood where the error happened. Thank you guys, especially Brian. :thumbup: I will make my todo list more better. :O
User avatar
Yincognito
Rainmeter Sage
Posts: 7128
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Sometimes the text become 0

Post by Yincognito »

Brian wrote: March 11th, 2020, 7:01 am Besides the encoding and path separators issues, I assume no one looked at the log where you will see what the problem is. (You may have to enable Debug mode.)

The error is:
Script: todo.lua:2: bad argument #1 to 'input' (C:\Users\admin\Documents\Rainmeter\Skins\cc\todo.md: Too many open files)

You opened a file, but did not close it. Eventually too many open file handles occurred and an error was raised and the file was not opened.
Many thanks for clearing this up, Brian. I did not look at the log with Debug mode checked, as I assumed the OP knew what he was doing more than me, since as I said, I'm not exactly an expert in Lua. I do know from my programming experience that a file needs to be closed after a read or write operation, but (incorrectly) assumed that the Lua integration in Rainmeter made such a process automatic, without needing user intervention.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth