简体   繁体   English

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

[英]How to change text color in Lua script?

I have two scripts for Conky calendar, downloaded from internet.我有两个用于 Conky 日历的脚本,是从互联网上下载的。 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).以前从未接触过 Lua 并且无法弄清楚如何为整个日历文本设置颜色(当前日期除外)。

I can understand that third line in cal.lua, conky_color = "${color1}%2d${color}" is variable for current date color.我可以理解 cal.lua, conky_color = "${color1}%2d${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),这种颜色后来在 conky.conf 中的八行定义: color1 = '86b5ea',据我了解,它在第 52 行的 cal.lua 中以(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.我无法弄清楚如何为日历文本的整个 rest 定义/设置颜色,请帮忙。

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.我的最终目标是在 Conky 小部件中将此日历放在白色背景上,所以如果事情是这样的话,除了当前日期之外,一切都将不可见,因为所有 rest 文本也是白色的。

This is how it looks like on black background:这是它在黑色背景上的样子:

在此处输入图像描述

Just to mention that usual color formatting inside conky.conf, ie 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:顺便提一下 conky.conf 中常用的颜色格式,即将${color xyz}放在${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font}在这里不起作用,因为在当前日期之前这只会改变颜色,并且当月的 rest 将保持白色,如下面的屏幕截图所示:

在此处输入图像描述

I assume that solution is not much complicated, but hadn't success to find it, please help.我认为解决方案并不复杂,但没有成功找到它,请帮忙。

Scripts:脚本:

cal.lua 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!非常感谢!

One general solution is to pass arguments to your lua script so that it doesn't need to assume anything about colours.一种通用的解决方案是将 arguments 传递给您的 lua 脚本,这样它就不需要假设任何颜色。 This is simple to do in the conky part by simply adding more words to the exec call, for example:这在 conky 部分很简单,只需在 exec 调用中添加更多单词即可,例如:

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

You can retrieve these in the lua arg array:您可以在 lua arg数组中检索这些:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM