【问题标题】:Cheking day of the month in lua在 lua 中检查每月的日期
【发布时间】:2021-07-05 09:27:42
【问题描述】:

所以我一直在 Linux 上配置我的 AwesomeWM 主题,我遇到了这个问题。现在我当然不是专业的程序员,所以我想我会来这里寻求帮助。我一直在尝试检查一个月中的日期是否是从 1 到 9 的数字,这将更改日历上焦点数字的填充,但它似乎不起作用..

if (os.time(%d)>= 1) and (os.time(%d) <= 9) then
    theme.calendar_focus_padding = dpi(5)
else
    theme.calendar_focus_padding = dpi(10)
end

我在一个完全不同的文件(我的 rc.lua)上遇到错误,我真的不明白为什么。如果有人明白,这是屏幕截图。

我能理解的是,以下代码行存在问题(来自我的 rc.lua 文件):

client.connect_signal("focus", function(c) c.border_color = beautiful.border_focus end)
client.connect_signal("unfocus", function(c) c.border_color = beautiful.border_normal end)

我知道这是非常具体地询问堆栈溢出问题,但如果有人能提供帮助,我将不胜感激。

【问题讨论】:

  • os.time(%d) 不是合法的 Lua。

标签: linux lua archlinux awesome-wm


【解决方案1】:

好吧,这个版本似乎可以工作:

if (os.date("%d") >= "1") and (os.date("%d") <= "9") then
    theme.calendar_focus_padding = dpi(5)
else
    theme.calendar_focus_padding = dpi(10)
end

但是代码并没有按照应有的方式运行。我的意思是,尽管这是 7 月 2 日(这应该将 if 语句切换为 true 并在 if 和 else 语句之前执行所有内容,对吧?)。相反,它执行 else 语句 (theme.calendar_focus_padding = dpi(10)) 下的所有内容。现在怎么样了?

编辑: 所以我发现我必须将 os.date(etc) 转换为 int。我使用 tonumber() 函数这样做了,现在我有了这个,这似乎可以工作:

day = tonumber(os.date("%d"))

if (day >= 1) and (day <= 9) then
    theme.calendar_focus_padding = dpi(20)
else
    theme.calendar_focus_padding = dpi(10)
end

【讨论】:

    【解决方案2】:

    正如@lhf 提到的,您的代码不是有效的lua 代码。 Lua 参数可以是字符串、表格、数字或空值——模式没有特殊处理(因此通常包含在字符串中)

    这段代码应该可以工作:

    if (os.date("%d")>= 1) and (os.date("%d") <= 9) then
        theme.calendar_focus_padding = dpi(5)
    else
        theme.calendar_focus_padding = dpi(10)
    end
    
    

    将模式包含在字符串中允许 lua 词法分析器对其进行解析,这意味着代码至少会执行。然后os.time 应该在函数调用中解析%d 以提供相关值,在本例中为月份中的哪一天。

    【讨论】:

    • os.time("%d") 也不好:bad argument #1 to 'time' (table expected, got string)。现在os.date("%d") 工作。
    猜你喜欢
    • 2021-07-15
    • 2020-10-06
    • 2016-06-29
    • 2016-09-02
    • 2018-09-01
    • 2017-03-23
    • 2021-02-22
    • 2013-05-15
    • 1970-01-01
    相关资源
    最近更新 更多