Re: Tips & Tricks Thread
Posted: September 13th, 2012, 5:31 pm
What would you do with that?
Generating readable tables or have you ever triedMerlinTheRed wrote:What would you do with that?
Code: Select all
return function(SKIN)
local ms = {
__index = function(table,key)
-- catch recursive call
if key == '__section' and #table < 2 then
return
elseif key == '__sectionname' and #table < 2 then
return
-- catch update()
elseif key == 'update' and SKIN:GetMeasure(table.__sectionname) then
return function() SKIN:Bang('!UpdateMeasure',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end
-- catch update()
elseif key == 'update' and SKIN:GetMeter(table.__sectionname) then
return function() SKIN:Bang('!UpdateMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end
-- catch isMeter()
elseif key == 'isMeter' then
return function() return SKIN:GetMeter(table.__sectionname) and true or false end
-- catch isMeter()
elseif key == 'isMeasure' then
return function() return SKIN:GetMeasure(table.__sectionname) and true or false end
-- catch hide()
elseif key == 'hide' and SKIN:GetMeter(table.__sectionname) then
return function() SKIN:Bang('!HideMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end
-- catch show()
elseif key == 'show' and SKIN:GetMeter(table.__sectionname) then
return function() SKIN:Bang('!ShowMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end
-- catch Rainmeter Native Build-In functions
-- Show, Hide, SetXYWH, GetXYWH, GetName, GetOption (though special case), Enable, Disable, GetValueRange, GetRelativeValue, GetMaxValue,
elseif table.__section[key] then
return function(...) return table.__section[key](table.__section,...) end
-- catch meter options
elseif table.__section.GetOption then
return table.__section:GetOption(key)
-- catch measure options
elseif table.__section.GetNumberOption then
return (table.__section.GetNumberOption and table.__section:GetNumberOption(key,nil))
-- unknown case
else
print('Unkown Section Case: ' .. key)
return
end
end,
__newindex = function(table,key,value) SKIN:Bang('!SetOption',table.__sectionname,key,value,SKIN:GetVariable('CURRENTCONFIG')) end
}
local sections = {
__index = function(table,key)
if key == 'redraw' then
return function() SKIN:Bang('!Redraw', SKIN:GetVariable('CURRENTCONFIG')) end
else
sections[key] = {}
-- store meter/measure
sections[key].__section = SKIN:GetMeasure(key) or SKIN:GetMeter(key)
-- store meter/measurename
sections[key].__sectionname = key
setmetatable(sections[key],ms)
return sections[key]
end
end,
isMeter = function(meter)
return SKIN:GetMeter(meter) and true or false
end,
isMeasure = function(measure)
return SKIN:GetMeasure(measure) and true or false
end,
toggleGroup = function(meterGroup)
SKIN:Bang('!ToggleMeterGroup', meterGroup,SKIN:GetVariable('CURRENTCONFIG'))
end
}
local variables = {
__index = function(table,key)
-- catch variables.ReplaceVariables()
if key == 'ReplaceVariables' then
return function(param) return SKIN:ReplaceVariables(param) end
-- catch variable
elseif SKIN:GetVariable(key) then
return SKIN:GetVariable(key)
-- unknown case
else
print('Unkown Variable Case: ' .. key)
return
end
end,
__newindex = function(table,key,value) SKIN:Bang('!SetVariable',key,value, SKIN:GetVariable('CURRENTCONFIG')) end
}
setmetatable(variables,variables)
setmetatable(sections,sections)
return sections, sections, variables
end
thatsIch wrote:Code: Select all
return function(SKIN) local ms = { __index = function(table,key) -- catch recursive call if key == '__section' and #table < 2 then return elseif key == '__sectionname' and #table < 2 then return -- catch update() elseif key == 'update' and SKIN:GetMeasure(table.__sectionname) then return function() SKIN:Bang('!UpdateMeasure',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end -- catch update() elseif key == 'update' and SKIN:GetMeter(table.__sectionname) then return function() SKIN:Bang('!UpdateMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end -- catch isMeter() elseif key == 'isMeter' then return function() return SKIN:GetMeter(table.__sectionname) and true or false end -- catch isMeter() elseif key == 'isMeasure' then return function() return SKIN:GetMeasure(table.__sectionname) and true or false end -- catch hide() elseif key == 'hide' and SKIN:GetMeter(table.__sectionname) then return function() SKIN:Bang('!HideMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end -- catch show() elseif key == 'show' and SKIN:GetMeter(table.__sectionname) then return function() SKIN:Bang('!ShowMeter',table.__sectionname, SKIN:GetVariable('CURRENTCONFIG')) end -- catch Rainmeter Native Build-In functions -- Show, Hide, SetXYWH, GetXYWH, GetName, GetOption (though special case), Enable, Disable, GetValueRange, GetRelativeValue, GetMaxValue, elseif table.__section[key] then return function(...) return table.__section[key](table.__section,...) end -- catch meter options elseif table.__section.GetOption then return table.__section:GetOption(key) -- catch measure options elseif table.__section.GetNumberOption then return (table.__section.GetNumberOption and table.__section:GetNumberOption(key,nil)) -- unknown case else print('Unkown Section Case: ' .. key) return end end, __newindex = function(table,key,value) SKIN:Bang('!SetOption',table.__sectionname,key,value,SKIN:GetVariable('CURRENTCONFIG')) end } local sections = { __index = function(table,key) if key == 'redraw' then return function() SKIN:Bang('!Redraw', SKIN:GetVariable('CURRENTCONFIG')) end else sections[key] = {} -- store meter/measure sections[key].__section = SKIN:GetMeasure(key) or SKIN:GetMeter(key) -- store meter/measurename sections[key].__sectionname = key setmetatable(sections[key],ms) return sections[key] end end, isMeter = function(meter) return SKIN:GetMeter(meter) and true or false end, isMeasure = function(measure) return SKIN:GetMeasure(measure) and true or false end, toggleGroup = function(meterGroup) SKIN:Bang('!ToggleMeterGroup', meterGroup,SKIN:GetVariable('CURRENTCONFIG')) end } local variables = { __index = function(table,key) -- catch variables.ReplaceVariables() if key == 'ReplaceVariables' then return function(param) return SKIN:ReplaceVariables(param) end -- catch variable elseif SKIN:GetVariable(key) then return SKIN:GetVariable(key) -- unknown case else print('Unkown Variable Case: ' .. key) return end end, __newindex = function(table,key,value) SKIN:Bang('!SetVariable',key,value, SKIN:GetVariable('CURRENTCONFIG')) end } setmetatable(variables,variables) setmetatable(sections,sections) return sections, sections, variables end
Code: Select all
function meta()
local ms = {__index = function(tb,key) if key == 'UpdateMeasure' then return function() SKIN:Bang('!UpdateMeasure',tb.__sectionname) end elseif tb.__section[key] then return function(...) return tb.__section[key](tb.__section,...) end else return (tb.__section.GetNumberOption and tb.__section:GetNumberOption(key,nil)) or tb.__section:GetOption(key) end end, __newindex = function(tb,key,value) SKIN:Bang('!SetOption',tb.__sectionname,key,value) end}
sections,variables = {__index = function(tb,key) sections[key]={} sections[key].__section, sections[key].__sectionname = SKIN:GetMeasure(key), key setmetatable(sections[key],ms) return sections[key] end},{__index = function(tb,key) return SKIN:GetVariable(key) end, __newindex = function(tb,key,value) SKIN:Bang('!SetVariable',key,value) end}
setmetatable(variables,variables)
setmetatable(sections,sections)
meters, measures = sections, sections
end
Code: Select all
[Rainmeter]
Update=1000
;Encoded in UCS-2 LE BOM with Notepad++
[Metadata]
Name=FolderContent
Author=kounger
Information=A test skin which shows a replacement for io.popen in lua.
Version=1.0
License=CC BY-NC-SA 3.0
;Every 10 seconds this measure returns a new random text file from the folder #@#TestFolders
[MeasureRandomTextFile]
Measure=Plugin
Plugin=QuotePlugin
PathName=#@#TestFolders
Subfolders=1
FileFilter=*.txt
UpdateDivider=10
OnUpdateAction=[!UpdateMeasure "MeasureTextFileFolder"]
DynamicVariables=1
;This measure gets the path to the folder of the file [MeasureRandomTextFile]
;and then calls [MeasureRunFolderContent] to run.
;"'":"''" to escape single quotes ' for cmd
[MeasureTextFileFolder]
Measure=String
String=[MeasureRandomTextFile]
RegExpSubstitute=1
Substitute="\\[^\\]*$":"","'":"''"
UpdateDivider=-1
DynamicVariables=1
OnUpdateAction=[!UpdateMeasure MeasureRunFolderContent][!CommandMeasure MeasureRunFolderContent "Run"]
;This measure uses a powershell script to create a text file which contains the paths of all files that can be found inside the folder [MeasureTextFileFolder].
;1st Argument: Path to the powershell script.
;2nd Argument: Path(s) to the folder(s) whose files should be listed. Multiple folder paths have to be listed with a comma and no spaces. 'Path1','Path2'
;3rd Argument: 'true' for recursive listing of files. 'false' to only list the files from this one folder.
;4th Argument: 'true' to append the output to the output file. 'false' to overwrite the output file.
;5th Argument: Path to the output file.
;OutputType=ANSI: ANSI is necessary to be able to read error meassages inside the rainmeter logs which the script may returns.
[MeasureRunFolderContent]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell.exe
Parameter=-NoProfile -ExecutionPolicy Bypass -Command "& '#@#\FolderContent.ps1' '[MeasureTextFileFolder]' 'false' 'false' '#@#\OutputFile.txt'"
OutputType=ANSI
State=Hide
FinishAction=[!UpdateMeasure MeasureLuaFileList]
DynamicVariables=1
;This measure runs a lua script that returns a list of filenames
;by using a list of file paths from the text file #@#\OutputFile.txt.
[MeasureLuaFileList]
Measure=Script
ScriptFile=#@#ListFiles.lua
TableName=NoPath
UpdateDivider=-1
OnUpdateAction=[!UpdateMeter MeterTextFileList][!Redraw]
[MeterTextFileList]
Meter=String
MeasureName=MeasureLuaFileList
FontFace=Trebuchet MS
StringEffect=Border
StringAlign=LEFT
FontColor=255,255,255,255
FontSize=10
AntiAlias=1
X=1
Y=1
Code: Select all
$inputFolder = $args[0]
$recursive = $args[1]
$writeMode = $args[2]
$outputPath = $args[3]
if ($recursive -eq 'true'){
$recursive = $true
}
elseif ($recursive -eq 'false') {
$recursive = $false
}
#Hashtable with Get-ChildItem parameters
$parameters = @{
LiteralPath = $inputFolder
Recurse = $recursive
}
if ($writeMode -eq 'true'){
$writeMode = 'Append'
}
elseif ($writeMode -eq 'false') {
$writeMode = 'Create'
}
#Array with FileStream parameters
$fileStreamParas = $outputPath , $writeMode, 'Write', 'Read'
try
{
$fileStream = New-Object IO.FileStream $fileStreamParas
$Utf8BomEncoding = New-Object System.Text.UTF8Encoding $True
$streamWriter = New-Object System.IO.StreamWriter($fileStream, $Utf8BomEncoding)
Get-ChildItem @parameters | ForEach-Object { $streamWriter.WriteLine($_.FullName) }
}
finally
{
$streamWriter.close()
}
Code: Select all
$inputFolder = $args[0]
$outputPath = $args[1]
#Array with FileStream parameters
$fileStreamParas = $outputPath , 'Create', 'Write', 'Read'
try
{
$fileStream = New-Object IO.FileStream $fileStreamParas
$Utf8BomEncoding = New-Object System.Text.UTF8Encoding $True
$streamWriter = New-Object System.IO.StreamWriter($fileStream, $Utf8BomEncoding)
Get-ChildItem -LiteralPath $inputFolder | ForEach-Object { $streamWriter.WriteLine($_.FullName) }
}
finally
{
$streamWriter.close()
}
Code: Select all
function Initialize()
TempFilePath = SKIN:MakePathAbsolute('@Resources\\OutputFile.txt')
end
function Update()
local array = ReadFileLines(TempFilePath)
return FilesToString(array)
end
--function takes a path to a text file which contains
--full paths to directories and files. It writes all
--file paths line by line into an array.
function ReadFileLines(FilePath)
--Open File
local File = io.open(FilePath)
--Handle Error opening the file
if not File then
print('ReadFile: unable to open file at ' .. FilePath)
return
end
local Contents = {}
--Read File and write into array 'Contents'
for Line in File:lines() do
table.insert(Contents, Line)
end
--Close File
File:close()
--Delete file content
--File = io.open(FilePath, "w")
--File:write("")
--File:close()
return Contents
end
--function takes an array with file paths and returns
--a string which contains the filenames of the files
--inside the array divided with a comma
function FilesToString(array)
local returnString = ""
--for every file inside the array
for i, file in ipairs(array) do
--get file name from path
fileName = string.match(file, '[^\\/]+$')
--append to returnString
if(i == 1) then
returnString = fileName
elseif (i > 1) then
returnString = returnString..', '..fileName
end
end
return returnString
end