client: Include c.name in the result of tostring(c)

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2014-12-06 18:16:05 +01:00
parent 8dc6fa8666
commit 8eed5e7bcf
4 changed files with 40 additions and 6 deletions

View File

@ -244,6 +244,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
class->newindex_miss_property = newindex_miss_property; class->newindex_miss_property = newindex_miss_property;
class->checker = checker; class->checker = checker;
class->parent = parent; class->parent = parent;
class->tostring = NULL;
class->instances = 0; class->instances = 0;
signal_add(&class->signals, "new"); signal_add(&class->signals, "new");

View File

@ -72,6 +72,8 @@ struct lua_class_t
lua_class_checker_t checker; lua_class_checker_t checker;
/** Number of instances of this class in lua */ /** Number of instances of this class in lua */
unsigned int instances; unsigned int instances;
/** Class tostring method */
lua_class_propfunc_t tostring;
}; };
const char * luaA_typename(lua_State *, int); const char * luaA_typename(lua_State *, int);
@ -100,6 +102,11 @@ int luaA_class_new(lua_State *, lua_class_t *);
void * luaA_checkudata(lua_State *, int, lua_class_t *); void * luaA_checkudata(lua_State *, int, lua_class_t *);
void * luaA_toudata(lua_State *L, int ud, lua_class_t *); void * luaA_toudata(lua_State *L, int ud, lua_class_t *);
static inline void luaA_class_set_tostring(lua_class_t *class, lua_class_propfunc_t callback)
{
class->tostring = callback;
}
static inline void * static inline void *
luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class) luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class)
{ {

View File

@ -317,22 +317,34 @@ luaA_object_tostring(lua_State *L)
{ {
lua_class_t *lua_class = luaA_class_get(L, 1); lua_class_t *lua_class = luaA_class_get(L, 1);
lua_object_t *object = luaA_checkudata(L, 1, lua_class); lua_object_t *object = luaA_checkudata(L, 1, lua_class);
int offset = 0;
int i = 0; for(; lua_class; lua_class = lua_class->parent)
for(; lua_class; lua_class = lua_class->parent, i++)
{ {
if(i) if(offset)
{ {
lua_pushliteral(L, "/"); lua_pushliteral(L, "/");
lua_insert(L, - (i * 2)); lua_insert(L, -++offset);
} }
lua_pushstring(L, NONULL(lua_class->name)); lua_pushstring(L, NONULL(lua_class->name));
lua_insert(L, - (i * 2) - 1); lua_insert(L, -++offset);
if (lua_class->tostring) {
int k, n;
lua_pushliteral(L, "(");
n = 2 + lua_class->tostring(L, object);
lua_pushliteral(L, ")");
for (k = 0; k < n; k++)
lua_insert(L, -offset);
offset += n;
}
} }
lua_pushfstring(L, ": %p", object); lua_pushfstring(L, ": %p", object);
lua_concat(L, i * 2); lua_concat(L, offset + 1);
return 1; return 1;
} }

View File

@ -2156,6 +2156,19 @@ luaA_client_keys(lua_State *L)
return luaA_key_array_get(L, 1, keys); return luaA_key_array_get(L, 1, keys);
} }
static int
client_tostring(lua_State *L, client_t *c)
{
char *name = c->name ? c->name : c->alt_name;
ssize_t len = a_strlen(name);
ssize_t limit = 20;
lua_pushlstring(L, name, MIN(len, limit));
if (len > limit)
lua_pushstring(L, "...");
return len > limit ? 2 : 1;
}
/* Client module. /* Client module.
* \param L The Lua VM state. * \param L The Lua VM state.
* \return The number of pushed elements. * \return The number of pushed elements.
@ -2233,6 +2246,7 @@ client_class_setup(lua_State *L)
(lua_class_checker_t) client_checker, (lua_class_checker_t) client_checker,
luaA_class_index_miss_property, luaA_class_newindex_miss_property, luaA_class_index_miss_property, luaA_class_newindex_miss_property,
client_methods, client_meta); client_methods, client_meta);
luaA_class_set_tostring(&client_class, (lua_class_propfunc_t) client_tostring);
luaA_class_add_property(&client_class, "name", luaA_class_add_property(&client_class, "name",
NULL, NULL,
(lua_class_propfunc_t) luaA_client_get_name, (lua_class_propfunc_t) luaA_client_get_name,