2017-12-16 5 views
1

を呼び出そうと私は、次のLuaのコードスニペットを持っている(つまり、カイロを使用しています)のluaエラー:しかしゼロ値(フィールド「getn」)

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」)

私はここで提案いくつかのことを試してみましたが、私はこの問題を解決するかどうかはわからないそう思っていました簡単な修正。

次のために上記ではなくのための美しくコメント作品に提案された解決策:

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') 
+0

'getn'はLua 5.1から廃止されました。' table.getn(processes) 'を'#processes'に置き換えてください。 –

+0

ありがとう!これは機能します。それがチェックマークでここにあると言っているかどうかは分かりません。 – user3236841

+0

関数に引数がtable.getn(arg)としてある場合はどうなりますか?これをどのように置き換えるのですか?ありがとう! – user3236841

答えて

0

は、固定されたコードスニペット、次のとおりです。

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,#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) 

そして、2つ目の質問のために固定コードスニペットは、次のとおりです。固定に入ったすべての提案のための

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/#arg) * #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, #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 
    return '' 
end 

感謝このコード。

関連する問題