It is currently April 18th, 2024, 11:53 pm

Lua for me.

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:Glad to help! To be honest, though, I wasn't completely doing this just to help you - I plan on implementing this method in my own Disks Meter. Right now I'm using the "brute-force" method I mentioned before, where I have measures and meters for every disk baked-in. That causes the INI to be almost 6000 lines long, which absolutely drains Rainmeter's performance every time it updates. This will allow me to make Disks Meter way more performance-friendly, especially for older computers/laptops.
Okay I feel better knowing you gained something from it too. Yea, no.... 6000 lines is way too much for Rainmeter to be friendly for your cpu usage... (now gpu too)
raiguard wrote: Also, the only thing you need to do to measure removable drives is to add IgnoreRemovable=0 to the total and used disk space measures.
Yup, just read this on the docs. It works indeed.
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua for me.

Post by balala »

kyriakos876 wrote:Yup, just read this on the docs. It works indeed.
Have you read my first point here: https://forum.rainmeter.net/viewtopic.php?p=152787#p152787?
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

balala wrote:Have you read my first point here: https://forum.rainmeter.net/viewtopic.php?p=152787#p152787?
Man... I feel stupid... before I missed the skin raiguard attached with his reply, now I lose a whole bullet point... I'm either aging faster than a normal 21 y.o. does, or I need to sleep more these days. Excuse my lack of awareness please!
User avatar
balala
Rainmeter Sage
Posts: 16144
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua for me.

Post by balala »

kyriakos876 wrote:Man... I feel stupid... before I missed the skin raiguard attached with his reply, now I lose a whole bullet point... I'm either aging faster than a normal 21 y.o. does, or I need to sleep more these days. Excuse my lack of awareness please!
Don't feel stupid and don't excuse yourself. I think it happens to all of us sometimes, to miss something, which later, we feel was important. At least it happens often to me.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

Hey raiguard, I have a question.

I have this:

Code: Select all


Microsoft DiskPart version 10.0.17134.1

Copyright (C) Microsoft Corporation.
On computer: somepc

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          465 GB      0 B        *
  Disk 1    Online          465 GB  2048 KB        *
  Disk 2    Online          931 GB      0 B         
  .
  .
  .
  .

in a file at #@#CMDOutputDisks.txt

I want to read the whole thing, stop at the first - and then for every Disk # that is encounter to save the whole line in a table. I've come this far:

Code: Select all

function Initialize()
	
	myTextFile = SKIN:GetVariable('@')..'CMDOutputDisks.txt'

end

function GetdisksCMD()

	fileContent = {}
		local file = io.open(myTextFile, '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), string.format('.')
		if j then
	
			for i=0, 15 do
				fileContent[index+i] = line
			end
			
			
		end
	
	end

	file = io.open(myTextFile, 'w')

	for index, value in ipairs(fileContent) do
		file:write(value..'\n')
	end
    
	io.close(file)
	
end
which basically deletes the 15 lines after a . but I don't know how to save the line to another file... (don't ask why it deletes stuff... I'm just trying stuff out, this is how far I've come)
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

Here is what I came up with:

Code: Select all

function Initialize()
   
   inputFile = SKIN:GetVariable('@')..'LuaDisksInput.txt'
   outputFile = SKIN:GetVariable('@')..'LuaDisksOutput.txt'

end

function GetdisksCMD()

    local lines = {}
    for line in io.lines(inputFile) do
        if string.match(line, '^%s*Disk %d* .*$') ~= nil then table.insert(lines, line) end
    end

    local output = io.open(outputFile, 'w')
    output:write(table.concat(lines, '\n'))
    output:close()
   
end
Basically, this iterates over every line in the file, trying to match each line to ^%s*Disk %d* .*$. This regex looks for, on a single line, any amount of whitespace characters, then Disk (one or more digits), then a space, then literally anything else before the end of the line. If it matches this, then it'll save that line to another table. Once it has iterated over the entire input, it will write the contents of the table to the output file, like this:

Code: Select all

  Disk 0    Online          465 GB      0 B        *
  Disk 1    Online          465 GB  2048 KB        *
  Disk 2    Online          931 GB      0 B         
Is this what you were looking for?
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:Here is what I came up with:

Code: Select all

function Initialize()
   
   inputFile = SKIN:GetVariable('@')..'LuaDisksInput.txt'
   outputFile = SKIN:GetVariable('@')..'LuaDisksOutput.txt'

end

function GetdisksCMD()

    local lines = {}
    for line in io.lines(inputFile) do
        if string.match(line, '^%s*Disk %d* .*$') ~= nil then table.insert(lines, line) end
    end

    local output = io.open(outputFile, 'w')
    output:write(table.concat(lines, '\n'))
    output:close()
   
end
Basically, this iterates over every line in the file, trying to match each line to ^%s*Disk %d* .*$. This regex looks for, on a single line, any amount of whitespace characters, then Disk (one or more digits), then a space, then literally anything else before the end of the line. If it matches this, then it'll save that line to another table. Once it has iterated over the entire input, it will write the contents of the table to the output file, like this:

Code: Select all

  Disk 0    Online          465 GB      0 B        *
  Disk 1    Online          465 GB  2048 KB        *
  Disk 2    Online          931 GB      0 B         
Is this what you were looking for?
Oh that's it alright... Thanks again! One (probably) last question that I have is:

in your concat.lua there's this function:

Code: Select all

function Split(allDisks)

	local BackgroundHeight = 65
	allDisks:gsub(".", function(c)
		table.insert(cTable, c)
		SKIN:Bang(string.format('!WriteKeyValue Variables "Underliner_Width_%s" "0" "#@#Variables.inc"',c))
	end)

  Concat()
  SKIN:Bang('!Refresh')

end

which, if I'm not mistaken, creates the table with the included disks (A,B,C,D, etc..) why is it that when I print it I get something like: table: 000001495BAE8DB0?
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:Oh that's it alright... Thanks again! One (probably) last question that I have is:

in your concat.lua there's this function:

Code: Select all

function Split(allDisks)

	local BackgroundHeight = 65
	allDisks:gsub(".", function(c)
		table.insert(cTable, c)
		SKIN:Bang(string.format('!WriteKeyValue Variables "Underliner_Width_%s" "0" "#@#Variables.inc"',c))
	end)

  Concat()
  SKIN:Bang('!Refresh')

end

which, if I'm not mistaken, creates the table with the included disks (A,B,C,D, etc..) why is it that when I print it I get something like: table: 000001495BAE8DB0?
That's normal. You can't just straight-up print a table, because it's not a string object. You have to iterate over the table and print the contents manually.

I actually made a function to do this a while back, here it is:

Code: Select all

-- prints the entire contents of a table to the Rainmeter log
function PrintTable(table)
  for k,v in pairs(table) do
    if type(v) == 'table' then
      local pI = printIndent
      print(printIndent .. k .. ':')
      printIndent = printIndent .. '  '
      PrintTable(v)
      printIndent = pI
    else
      print(printIndent .. k .. ': ' .. v)
    end
  end
end
EDIT: Removed accidental LogHelper reference that I thought I had removed. :/
Before the script will work you need to put printIndent = '' either in the initialize function or in the global area.

I'm actually really proud of this script, because it's the first time I've ever successfully used recursion in programming. Feel free to use it if you want!
Last edited by raiguard on September 29th, 2018, 7:07 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
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:That's normal. You can't just straight-up print a table, because it's not a string object. You have to iterate over the table and print the contents manually.
Does that apply for all tables? Even if the values are numbers? Because I'm pretty sure I used to print a table with numbers in it in the log just by using print(tablesname)
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:Does that apply for all tables? Even if the values are numbers? Because I'm pretty sure I used to print a table with numbers in it in the log just by using print(tablesname)
Hmm, are you sure? I've never had any luck printing tables straight-up. That would certainly be interesting if it's possible.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017