我有两个用于 Conky 日历的脚本,是从互联网上下载的。你可以在这篇文章的末尾找到它。
我不是开发人员,但通常我能够对完成的代码进行小幅编辑并使其适合我的需要。
以前从未接触过 Lua,无法弄清楚如何为整个日历文本设置颜色(当前日期除外)。
我可以理解 cal.lua 中的第三行conky_color = "${color1}%2d${color}"
是当前日期颜色的变量。这种颜色后来在第八行的 conky.conf 中定义:color1 = '86b5ea',
据我了解,它在第 52 行的 cal.lua 中被调用(conky_color):format(currentday),
我无法弄清楚如何为日历文本的其余部分定义/设置颜色,请帮忙。
我的最终目标是把这个日历放在 Conky 小部件的白色背景上,所以如果事情是这样的话,除了当前日期之外,一切都将是不可见的,因为所有其余文本也是白色的。
这是它在黑色背景上的样子:
顺便提一下 conky.conf 中通常的颜色格式,即放在${color xyz}
前面的${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font}
在这里不起作用,因为在当前日期之前这只会改变颜色,而当月的其余时间将保持白色,如下面的屏幕截图所示:
我认为解决方案并不复杂,但没有成功找到它,请帮忙。
脚本:
调用.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"
)
配置文件
-- 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}
]]
非常感谢!