如何更改 Lua 脚本中的文本颜色?

问题描述

我有两个 Conky 日历脚本,从互联网上下载。你可以在这文章的末尾找到它。

我不是开发人员,但通常我能够对完成的代码进行小的编辑并使其适合我的需要。

之前没接触过 Lua,不知道如何为整个日历文本设置颜色(当前日期除外)。

我可以理解 cal.lua 中的第三行,conky_color = "${color1}%2d${color}" 是当前日期颜色的变量。这种颜色后来在 conky.conf 中的第 8 行定义:color1 = '86b5ea', 并且据我所知,它在第 52 行的 cal.lua 中使用 (conky_color):format(currentday),

调用

我无法弄清楚如何为整个日历文本的其余部分定义/设置颜色,请帮忙。

我的最终目标是在 Conky 小部件中将此日历放在白色背景上,因此如果事物保持原样,除了当前日期外,所有内容都将不可见,因为所有其余文本也是白色的。

这是黑色背景下的样子:

enter image description here

只是提到 conky.conf 中通常的颜色格式,即在 ${color xyz} 前面放 ${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font} 在这里不起作用,因为这只会改变颜色直到当前日期和本月剩余时间保持白色,如下面的屏幕截图所示:

enter image description here

我认为解决方案并不复杂,但没有成功找到它,请帮助。

脚本:

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
}

-- 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}
]]

非常感谢!

解决方法

一个通用的解决方案是将参数传递给你的 lua 脚本,这样它就不需要假设任何关于颜色的东西。这在 conky 部分很简单,只需在 exec 调用中添加更多单词即可,例如:

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

您可以在 lua arg 数组中检索这些:

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

另外,在写标题之前设置颜色:

io.write(colorA..title)