lua代码的奇怪行为(AwesomeWM config)

lua代码的奇怪行为(AwesomeWM config),lua,awesome-wm,Lua,Awesome Wm,通过我的rc.lua(AwesomeWM的配置文件)中的这段代码,我得到了您在下图中看到的内容: mybattmon = wibox.widget.textbox() function battery_status () local output={} local fd=io.popen("acpi", "r") local line=fd:read() while line do local battery_load = string.match(line, "(%d*

通过我的rc.lua(AwesomeWM的配置文件)中的这段代码,我得到了您在下图中看到的内容:

mybattmon = wibox.widget.textbox()
function battery_status ()
  local output={}
  local fd=io.popen("acpi", "r")
  local line=fd:read()
  while line do
    local battery_load = string.match(line, "(%d*)%%")
    local discharging
    if string.match(line, "Discharging")=="Discharging"
    then
      discharging="-"
    elseif string.match(line, "Charging")=="Charging"
      then
      discharging="⚡"
    else 
      discharging=""
    end
--    if tonumber(battery_load) < 10 then fontColor="red" else fontColor="black" end
--    table.insert(output,"<span color='" ..fontColor.. "'>")
    table.insert(output,discharging.. "" ..battery_load.. "%")
--    table.insert(output,"</span>")
    line=fd:read() --read next line
  end
  return table.concat(output,"|")
end
my_battmon_timer = timer({ timeout = 2 })
my_battmon_timer:connect_signal("timeout", function() 
  mybattmon:set_markup( '<span background="#92B0A0" font="' .. font .. '"color="#000">BAT: ' .. battery_status() .. '</span>' )
end)
my_battmon_timer:start()
mybattmon=wibox.widget.textbox()
功能电池\u状态()
本地输出={}
本地fd=io.popen(“acpi”、“r”)
本地行=fd:read()
当你排队时
本地电池负载=字符串。匹配(行,(%d*)%%”)
局部放电
if string.match(第行,“卸料”)=“卸料”
然后
放电=“-”
elseif string.match(第行,“充电”)=“充电”
然后
卸货=”⚡"
其他的
放电=“”
结束
--如果tonumber(电池负载)<10,则fontColor=“红色”否则fontColor=“黑色”结束
--表.插入(输出,“”)
表.插入(输出、放电“…电池负载“%”)
--表.插入(输出,“”)
line=fd:read()--读取下一行
结束
返回表.concat(输出“|”)
结束
my_battmon_timer=timer({timeout=2})
我的电池定时器:连接信号(“超时”,函数()
mybattmon:set_标记('BAT:'…电池状态()…')
(完)
我的计时器:开始()

如果我取消注释三条注释行(当电池电量降至10%以下时,这三条注释行将改变颜色),我将得到以下结果:

当我放置第二块电池时,竖条是用来分开的


有人知道为什么改变颜色的三行会在文本前后插入条形图吗?

当表格有多个元素时,您正在插入管道

您的原始代码实际上是这样的(对于单电池箱):

您未注释的代码版本实际上是这样的:

local output={}
table.insert(output, "pre-a")
table.insert(output, "a")
table.insert(output, "post-a")
print(table.concat(output, "|"))
# pre-a|a|post-a
您希望将跨度字符串格式化为表中的一个条目

if tonumber(battery_load) < 10 then fontColor="red" else fontColor="black" end
local batstr = "<span color='" ..fontColor.. "'>"
batstr = batstr..discharging.. "" ..battery_load.. "%"
batstr = batstr.."</span>"
table.insert(output,batstr)
如果tonumber(电池负载)<10,则fontColor=“red”else fontColor=“black”结束
本地batstr=“”
BATSR=BATSR..放电..“..电池负载..”
batstr=batstr.“”
表.插入(输出,BATSR)

table.concat
将concat字符串粘贴在表的每个元素之间。您的表通常每个电池包含一个条目。未注释的代码在每个电池的表中粘贴两个额外条目。开始和结束跨距标记。因此
table.concat
将管道粘贴在这些元素之间。用visi替换跨距可打印/可打印字符,您会更清楚地看到这个问题。非常感谢!我不知道这是table的功能。concat,我认为它与
table相同。insert
但没有放置位置(即结尾)。感谢您的解释。它起作用了。
table.insert
不需要位置(您在任何调用中都不会给出一个)。并且
table.insert
不会返回任何内容,而
table.concat
显然会返回任何内容。
if tonumber(battery_load) < 10 then fontColor="red" else fontColor="black" end
local batstr = "<span color='" ..fontColor.. "'>"
batstr = batstr..discharging.. "" ..battery_load.. "%"
batstr = batstr.."</span>"
table.insert(output,batstr)