Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Created October 6, 2017 10:20
Show Gist options
  • Save tomoe-mami/87dd0b471be1c230f3699b9a17287f34 to your computer and use it in GitHub Desktop.
Save tomoe-mami/87dd0b471be1c230f3699b9a17287f34 to your computer and use it in GitHub Desktop.
first attempt at making hdata less annoying
w, script_name = weechat, "hdata_test"
hdata = {
meta = {
__index = function (tb, name)
local var_type = tb._vars[name]
if not var_type then
return
end
if var_type == "hashtable" then
return w.hdata_hashtable(tb._hdata, tb._ptr, name)
elseif var_type:sub(1, 1) == "@" then
return hdata.new(var_type:sub(2), w.hdata_pointer(tb._hdata, tb._ptr, name))
else
local func = w["hdata_"..var_type]
local size = w.hdata_get_var_array_size(tb._hdata, tb._ptr, name)
if size < 1 then
return func(tb._hdata, tb._ptr, name)
end
local v = {}
for i = 0, size - 1 do
v[#v+1] = func(tb._hdata, tb._ptr, i.."|"..name)
end
return v
end
end
},
new = function (name, ptr)
if not name or not ptr or name == "" or ptr == "" then
return nil, "hdata name and pointer can not be empty"
end
local tb = {
_type = name,
_ptr = ptr,
_hdata = w.hdata_get(name),
_vars = {}
}
if tb._hdata == "" then
return nil, "unknown hdata: "..name
end
local var_keys = w.hdata_get_string(tb._hdata, "var_keys")
for var_name in var_keys:gmatch("([^,]+)") do
local var_type = w.hdata_get_var_type_string(tb._hdata, var_name)
if var_type == "shared_string" then
var_type = "string"
elseif var_type == "pointer" then
local var_hdata = w.hdata_get_var_hdata(tb._hdata, var_name)
if var_hdata ~= "" then
var_type = "@"..var_hdata
end
end
tb._vars[var_name] = var_type
end
return setmetatable(tb, hdata.meta)
end,
list = function (name, list_name)
return hdata.new(name, w.hdata_get_list(w.hdata_get(name), list_name))
end
}
function main()
w.register(script_name, "singalaut <https://github.com/tomoe-mami>", "0.1", "WTFPL", "hdata test", "", "")
local server = hdata.list("irc_server", "irc_servers")
while server do
w.print("", server.name)
if server.is_connected == 1 then
local channel = server.channels
while channel do
w.print("", " "..channel.name)
channel = channel.next_channel
end
end
server = server.next_server
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment