It is currently April 25th, 2024, 11:41 am

Ternary operator with lua to use strings

Tips and Tricks from the Rainmeter Community
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Ternary operator with lua to use strings

Post by death.crafter »

Yesterday while helping a fellow mate with his skin, it occurred to me if we could work with strings in ternary operator, it would have been a lot easier to do things. So I just made one with Lua. Here is the lua code and example skin with usage instructions:

Lua:

Code: Select all

function Ternary(value, comparisonType, equalValue, trueValue, falseValue)

    --Sees if value, comparisonType or equalValue is to be parsed or used as is.
    if string.match(value, '^%{(.*)}$') then value=FormulaParser(value) value=tostring(value) end

    if string.match(equalValue, '^%{(.*)%}$') then equalValue=FormulaParser(equalValue) equalValue=tostring(equalValue) end

    --If user wants to use { } at the ends without wanting to evaluate the formula.
    if string.match(value, '^\\%{.*\\%}$') then value=string.gsub(value, '^\\%{(.*)\\%}$', '{%1}') end

    if string.match(equalValue, '^\\%{.*\\%}$') then equalValue=string.gsub(equalValue, '^\\%{(.*)\\%}$', '{%1}') end
        
    --converts strings to numbers to apply relational operators
    local x=comparisonType
    if x~='EqualsTo' and x~='=' and x~='NotEquals' and x~='!=' then value=tonumber(value) equalValue=tonumber(equalValue) end

    --main function
    if comparisonType == 'EqualsTo' or comparisonType == '=' then
        if value == equalValue then
            return trueValue
        else
            return falseValue
        end
    elseif comparisonType == 'GreaterThan' or comparisonType == '>' then
        if value > equalValue then
            return trueValue
        else
            return falseValue
        end
    elseif comparisonType == 'GreaterThanEquals' or comparisonType == '>=' then
        if value >= equalValue then
            return trueValue
        else
            return falseValue
        end
    elseif comparisonType == 'LessThan' or comparisonType == '<' then
        if value < equalValue then
            return trueValue
        else
            return falseValue
        end
    elseif comparisonType == 'LessThanEquals' or comparisonType == '<=' then
        if value <= equalValue then
            return trueValue
        else
            return falseValue
        end
    elseif comparisonType == 'NotEquals' or comparisonType == '!=' then
        if value ~= equalValue then
            return trueValue
        else
            return falseValue
        end
    end
end

function MatchTernary(string, matchType, regexVar, trueValue, falseValue)

    --gets the regex pattern variable
    regexPattern=SKIN:GetVariable(regexVar)

    --main function
    if matchType == 'match' then
        if string.match(string, regexPattern) then
            return trueValue
        else
            return falseValue
        end
    elseif matchType == 'notmatch' then
        if not string.match(string, regexPattern) then
            return trueValue
        else
            return falseValue
        end
    elseif matchType == 'contains' then
        if string.find(string, regexPattern) then
            return trueValue
        else
            return falseValue
        end
    elseif matchType == 'notcontains' then
        if not string.find(string, regexPattern) then
            return trueValue
        else
            return falseValue
        end
    end
end

function FormulaParser(arg)
    arg=string.gsub(arg, '^%{(.*)%}$', '(%1)')
    return SKIN:ParseFormula(arg)
end
Skin:

Code: Select all

[Rainmeter]
Update=1000
DefaultUpdateDivider=-1
AccurateText=1

[Variables]
Num=5
RegexPattern=Green%+red

;Colors
RightColor=0,255,0
WrongColor=255,0,0

[Terlua]
Measure=Script
ScriptFile=Ternary.lua
Disabled=1

[MeterString]
Meter=String
Text=Color is [&TerLua:Ternary('\{[#Num]+1\}', '=', '\{5+1\}', '[#RightColor]', '[#WrongColor]')]
FontSize=20
FontColor=[&TerLua:Ternary('{[#Num]+1}', 'EqualsTo', '6', '[#RightColor]', '[#WrongColor]')]
SolidColor=0,0,0,1
DynamicVariables=1
AntiAlias=1

[MeterShape]
Meter=Shape
Shape=Rectangle 1,30,20,20,10 | Fill Color [&TerLua:MatchTernary('Green+red', 'match', 'RegexPattern', '[#RightColor]', '[#WrongColor]')] | StrokeWidth 2
DynamicVariables=1
Usage:

Ternary:

Code: Select all


[&TerLua:Ternary('value', 'comparisonType', 'equalValue', 'trueValue', 'falseValue')]

'value' and 'equalValue'::

	-- If you want to use a formula or it requires evaluation, Use it inside {}. E.g. {5+3} or {[#Num]+5}
	
	-- If you want to use curly braces({}) without evaluating the term inside it, escape the braces 
	using '\'. E.g. \{5+2\} will be recognized as {5+2} and not 7.
	

comparisonTypes::

	'EqualsTo' or '='
	
	'GreaterThan' or '>'
	
	'GreaterThanEquals' or '>='
	
	'LessThan' or '<'
	
	'LessThanEquals' or '<='
	
	'NotEquals' or '!='
	
'trueValue' and 'falseValue'::

	-- Any string or integer. trueValue is returned if condition is true and vice-versa.

MatchTernary:

Code: Select all


[&TerLua:MatchTernary('string', 'matchType', 'regexVariable', 'trueValue', 'falseValue')]

'string'::

	-- Any string
	
'matchType'::

	'match' - True if the string matches with given Regex Pattern**
	
	'notmatch' - True if the string does not match with the given Regex Pattern**
	
	'contains' - True if the string contains the Regex Pattern**
	
	'notcontains' - True if the string does not contain the Regex Pattern**
	
'regexVariable'::

	-- A variable must be declared for using a Regex Pattern (because of some drawbacks). Use that variable as regexVariable argument.
	
'trueValue' and 'falseValue'::

	-- Same as Ternary.
	
**Regex Pattern: the Pattern must be in Lua's pattern matching format. Otherwise it wouldn't work. Reference.

Example skin:
LuaTernary_1.00.rmskin
You do not have the required permissions to view the files attached to this post.
from the Realm of Death