【发布时间】:2018-05-30 12:31:56
【问题描述】:
我有以下 lua 代码 sn-p(使用 cairo)
cairo_set_source_rgba(cr, COLOR_FONT_R, COLOR_FONT_G, COLOR_FONT_B, 1)
cairo_set_font_size(cr, 0.011 * WINDOW_HEIGHT)
local ps_str = conky_parse("${exec ps -Ao comm,pcpu,%mem --sort=-pcpu | head -n 15}")
local processes = {}
for line in string.gmatch(ps_str, '([^\n]+)') do
table.insert(processes, line)
end
for line = 1,table.getn(processes) do
cairo_move_to(cr, 0.213 * WINDOW_WIDTH, 0.443 * WINDOW_HEIGHT + line * 0.014 * WINDOW_HEIGHT)
cairo_show_text(cr, processes[line])
end
cairo_stroke(cr)
但是,当我通过 conky 运行它时,我收到以下错误(这是在行中,距离末尾 5 行)。
我收到错误:尝试调用 nil 值(字段“getn”)
我已经尝试了这里建议的一些方法,但我不确定如何解决这个问题,所以想知道是否有一个简单的解决方法。
cmets 中建议的解决方案适用于上述情况,但不适用于以下情况:
function conky_geo_dotspiral(cx_str, cy_str, ...)
local cx = conky_to_num(cx_str)
local cy = conky_to_num(cy_str)
local arms = math.ceil(24 / table.getn(arg)) * table.getn(arg)
local rows = 10
local radius0, radius1 = 50, 140
local dotradius = 4
for i,v_str in ipairs(arg) do
v = conky_to_num(conky_parse(v_str))
for j = i-1, arms - 1, table.getn(arg) do
local p = j / arms
for k = 0, v / rows do
local dx = cx + (radius0 + (radius1-radius0) * k/rows) * math.cos(p * 2*math.pi + k * math.pi/arms)
local dy = cy + (radius0 + (radius1-radius0) * k/rows) * math.sin(p * 2*math.pi + k * math.pi/arms)
cairo_arc (cr, dx, dy, dotradius, 0, 2 * math.pi)
cairo_fill(cr)
end
end
end
end
我得到错误:
尝试调用一个 nil 值(字段 'getn')
我尝试将 table.getn(arg) 替换为 #arg 但仍然出现错误。
conky: llua_do_call: function conky_geo_dotspiral execution failed: conky_geometry.lua:155: attempt to get length of a nil value (global 'arg')
【问题讨论】:
-
getn自 Lua 5.1 起已弃用,将table.getn(processes)替换为#processes -
谢谢!这行得通。不知道如何通过复选标记在这里表示它。
-
如果我在函数中有一个参数作为 table.getn(arg) 怎么办?我该如何替换这个?谢谢!
-
#arg不工作吗? -
试过了。才不是。我将在编辑中举一个例子,我的问题可能会更清楚。
标签: lua