It is currently March 28th, 2024, 2:16 pm

Random stringindex problem - for "quote of the day"

Get help with creating, editing & fixing problems with skins
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Random stringindex problem - for "quote of the day"

Post by jsmorley »

To follow up on my comment in my last post:

Using your text file exactly as it is as Test.txt...

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[Variables]
Font=Arial

[MeasureLuaScript]
Measure=Script
ScriptFile=Test.lua
UpdateDivider=30

[QuoteText]
Meter=STRING
X=1
Y=1
W=800
H=600
FontColor=255,255,255,255
FontSize=15
StringAlign=Left
StringStyle=BOLD
FontFace=#Font#
StringEffect=Shadow
FontEffectColor=0,0,0,140
Antialias=1
ClipString=1
Text=Reading...
Test.lua:

Code: Select all

function Initialize()

	CURRENTPATH = SKIN:GetVariable('CURRENTPATH')
	Quotes = {}
	i = 0

	for line in io.lines(CURRENTPATH..'Test.txt') do
		if string.sub(line, 1, 7) == '<title>' then
			i = i + 1
			Quotes[i] = string.sub(line, 8)
		else
			Quotes[i] = Quotes[i]..'#CRLF#'..line
		end
	end
	
end -->Initialize

function Update()

	rand = math.random(1, #Quotes)
	SKIN:Bang('!SetOption QuoteText Text """'..Quotes[rand]..'"""')
	
	return 'Quote number: '..rand
	
end -->Update
4-30-2012 11-39-30 PM.jpg
What this does is initialize the Lua script when the skin is loaded or refreshed. During the initialization, the Lua reads in the entire text file, and creates a table with one full quote per "row" of the table. It adds Rainmeter #CRLF# (carriage return / line feed) commands where they are needed so the line breaking stays the same as the text file.

Then on each update of the Lua script (every 30 seconds in my sample) it generates a random number between 1 and the total number of quotes (#Quotes = Total number of items in the table) and sets the Text= option of the meter with the selected random quote.

One measure in the skin instead of 100 or more...

Let me know if you have any interest in this approach and need any questions answered about what or why or how it works.
dragonmage
Developer
Posts: 1270
Joined: April 3rd, 2009, 4:31 am
Location: NC, US

Re: Random stringindex problem - for "quote of the day"

Post by dragonmage »

Well, if you are going to go Lua why don't you break it up in quote and author so you can use two different string meters? That way you could have the one line bolded or even in a different font.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Random stringindex problem - for "quote of the day"

Post by jsmorley »

dragonmage wrote:Well, if you are going to go Lua why don't you break it up in quote and author so you can use two different string meters? That way you could have the one line bolded or even in a different font.
Go for it... I resisted that as not every quote has an author, and it is not certain that a line that starts with "-something" is an "author line". The only really certain thing is that each quote starts with "<title>". While it might not be perfect for those reasons, it certainly would not be rocket science to do that and create two tables (Quotes and Authors) and send two bangs to two different meters.

Be a lot easier if the file was formatted:

<title>I drank what?
<author>Socrates
djan3091
Posts: 3
Joined: April 30th, 2012, 3:58 am

Re: Random stringindex problem - for "quote of the day"

Post by djan3091 »

You are a genius! Jeez. Thanks - this is awesome stuff. :thumbup: :D

I tried a lua file earlier, but failed miserably haha.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Random stringindex problem - for "quote of the day"

Post by jsmorley »

djan3091 wrote:You are a genius! Jeez. Thanks - this is awesome stuff. :thumbup: :D

I tried a lua file earlier, but failed miserably haha.
Glad to help. This is just the kind of thing that Lua is best at. It has very powerful and flexible file reading / writing capabilities, has "organizing" things in tables built into its DNA, and is really good at searching / chopping apart / replacing and generally doing magic with "strings".
Post Reply