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

script: add option "script.look.display_source" (display source code with detail of script, enabled by default)

This commit is contained in:
Sebastien Helleu
2012-08-28 16:11:51 +02:00
parent 5073048428
commit df7b14e41c
21 changed files with 460 additions and 41 deletions
+142 -2
View File
@@ -22,6 +22,7 @@
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <libgen.h>
@@ -413,6 +414,7 @@ script_action_installnext_timer_cb (void *data, int remaining_calls)
/*
* script_action_install_process_cb: callback called when script is downloaded
* (for installing it)
*/
int
@@ -446,7 +448,8 @@ script_action_install_process_cb (void *data, const char *command,
ptr_script = script_repo_search_by_name_ext (pos + 1);
if (ptr_script)
{
filename = script_config_get_script_download_filename (ptr_script);
filename = script_config_get_script_download_filename (ptr_script,
NULL);
if (filename)
{
length = 3 + strlen (filename) + 1;
@@ -506,7 +509,8 @@ script_action_install (int quiet)
if (ptr_script_to_install)
{
filename = script_config_get_script_download_filename (ptr_script_to_install);
filename = script_config_get_script_download_filename (ptr_script_to_install,
NULL);
if (filename)
{
options = weechat_hashtable_new (8,
@@ -656,6 +660,94 @@ script_action_hold (const char *name, int quiet)
return 0;
}
/*
* script_action_show_process_cb: callback called when script is downloaded
* (for showing source code below script detail)
*/
int
script_action_show_process_cb (void *data, const char *command,
int return_code, const char *out,
const char *err)
{
char *pos, *filename, line[4096], *ptr_line;
struct t_repo_script *ptr_script;
FILE *file;
int line_y;
/* make C compiler happy */
(void) data;
if (return_code >= 0)
{
pos = strrchr (command, '/');
if ((err && err[0]) || (out && (strncmp (out, "error:", 6) == 0)))
{
weechat_printf (NULL,
_("%s%s: error downloading script \"%s\": %s"),
weechat_prefix ("error"),
SCRIPT_PLUGIN_NAME,
(pos) ? pos + 1 : "?",
(err && err[0]) ? err : out + 6);
return WEECHAT_RC_OK;
}
if (pos)
{
ptr_script = script_repo_search_by_name_ext (pos + 1);
if (ptr_script)
{
filename = script_config_get_script_download_filename (ptr_script,
".tmp");
if (filename)
{
/*
* read file and display content on script buffer
* (only if script buffer is still displaying detail of
* this script)
*/
if (script_buffer && script_buffer_detail_script
&& (script_buffer_detail_script == ptr_script))
{
line_y = script_buffer_detail_script_line_source + 2;
file = fopen (filename, "r");
if (file)
{
while (!feof (file))
{
ptr_line = fgets (line, sizeof (line) - 1, file);
if (ptr_line)
{
weechat_printf_y (script_buffer, line_y,
"%s", ptr_line);
line_y++;
}
}
fclose (file);
}
else
{
weechat_printf_y (script_buffer, line_y,
_("Error: file not found"));
line_y++;
}
weechat_printf_y (script_buffer, line_y,
"%s----------------------------------------"
"----------------------------------------",
weechat_color ("green"));
line_y++;
}
unlink (filename);
free (filename);
}
}
}
}
return WEECHAT_RC_OK;
}
/*
* script_action_show: show detailed info on a script
*/
@@ -664,6 +756,9 @@ void
script_action_show (const char *name, int quiet)
{
struct t_repo_script *ptr_script;
char *filename, *url;
int length;
struct t_hashtable *options;
if (name)
{
@@ -671,6 +766,51 @@ script_action_show (const char *name, int quiet)
if (ptr_script)
{
script_buffer_show_detail_script (ptr_script);
if (weechat_config_boolean (script_config_look_display_source))
{
weechat_printf_y (script_buffer,
script_buffer_detail_script_line_source,
_("Source code:"));
weechat_printf_y (script_buffer,
script_buffer_detail_script_line_source + 1,
"%s----------------------------------------"
"----------------------------------------",
weechat_color ("green"));
weechat_printf_y (script_buffer,
script_buffer_detail_script_line_source + 2,
_("Downloading script..."));
weechat_printf_y (script_buffer,
script_buffer_detail_script_line_source + 3,
"%s----------------------------------------"
"----------------------------------------",
weechat_color ("green"));
filename = script_config_get_script_download_filename (ptr_script,
".tmp");
if (filename)
{
options = weechat_hashtable_new (8,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (options)
{
length = 4 + strlen (ptr_script->url) + 1;
url = malloc (length);
if (url)
{
snprintf (url, length, "url:%s", ptr_script->url);
weechat_hashtable_set (options, "file_out", filename);
weechat_hook_process_hashtable (url, options, 30000,
&script_action_show_process_cb,
NULL);
free (url);
}
weechat_hashtable_free (options);
}
free (filename);
}
}
}
else
{
+38 -23
View File
@@ -36,6 +36,7 @@
struct t_gui_buffer *script_buffer = NULL;
int script_buffer_selected_line = 0;
struct t_repo_script *script_buffer_detail_script = NULL;
int script_buffer_detail_script_line_source = 0;
/*
@@ -361,11 +362,12 @@ script_buffer_display_detail_script (struct t_repo_script *script)
{
struct tm *tm;
char str_time[1024];
char *labels[] = { N_("Script"), N_("Version"), N_("Author"),
N_("License"), N_("Description"), N_("Tags"),
N_("Status"), N_("Date added"), N_("Date updated"),
N_("URL"), N_("MD5"), N_("Requires"), N_("Min WeeChat"),
N_("Max WeeChat"), NULL };
char *labels[] = { N_("Script"), N_("Version"), N_("Version loaded"),
N_("Author"), N_("License"), N_("Description"),
N_("Tags"), N_("Status"), N_("Date added"),
N_("Date updated"), N_("URL"), N_("MD5"), N_("Requires"),
N_("Min WeeChat"), N_("Max WeeChat"),
NULL };
int i, length, max_length, line;
max_length = 0;
@@ -386,10 +388,16 @@ script_buffer_display_detail_script (struct t_repo_script *script)
weechat_color (weechat_config_string (script_config_color_text_extension)),
script_extension[script->language]);
line++;
weechat_printf_y (script_buffer, line + 1, "%s: %s",
weechat_printf_y (script_buffer, line + 1, "%s: %s%s",
script_buffer_detail_label (_(labels[line]), max_length),
weechat_color (weechat_config_string (script_config_color_text_version)),
script->version);
line++;
weechat_printf_y (script_buffer, line + 1, "%s: %s%s",
script_buffer_detail_label (_(labels[line]), max_length),
weechat_color (weechat_config_string (script_config_color_text_version_loaded)),
(script->version_loaded) ? script->version_loaded : "-");
line++;
weechat_printf_y (script_buffer, line + 1,
"%s: %s <%s>",
script_buffer_detail_label (_(labels[line]), max_length),
@@ -481,6 +489,9 @@ script_buffer_display_detail_script (struct t_repo_script *script)
"%s: %s",
script_buffer_detail_label (_(labels[line]), max_length),
(script->max_weechat) ? script->max_weechat : "-");
line++;
script_buffer_detail_script_line_source = line + 2;
}
/*
@@ -730,24 +741,27 @@ script_buffer_input_cb (void *data, struct t_gui_buffer *buffer,
return WEECHAT_RC_OK;
}
/* change sort keys on buffer */
if (strncmp (input_data, "s:", 2) == 0)
if (!script_buffer_detail_script)
{
if (input_data[2])
weechat_config_option_set (script_config_look_sort, input_data + 2, 1);
else
weechat_config_option_reset (script_config_look_sort, 1);
return WEECHAT_RC_OK;
}
/* change sort keys on buffer */
if (strncmp (input_data, "s:", 2) == 0)
{
if (input_data[2])
weechat_config_option_set (script_config_look_sort, input_data + 2, 1);
else
weechat_config_option_reset (script_config_look_sort, 1);
return WEECHAT_RC_OK;
}
/* refresh buffer */
if (strcmp (input_data, "$") == 0)
{
script_get_loaded_scripts ();
script_repo_remove_all ();
script_repo_file_read (1);
script_buffer_refresh (1);
return WEECHAT_RC_OK;
/* refresh buffer */
if (strcmp (input_data, "$") == 0)
{
script_get_loaded_scripts ();
script_repo_remove_all ();
script_repo_file_read (1);
script_buffer_refresh (1);
return WEECHAT_RC_OK;
}
}
/* execute action on a script */
@@ -763,7 +777,8 @@ script_buffer_input_cb (void *data, struct t_gui_buffer *buffer,
}
/* filter scripts with given text */
script_repo_filter_scripts (input_data);
if (!script_buffer_detail_script)
script_repo_filter_scripts (input_data);
return WEECHAT_RC_OK;
}
+1
View File
@@ -27,6 +27,7 @@ struct t_repo_script;
extern struct t_gui_buffer *script_buffer;
extern int script_buffer_selected_line;
extern struct t_repo_script *script_buffer_detail_script;
extern int script_buffer_detail_script_line_source;
extern void script_buffer_refresh (int clear);
extern void script_buffer_set_current_line (int line);
+22 -3
View File
@@ -40,6 +40,7 @@ struct t_config_section *script_config_section_scripts = NULL;
/* script config, look section */
struct t_config_option *script_config_look_columns;
struct t_config_option *script_config_look_display_source;
struct t_config_option *script_config_look_quiet_actions;
struct t_config_option *script_config_look_sort;
struct t_config_option *script_config_look_translate_description;
@@ -138,21 +139,31 @@ script_config_get_xml_filename ()
* script_config_get_script_download_filename: get filename for a script to
* download, for eample:
* "/home/xxx/.weechat/script/iset.pl"
* (if suffix is not NULL, it is
* added to filename)
* Note: result must be freed after
* use
*/
char *
script_config_get_script_download_filename (struct t_repo_script *script)
script_config_get_script_download_filename (struct t_repo_script *script,
const char *suffix)
{
char *path, *filename;
int length;
path = script_config_get_dir ();
length = strlen (path) + 1 + strlen (script->name_with_extension) + 1;
length = strlen (path) + 1 + strlen (script->name_with_extension)
+ ((suffix) ? strlen (suffix) : 0) + 1;
filename = malloc (length);
if (filename)
snprintf (filename, length, "%s/%s", path, script->name_with_extension);
{
snprintf (filename, length,
"%s/%s%s",
path,
script->name_with_extension,
(suffix) ? suffix : "");
}
free (path);
return filename;
}
@@ -359,6 +370,14 @@ script_config_init ()
"%W=max_weechat)"),
NULL, 0, 0, "%s %n %V %v %u | %d | %t", NULL, 0,
NULL, NULL, &script_config_refresh_cb, NULL, NULL, NULL);
script_config_look_display_source = weechat_config_new_option (
script_config_file, ptr_section,
"display_source", "boolean",
N_("display source code of script on buffer with detail on a script "
"(script is downloaded in a temporary file when detail on script "
"is displayed)"),
NULL, 0, 0, "on", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
script_config_look_quiet_actions = weechat_config_new_option (
script_config_file, ptr_section,
"quiet_actions", "boolean",
+3 -1
View File
@@ -25,6 +25,7 @@
struct t_repo_script;
extern struct t_config_option *script_config_look_columns;
extern struct t_config_option *script_config_look_display_source;
extern struct t_config_option *script_config_look_quiet_actions;
extern struct t_config_option *script_config_look_sort;
extern struct t_config_option *script_config_look_translate_description;
@@ -64,7 +65,8 @@ extern struct t_config_option *script_config_scripts_url;
extern char *script_config_get_dir ();
extern char *script_config_get_xml_filename ();
extern char *script_config_get_script_download_filename (struct t_repo_script *script);
extern char *script_config_get_script_download_filename (struct t_repo_script *script,
const char *suffix);
extern void script_config_hold (const char *name_with_extension);
extern void script_config_unhold (const char *name_with_extension);
extern int script_config_init ();