It is currently March 29th, 2024, 8:36 am

Lua for me.

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

Re: Lua for me.

Post by balala »

kyriakos876 wrote:do you face a problem?
Yep, I do, even more:
  • The skin doesn't show up the data of the removable drives. That's because of the missing IgnoreRemovable=0 option, which should have to be added to all FreeDiskSpace measures. These measures, by default, are ignoring the removable drives. To change this you have to add this option to the measures. Would be great if you'd add it, because by default the skin shows 0 for some of my disks, which are removable.
  • On the other hand if I remove a removable drive, I don't see immediately de change in the skin. Especially that I have to click to the red button, to rewrite the file. I think this should have to go on automatically, without click.
  • Would be great if the drives would be identified (by the appropriate letters) on the skin. Because right now I have there some numbers, but which one what does mean, well, this is a good question.
  • And one last, which probably is less important, however I'd change this, too: I wouldn't use a .txt file, instead a .inc. It would be much more good, I think.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

I just want to inject an alternate view: aren't you overcomplicating this a lot? It would be waaaay easier to just include a measure for every disk to tell whether it's connected or not, then use if conditions to set what disks are shown. You could either brute-force it and include the full set for every disk (which is what I did with my Disks Meter), or you could have a limited set (say, five sets of measures and meters) that would monitor different disks depending on variables that are set in the ifconditions.

That seems way easier than completely rewriting the file every time a disk is added or removed...

I would post some code samples for you but I'm at work at the moment.
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Lua for me.

Post by balala »

raiguard wrote:I just want to inject an alternate view: aren't you overcomplicating this a lot? It would be waaaay easier to just include a measure for every disk to tell whether it's connected or not, then use if conditions to set what disks are shown. You could either brute-force it and include the full set for every disk (which is what I did with my Disks Meter), or you could have a limited set (say, five sets of measures and meters) that would monitor different disks depending on variables that are set in the ifconditions.
Yes, I also used a such approach a while ago and it definitely does work. But this was kyriakos876's approach and it can work as well. Just the lua script / skin isn't created in a proper way. At least from my point of view they aren't.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

balala wrote:Yep, I do, even more:
  • The skin doesn't show up the data of the removable drives. That's because of the missing IgnoreRemovable=0 option, which should have to be added to all FreeDiskSpace measures. These measures, by default, are ignoring the removable drives. To change this you have to add this option to the measures. Would be great if you'd add it, because by default the skin shows 0 for some of my disks, which are removable.
  • On the other hand if I remove a removable drive, I don't see immediately de change in the skin. Especially that I have to click to the red button, to rewrite the file. I think this should have to go on automatically, without click.
  • Would be great if the drives would be identified (by the appropriate letters) on the skin. Because right now I have there some numbers, but which one what does mean, well, this is a good question.
  • And one last, which probably is less important, however I'd change this, too: I wouldn't use a .txt file, instead a .inc. It would be much more good, I think.
The skin is far from working... At the moment I'm trying to have the disks displayed but there's many things not working... The fact that you don't see imidiate changes is one of the last things to add to the code... Also you can only open and rewrite on a file without deleting everything only if it's in .txt format
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

raiguard wrote:I just want to inject an alternate view: aren't you overcomplicating this a lot? It would be waaaay easier to just include a measure for every disk to tell whether it's connected or not, then use if conditions to set what disks are shown. You could either brute-force it and include the full set for every disk (which is what I did with my Disks Meter), or you could have a limited set (say, five sets of measures and meters) that would monitor different disks depending on variables that are set in the ifconditions.

That seems way easier than completely rewriting the file every time a disk is added or removed...

