1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-28 13:56:37 +02:00

relay/api: add buffer keys in GET /api/buffers (issue #2066)

This commit is contained in:
Sébastien Helleu
2024-05-01 13:51:32 +02:00
parent c5268bc5f9
commit afe037c579
6 changed files with 81 additions and 4 deletions
+55
View File
@@ -405,6 +405,9 @@ relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
cJSON_AddItemToObject (json, "local_variables", json_local_vars);
}
/* keys local to buffer */
cJSON_AddItemToObject (json, "keys", relay_api_msg_keys_to_json (buffer));
/* lines */
if (lines != 0)
{
@@ -426,6 +429,58 @@ relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
return json;
}
/*
* Creates a JSON object with a buffer key.
*/
cJSON *
relay_api_msg_key_to_json (struct t_gui_key *key)
{
struct t_hdata *hdata;
struct t_gui_key *pointer;
cJSON *json;
const char *ptr_string;
hdata = relay_hdata_key;
pointer = key;
json = cJSON_CreateObject ();
if (!json)
return NULL;
if (!key)
return json;
MSG_ADD_HDATA_STR("key", "key");
MSG_ADD_HDATA_STR("command", "command");
return json;
}
/*
* Creates a JSON object with an array of buffer keys.
*/
cJSON *
relay_api_msg_keys_to_json (struct t_gui_buffer *buffer)
{
cJSON *json;
struct t_gui_key *ptr_key;
json = cJSON_CreateArray ();
if (!json)
return NULL;
ptr_key = weechat_hdata_pointer (relay_hdata_buffer, buffer, "keys");
while (ptr_key)
{
cJSON_AddItemToArray (json, relay_api_msg_key_to_json (ptr_key));
ptr_key = weechat_hdata_move (relay_hdata_key, ptr_key, 1);
}
return json;
}
/*
* Creates a JSON object with a buffer line data.
*/
+5 -3
View File
@@ -41,11 +41,13 @@ extern cJSON *relay_api_msg_buffer_to_json (struct t_gui_buffer *buffer,
long lines,
int nicks,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_lines_to_json (struct t_gui_buffer *pointer,
long lines,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_key_to_json (struct t_gui_key *key);
extern cJSON *relay_api_msg_keys_to_json (struct t_gui_buffer *buffer);
extern cJSON *relay_api_msg_line_data_to_json (struct t_gui_line_data *line_data,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_lines_to_json (struct t_gui_buffer *buffer,
long lines,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_nick_to_json (struct t_gui_nick *nick,
enum t_relay_api_colors colors);
extern cJSON *relay_api_msg_nick_group_to_json (struct t_gui_nick_group *nick_group,
+2
View File
@@ -62,6 +62,7 @@ char *relay_msg_type_string[] = /* prefix in raw buffer for msg */
{ "", "[PING]\n", "[PONG]\n", "[CLOSE]\n" };
struct t_hdata *relay_hdata_buffer = NULL;
struct t_hdata *relay_hdata_key = NULL;
struct t_hdata *relay_hdata_lines = NULL;
struct t_hdata *relay_hdata_line = NULL;
struct t_hdata *relay_hdata_line_data = NULL;
@@ -251,6 +252,7 @@ weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[])
weechat_plugin = plugin;
relay_hdata_buffer = weechat_hdata_get ("buffer");
relay_hdata_key = weechat_hdata_get ("key");
relay_hdata_lines = weechat_hdata_get ("lines");
relay_hdata_line = weechat_hdata_get ("line");
relay_hdata_line_data = weechat_hdata_get ("line_data");
+1
View File
@@ -27,6 +27,7 @@
extern struct t_weechat_plugin *weechat_relay_plugin;
extern struct t_hdata *relay_hdata_buffer;
extern struct t_hdata *relay_hdata_key;
extern struct t_hdata *relay_hdata_lines;
extern struct t_hdata *relay_hdata_line;
extern struct t_hdata *relay_hdata_line_data;
+1
View File
@@ -49,6 +49,7 @@ struct t_gui_bar_window;
struct t_gui_buffer;
struct t_gui_completion;
struct t_gui_hotlist;
struct t_gui_key;
struct t_gui_line;
struct t_gui_line_data;
struct t_gui_lines;
@@ -118,7 +118,7 @@ TEST(RelayApiMsg, SendEvent)
TEST(RelayApiMsg, BufferToJson)
{
cJSON *json, *json_obj, *json_local_vars, *json_lines;
cJSON *json, *json_obj, *json_local_vars, *json_keys, *json_key, *json_lines;
cJSON *json_nicklist_root, *json_nicks, *json_groups, *json_group;
cJSON *json_group_nicks, *json_nick;
struct t_gui_buffer *buffer;
@@ -132,6 +132,9 @@ TEST(RelayApiMsg, BufferToJson)
POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "name"));
cJSON_Delete (json);
gui_buffer_set (gui_buffers, "key_bind_meta-y,1", "/test1");
gui_buffer_set (gui_buffers, "key_bind_meta-y,2", "/test2 arg");
/* buffer without lines and nicks */
json = relay_api_msg_buffer_to_json (gui_buffers, 0, 0, RELAY_API_COLORS_ANSI);
CHECK(json);
@@ -148,6 +151,17 @@ TEST(RelayApiMsg, BufferToJson)
json_local_vars = cJSON_GetObjectItem (json, "local_variables");
CHECK(json_local_vars);
CHECK(cJSON_IsObject (json_local_vars));
json_keys = cJSON_GetObjectItem (json, "keys");
CHECK(json_keys);
LONGS_EQUAL(2, cJSON_GetArraySize (json_keys));
json_key = cJSON_GetArrayItem (json_keys, 0);
CHECK(json_key);
WEE_CHECK_OBJ_STR("meta-y,1", json_key, "key");
WEE_CHECK_OBJ_STR("/test1", json_key, "command");
json_key = cJSON_GetArrayItem (json_keys, 1);
CHECK(json_key);
WEE_CHECK_OBJ_STR("meta-y,2", json_key, "key");
WEE_CHECK_OBJ_STR("/test2 arg", json_key, "command");
WEE_CHECK_OBJ_STR("core", json_local_vars, "plugin");
WEE_CHECK_OBJ_STR("weechat", json_local_vars, "name");
POINTERS_EQUAL(NULL, cJSON_GetObjectItem (json, "lines"));
@@ -298,6 +312,8 @@ TEST(RelayApiMsg, BufferToJson)
WEE_CHECK_OBJ_BOOL(0, json_nick, "visible");
cJSON_Delete (json);
gui_buffer_set (gui_buffers, "key_unbind_meta-y", "");
gui_buffer_close (buffer);
}