How to change text color in Lua script?

10.4k views Asked by At

I have two scripts for Conky calendar, downloaded from internet. You can find it at the end of this post.

I am not developer, but usually I am able to do small edits on finished code and fit it to my needs.

Never touched Lua before and cannot figure out how to set color for whole calendar text (except for current date).

I can understand that third line in cal.lua, conky_color = "${color1}%2d${color}" is variable for current date color. This color is later defined in conky.conf on eight line: color1 = '86b5ea', and from my understanding, it is called in cal.lua on 52nd line with (conky_color):format(currentday),

What I cannot figure out is how to define/set color for the whole rest of the calendar text, please help.

My end goal is to put this calendar on white background in Conky widget, so if the things are as is, everything will be invisible except of current date, cause all rest text is also white.

This is how it looks like on black background:

enter image description here

Just to mention that usual color formatting inside conky.conf, i.e. put ${color xyz} in front of ${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font} will not work here, cause this will change only color until current date and rest of the month will stay white as on screenshot below:

enter image description here

I assume that solution is not much complicated, but hadn't success to find it, please help.

Scripts:

cal.lua

conky_color = "${color1}%2d${color}"

t = os.date('*t', os.time())
year, month, currentday = t.year, t.month, t.day

daystart = os.date("*t",os.time{year=year,month=month,day=01}).wday

month_name = os.date("%B")

days_in_month = {
    31, 28, 31, 30, 31, 30, 
    31, 31, 30, 31, 30, 31
}

-- check for leap year
-- Any year that is evenly divisible by 4 is a leap year
-- Any year that is evenly divisible by 100 is a leap year if
-- it is also evenly divisible by 400.
LeapYear = function (year)
    return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
end

if LeapYear(year) then
    days_in_month[2] = 29
end

title_start = (20 - (string.len(month_name) + 5)) / 2

title = string.rep(" ", math.floor(title_start+0.5)) .. -- add padding to center the title
        (" %s %s\n Su Mo Tu We Th Fr Sa\n"):format(month_name, year)

io.write(title)

function seq(a,b)
    if a > b then
        return
    else
        return a, seq(a+1,b)
    end 
end

days = days_in_month[month]

io.write(
    string.format(
        string.rep("   ", daystart-1) ..
        string.rep(" %2d", days), seq(1,days)
    ):gsub(string.rep(".",21),"%0\n")
     :gsub(("%2d"):format(currentday),
           (conky_color):format(currentday),
           1
     ) .. "\n"
)

conky.conf

-- vim: ts=4 sw=4 noet ai cindent syntax=lua conky.config = {
    alignment = 'top_right',
    background = false,
    border_width = 0,
    cpu_avg_samples = 2,
    default_color = 'cccccc',
    color1 = '86b5ea',
    default_outline_color = 'cccccc',
    default_shade_color = '7a999c',
    double_buffer = true,
    draw_borders = false,
    draw_graph_borders = false,
    draw_outline = false,
    draw_shades = false,
    use_xft = true,
    font = 'Fira Sans:normal:size=14',
    gap_x = 10,
    gap_y = 41,
    minimum_height = 5,
    minimum_width = 231,
    maximum_width = 231,
    net_avg_samples = 2,
    no_buffers = true,
    out_to_console = false,
    out_to_stderr = false,
    extra_newline = false,
    own_window = true,
    own_window_class = 'Conky',
    own_window_transparent = true,  own_window_argb_visual = true,  own_window_argb_value = 255,
    own_window_type = 'desktop',
    stippled_borders = 0,
    update_interval = 1.0,
    uppercase = false,
    use_spacer = 'none',
    show_graph_scale = false,
    show_graph_range = false, }

conky.text = [[
${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font}
]]

Thank you very much!

1

There are 1 answers

0
meuh On

One general solution is to pass arguments to your lua script so that it doesn't need to assume anything about colours. This is simple to do in the conky part by simply adding more words to the exec call, for example:

${execpi 3600 ~/.config/conky/cal.lua blue green}

You can retrieve these in the lua arg array:

colorA = string.format("${color %s}",arg[1])
colorB = string.format("${color %s}",arg[2])
conky_color = colorB .. "%2d" .. colorA

Also, set the colour before writing the title:

io.write(colorA..title)