I would post some code samples for you but I'm at work at the moment.
When you want it for a server the only thing that will work is rewriting the whole thing. There's more than 100 disks in the server I'm not gonna write all 100 meters and measures nor am I going to edit it Everytime something changes. Also if I make this work (it's only a matter of time) it will apply to every single PC and you won't have to do anything. Run the skin and whether you have 1 or 101 disks you're good to go.

Again as I said above, this is a very alpha stage so alpha you could go back to Omega lol... I only posted because balala asked for it, I knew it was going to raise confusion.

But yeah, if you want do post your code when you're back. Though I think it won't be what I want, I might grab something off of it.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Lua for me.

Post by kyriakos876 »

Let me add that I know this could be as simple as 2 measures and 2 meters, it's just that I don't want it to more boring to look at than it has to be. Hence the animations etc...
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Lua for me.

Post by raiguard »

I see now why you're using this method, I apologize if I seemed rude. The method I was going to suggest won't work in this case.

However, I do have another suggestion. I have gone ahead and downloaded the .RMSKIN you provided and stripped it apart. I realized that a tool that I made for a completely separate purpose could be used here. I have attached the new .RMSKIN to this reply.

The gist of the changes I made are to no longer rely on !WriteKeyValue bangs (except for the creation of the Underliner_Width variable). Instead, my script reads in a template file and parses it, replacing every instance of a specific marker (^1^ in this case) with the first disk letter. It then goes and does the same with all of the disk letters contained in the AllDisks variable. Once finished, it writes the result into the output file and saves it.

This has numerous advantages over your !WriteKeyValue method: first, you can edit the template just like a normal skin, significantly decreasing the amount of work that goes into changing or adding an option; second, it is much more reliable and doesn't require you to escape the variables.

I hope I explained that clearly. If you need any help on how the script works (I jury-rigged it to work for this specific usecase, so it is kinda messy and has a lot of excess code), feel free to shoot me a reply.

Another important change is that rather than using the script update function to update the contents of AllDisks, I simply pass it as an option on the script measure, and only have the LUA retrieve it when it actually needs it.

Here's the code for the script:

Code: Select all

debug = false

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
revAlphabet = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'

identifierString = '%^(#)%^'

function Initialize() 

  cTable = newT()
  bTable = newT()
  keyIndex = 0
  tableLength = 0

end

function Update() end

function Split()

  local lTable = {}
  SELF:GetOption('AllDisks'):gsub(".", function(c)
    table.insert(lTable, c)
    SKIN:Bang(string.format('!WriteKeyValue Variables "Underliner_Width_%s" "0" "#@#Variables.inc"',c))
  end)
  LogHelper(SELF:GetOption('AllDisks'))
  AddTable{ type = 'custom', customTable = lTable }
  Concat()

end

function Concat()
  
  local rTable = {}
  local lines = {}
  for line in io.lines(SELF:GetOption('templateFilePath')) do
    table.insert(rTable, line)
  end

  for i = 1, tableLength do 
    for l,line in pairs(rTable) do
      for k,v in cTable:opairs() do
        if v[i] ~= nil then line = line:gsub(k, v[i]) end
      end
      table.insert(lines, line)
    end
  end

  outputFile = io.open(SELF:GetOption('outputFilePath'), 'w')
  outputFile:write(table.concat(lines, '\n'))
  outputFile:close()

end

function AddTable(args)

  keyIndex = keyIndex + 1
  cTable[identifierString:gsub('%(#%)', keyIndex)] = BuildTable(args)

end

function BuildTable(args)

  local type = args.type
  local start = args.start
  local finish = args.finish
  local customTable = args.customTable
  local lTable = {}

  if type == 'alphabetical' then
    alphabet:gsub(".", function(c)
      table.insert(lTable, c)
    end)
  elseif type == 'revAlphabetical' then
    revAlphabet:gsub(".", function(c)
      table.insert(lTable, c)
    end)
  elseif type == 'numerical' then
    for i = start,finish do
      table.insert(lTable, i)
    end
  elseif type == 'revNumerical' then
    for i = start,finish,-1 do
      table.insert(lTable, i)
    end
  elseif type == 'custom' then
    lTable = customTable
  end

  if tablelength(lTable) > tableLength then tableLength = tablelength(lTable) end

  return lTable

end

-- function to make logging messages less cluttered
function LogHelper(message, type)

  if type == nil then type = 'Debug' end

  if debug == true then
    SKIN:Bang("!Log", message, type)
  elseif type ~= 'Debug' then
  	SKIN:Bang("!Log", message, type)
	end

end

-- simple ordered table (http://lua-users.org/wiki/OrderedTableSimple)
function newT( t )
   local mt = {}
   -- set methods
   mt.__index = {
      -- set key order table inside __index for faster lookup
      _korder = {},
      -- traversal of hidden values
      hidden = function() return pairs( mt.__index ) end,
      -- traversal of table ordered: returning index, key
      ipairs = function( self ) return ipairs( self._korder ) end,
      -- traversal of table
      pairs = function( self ) return pairs( self ) end,
      -- traversal of table ordered: returning key,value
      opairs = function( self )
         local i = 0
         local function iter( self )
            i = i + 1
            local k = self._korder[i]
            if k then
               return k,self[k]
            end
         end
         return iter,self
      end,
      -- to be able to delete entries we must write a delete function
      del = function( self,key )
         if self[key] then
            self[key] = nil
            for i,k in ipairs( self._korder ) do
               if k == key then
                  table.remove( self._korder, i )
                  return
               end
            end
         end
      end,
   }
   -- set new index handling
   mt.__newindex = function( self,k,v )
      if k ~= "del" and v then
         rawset( self,k,v )
         table.insert( self._korder, k )
      end      
   end
   return setmetatable( t or {},mt )
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
You do not have the required permissions to view the files attached to this post.
”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:

Code: Select all

debug = false

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
revAlphabet = 'ZYXWVUTSRQPONMLKJIHGFEDCBA'

identifierString = '%^(#)%^'

function Initialize() 

  cTable = newT()
  bTable = newT()
  keyIndex = 0
  tableLength = 0

end

function Update() end

function Split()

  local lTable = {}
  SELF:GetOption('AllDisks'):gsub(".", function(c)
    table.insert(lTable, c)
    SKIN:Bang(string.format('!WriteKeyValue Variables "Underliner_Width_%s" "0" "#@#Variables.inc"',c))
  end)
  LogHelper(SELF:GetOption('AllDisks'))
  AddTable{ type = 'custom', customTable = lTable }
  Concat()

end

function Concat()
  
  local rTable = {}
  local lines = {}
  for line in io.lines(SELF:GetOption('templateFilePath')) do
    table.insert(rTable, line)
  end

  for i = 1, tableLength do 
    for l,line in pairs(rTable) do
      for k,v in cTable:opairs() do
        if v[i] ~= nil then line = line:gsub(k, v[i]) end
      end
      table.insert(lines, line)
    end
  end

  outputFile = io.open(SELF:GetOption('outputFilePath'), 'w')
  outputFile:write(table.concat(lines, '\n'))
  outputFile:close()

end

function AddTable(args)

  keyIndex = keyIndex + 1
  cTable[identifierString:gsub('%(#%)', keyIndex)] = BuildTable(args)

end

function BuildTable(args)

  local type = args.type
  local start = args.start
  local finish = args.finish
  local customTable = args.customTable
  local lTable = {}

  if type == 'alphabetical' then
    alphabet:gsub(".", function(c)
      table.insert(lTable, c)
    end)
  elseif type == 'revAlphabetical' then
    revAlphabet:gsub(".", function(c)
      table.insert(lTable, c)
    end)
  elseif type == 'numerical' then
    for i = start,finish do
      table.insert(lTable, i)
    end
  elseif type == 'revNumerical' then
    for i = start,finish,-1 do
      table.insert(lTable, i)
    end
  elseif type == 'custom' then
    lTable = customTable
  end

  if tablelength(lTable) > tableLength then tableLength = tablelength(lTable) end

  return lTable

end

-- function to make logging messages less cluttered
function LogHelper(message, type)

  if type == nil then type = 'Debug' end

  if debug == true then
    SKIN:Bang("!Log", message, type)
  elseif type ~= 'Debug' then
  	SKIN:Bang("!Log", message, type)
	end

end

-- simple ordered table (http://lua-users.org/wiki/OrderedTableSimple)
function newT( t )
   local mt = {}
   -- set methods
   mt.__index = {
      -- set key order table inside __index for faster lookup
      _korder = {},
      -- traversal of hidden values
      hidden = function() return pairs( mt.__index ) end,
      -- traversal of table ordered: returning index, key
      ipairs = function( self ) return ipairs( self._korder ) end,
      -- traversal of table
      pairs = function( self ) return pairs( self ) end,
      -- traversal of table ordered: returning key,value
      opairs = function( self )
         local i = 0
         local function iter( self )
            i = i + 1
            local k = self._korder[i]
            if k then
               return k,self[k]
            end
         end
         return iter,self
      end,
      -- to be able to delete entries we must write a delete function
      del = function( self,key )
         if self[key] then
            self[key] = nil
            for i,k in ipairs( self._korder ) do
               if k == key then
                  table.remove( self._korder, i )
                  return
               end
            end
         end
      end,
   }
   -- set new index handling
   mt.__newindex = function( self,k,v )
      if k ~= "del" and v then
         rawset( self,k,v )
         table.insert( self._korder, k )
      end      
   end
   return setmetatable( t or {},mt )
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
You did not seem rude and excuse me if I made you feel that you did.

That's nice, I'm going to steal some parts from here however, one thing you didn't consider is that the alphabet has 24 letters, when windows runs out you mount the drives on a point or path. My solution at the moment only support only letters from the alphabet alone like yours, but I can easily change that bu changing what to search for. You on the other hand created a table assuming that each letter holds "position" in the table which will not be the case. Now this complicates things other was as well if you know what I mean... I can change it obviously so that drive names/locations are converted to numbers, but this over-complicates things imho.
Again, my code is on a very initial stage of it's form. There's even more things to consider but at the moment I'm trying to fight with what I know on lua as well as try to understand this awful documentary of lua's.
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:You did not seem rude and excuse me if I made you feel that you did.

That's nice, I'm going to steal some parts from here however, one thing you didn't consider is that the alphabet has 24 letters, when windows runs out you mount the drives on a point or path. My solution at the moment only support only letters from the alphabet alone like yours, but I can easily change that bu changing what to search for. You on the other hand created a table assuming that each letter holds "position" in the table which will not be the case. Now this complicates things other was as well if you know what I mean... I can change it obviously so that drive names/locations are converted to numbers, but this over-complicates things imho.
Again, my code is on a very initial stage of it's form. There's even more things to consider but at the moment I'm trying to fight with what I know on lua as well as try to understand this awful documentary of lua's.
Nah it wasn't you, I'm just paranoid about offending people.

My solution actually supports any and all combinations of letters and endpoints, assuming you can somehow separate them with a common separator. You'd just need to change the gsub slightly to accommodate.

I have a moderate amount of experience with LUA so hopefully I can be of some use to you if you ask for it.

Edit: just to be clear, the alphabet and revAlphabet variables at the top are part of the "excess code" that I mentioned. If you want I can provide a stripped down version of the script so you can look at the parts only pertaining to your use case. It shouldn't take very long, but I have to sleep first.
”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:Nah it wasn't you, I'm just paranoid about offending people.

My solution actually supports any and all combinations of letters and endpoints, assuming you can somehow separate them with a common separator. You'd just need to change the gsub slightly to accommodate.

I have a moderate amount of experience with LUA so hopefully I can be of some use to you if you ask for it.
I personally can only be offended from a family member, so unless you are my mysterious brother, you won't have to worry about that. Though it's 2018, people might get offended because you offered them just water instead of sparkling water so you're only normal if you're paranoid about that nowadays lol.

I'm gonna have to work on the separating thingy indeed... I have an idea thought which I tried and kinda worked so I moved on.
I see you are fluent in lua so maybe you can riddle me this:

Code: Select all

SKIN:Bang(string.format('!WriteKeyValue DiskAnimation%s "IconAnimationIn" "[!SetVariable Underliner_Width_%s (Clamp(#Underliner_Width_%s#+25,0,(#MaxWidth#-[MeterDisk%s_Name:X]*2)))][!UpdateMeter Underliner_%s][!UpdateMeasure #CurrentSection#][!Redraw]" "#@#FileToEdit.txt"',i,i,i,i,i))
Why does this bang won't write the #CurrentSection# pramater in the [!UpdateMeasure #CurrentSection#] section but instead it leaves it blank?

and in this:

Code: Select all

SKIN:Bang(string.format('!WriteKeyValue Background_Disk_%s "MouseLeaveAction" "[!CommandMeasure DiskAnimation%s "Stop 1"][!CommandMeasure DiskAnimation%s "Execute 2"]" "#@#FileToEdit.txt"',i,i,i))
there's some syntax stuff going on in the "Stop 1" and "Execute 2" parameters because it wont write the MouseLeaveAction section at all, but when I erase the above, it does write it.


In both case, I tried escaping some double quotes or some hashtags, but, I either didn't escape the correct things, or that's not what's going on....

If you could help me here, that'd be great.