如何在Lua脚本中设置Conky日历非当前日期的文本颜色?
解决Conky Lua日历非当前日期颜色不可见问题
我来帮你搞定这个问题!你遇到的核心问题是:Lua脚本输出的日历中,只有当前日期被设置了颜色,其余文本继承Conky的默认颜色,在白色背景上无法显示。下面是具体的解决方案:
步骤1:在Conky配置中新增非当前日期颜色
首先打开你的conky.conf,在conky.config块里添加一个新的颜色变量color2,用来定义非当前日期、标题和星期几的颜色(这里用深灰色示例,你可以根据自己的白色背景调整成合适的颜色值):
conky.config = { -- ... 保留原有配置 ... color1 = '86b5ea', color2 = '666666', -- 新增:非当前日期/标题的颜色 -- ... 保留原有配置 ... }
步骤2:修改Lua日历脚本
接下来修改cal.lua脚本,给非当前日期、标题和星期几统一加上颜色标签,替换掉原来的日期生成逻辑:
conky_color = "${color1}%2d${color}" normal_color = "${color2}%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 -- 修改标题:给月份、年份、星期几加上color2颜色 title = "${color2}" .. string.rep(" ", math.floor(title_start+0.5)) .. (" %s %s\n Su Mo Tu We Th Fr Sa\n"):format(month_name, year) io.write(title) local date_str = "" -- 月初空白占位:保持日历对齐,每个占位对应3字符宽度 date_str = date_str .. string.rep(" ", (daystart - 1) * 3) for day = 1, days_in_month[month] do if day == currentday then -- 当前日期使用color1高亮 date_str = date_str .. string.format(" " .. conky_color, day) else -- 非当前日期使用color2 date_str = date_str .. string.format(" " .. normal_color, day) end -- 每7天换行,保持日历的星期结构 if (daystart - 1 + day) % 7 == 0 then date_str = date_str .. "\n" end end -- 输出日期并恢复默认颜色,避免影响Conky其他部件 io.write(date_str .. "${color}\n")
为什么原来的方法无效?
你之前尝试在${execpi}前加${color}没用,是因为Lua脚本里的当前日期用了${color}恢复了Conky的默认颜色,导致后续的日期文本又变回了白色。现在的方案给每个非当前日期都单独加上了color2的标签,标题也统一设置了颜色,确保所有需要显示的文本都有明确的颜色定义,不会被默认颜色覆盖。
重启Conky后,你就能看到白色背景上,非当前日期是你设置的color2颜色,当前日期保留原来的color1高亮啦!
内容的提问来源于stack exchange,提问作者stiw47
相关产品推荐
相关产品推荐

