1
0
mirror of https://github.com/irssi/irssi.git synced 2024-12-04 14:46:39 -05:00

/HELP, /NAMES: using now the column helper function in misc.c. /NAMES

moved to core and is printed immediately instead of sending /NAMES
request to server.


git-svn-id: http://svn.irssi.org/repos/irssi/trunk@1162 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Timo Sirainen 2001-01-28 15:46:00 +00:00 committed by cras
parent 4339c9a7ed
commit 3927ef9713
9 changed files with 246 additions and 142 deletions

View File

@ -289,6 +289,156 @@ static void cmd_channel_remove(const char *data)
cmd_params_free(free_arg);
}
static int get_nick_length(void *data)
{
return strlen(((NICK_REC *) data)->nick);
}
static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist)
{
WINDOW_REC *window;
TEXT_DEST_REC dest;
GString *str;
GSList *tmp;
char *format, *stripped;
char *linebuf, nickmode[2] = { 0, 0 };
int *columns, cols, rows, last_col_rows, col, row, max_width;
int item_extra;
window = window_find_closest(channel->server, channel->name,
MSGLEVEL_CLIENTCRAP);
max_width = window->width;
/* get the length of item extra stuff ("[ ] ") */
format = format_get_text(MODULE_NAME, NULL,
channel->server, channel->name,
TXT_NAMES_NICK, " ", "");
stripped = strip_codes(format);
item_extra = strlen(stripped);
g_free(stripped);
g_free(format);
/* remove width of timestamp from max_width */
format_create_dest(&dest, channel->server, channel->name,
MSGLEVEL_CLIENTCRAP, NULL);
format = format_get_line_start(current_theme, &dest, time(NULL));
if (format != NULL) {
stripped = strip_codes(format);
max_width -= strlen(stripped);
g_free(stripped);
g_free(format);
}
/* calculate columns */
cols = get_max_column_count(nicklist, get_nick_length, max_width,
item_extra, 3, &columns, &rows);
nicklist = columns_sort_list(nicklist, rows);
/* rows in last column */
last_col_rows = rows-(cols*rows-g_slist_length(nicklist));
if (last_col_rows == 0)
last_col_rows = rows;
str = g_string_new(NULL);
linebuf = g_malloc(max_width+1);
col = 0; row = 0;
for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
NICK_REC *rec = tmp->data;
nickmode[0] = rec->op ? '@' : rec->voice ? '+' : ' ';
memset(linebuf, ' ', columns[col]-item_extra);
linebuf[columns[col]-item_extra] = '\0';
memcpy(linebuf, rec->nick, strlen(rec->nick));
format = format_get_text(MODULE_NAME, NULL,
channel->server, channel->name,
TXT_NAMES_NICK, nickmode, linebuf);
g_string_append(str, format);
g_free(format);
if (++col == cols) {
printtext(channel->server, channel->name,
MSGLEVEL_CLIENTCRAP, "%s", str->str);
g_string_truncate(str, 0);
col = 0; row++;
if (row == last_col_rows)
cols--;
}
}
if (str->len != 0) {
printtext(channel->server, channel->name,
MSGLEVEL_CLIENTCRAP, "%s", str->str);
}
g_slist_free(nicklist);
g_string_free(str, TRUE);
g_free(columns);
g_free(linebuf);
}
void fe_channels_nicklist(CHANNEL_REC *channel)
{
NICK_REC *nick;
GSList *tmp, *nicklist, *sorted;
int nicks, normal, voices, ops;
nicks = normal = voices = ops = 0;
nicklist = nicklist_getnicks(channel);
sorted = NULL;
/* sort the nicklist */
for (tmp = nicklist; tmp != NULL; tmp = tmp->next) {
nick = tmp->data;
sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc) nicklist_compare);
if (nick->op)
ops++;
else if (nick->voice)
voices++;
else
normal++;
nicks++;
}
g_slist_free(nicklist);
/* display the nicks */
printformat(channel->server, channel->name,
MSGLEVEL_CRAP, TXT_NAMES, channel->name, "");
display_sorted_nicks(channel, sorted);
g_slist_free(sorted);
printformat(channel->server, channel->name,
MSGLEVEL_CRAP, TXT_ENDOFNAMES,
channel->name, nicks, ops, voices, normal);
}
static void cmd_names(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
{
CHANNEL_REC *channel;
g_return_if_fail(data != NULL);
if (server == NULL || !server->connected)
cmd_return_error(CMDERR_NOT_CONNECTED);
if (strcmp(data, "*") == 0) {
if (!IS_CHANNEL(item))
cmd_return_error(CMDERR_NOT_JOINED);
data = item->name;
}
channel = channel_find(server, data);
if (channel != NULL) {
fe_channels_nicklist(channel);
signal_stop();
}
}
void fe_channels_init(void)
{
settings_add_bool("lookandfeel", "autoclose_windows", TRUE);
@ -306,6 +456,7 @@ void fe_channels_init(void)
command_bind("channel add", NULL, (SIGNAL_FUNC) cmd_channel_add);
command_bind("channel remove", NULL, (SIGNAL_FUNC) cmd_channel_remove);
command_bind("channel list", NULL, (SIGNAL_FUNC) cmd_channel_list);
command_bind("names", NULL, (SIGNAL_FUNC) cmd_names);
command_set_options("channel add", "auto noauto -bots -botcmd");
command_set_options("join", "window");
@ -326,4 +477,5 @@ void fe_channels_deinit(void)
command_unbind("channel add", (SIGNAL_FUNC) cmd_channel_add);
command_unbind("channel remove", (SIGNAL_FUNC) cmd_channel_remove);
command_unbind("channel list", (SIGNAL_FUNC) cmd_channel_list);
command_unbind("names", (SIGNAL_FUNC) cmd_names);
}

View File

@ -0,0 +1,9 @@
#ifndef __FE_CHANNELS_H
#define __FE_CHANNELS_H
void fe_channels_nicklist(CHANNEL_REC *channel);
void fe_channels_init(void);
void fe_channels_deinit(void);
#endif

View File

@ -33,6 +33,7 @@
#include "formats.h"
#include "themes.h"
#include "translation.h"
#include "fe-channels.h"
#include "fe-windows.h"
#include "window-items.h"
#include "window-save.h"
@ -42,9 +43,6 @@
void autorun_init(void);
void autorun_deinit(void);
void fe_channels_init(void);
void fe_channels_deinit(void);
void fe_core_log_init(void);
void fe_core_log_deinit(void);

View File

@ -67,45 +67,72 @@ static int commands_compare(COMMAND_REC *rec, COMMAND_REC *rec2)
return strcmp(rec->cmd, rec2->cmd);
}
static void help_category(GSList *cmdlist, gint items, gint max)
static int get_cmd_length(void *data)
{
COMMAND_REC *rec;
GString *str;
GSList *tmp;
gint lines, cols, line, col, skip;
gchar *cmdbuf;
return strlen(((COMMAND_REC *) data)->cmd);
}
str = g_string_new(NULL);
static void help_category(GSList *cmdlist, int items)
{
WINDOW_REC *window;
TEXT_DEST_REC dest;
GString *str;
GSList *tmp;
int *columns, cols, rows, col, row, last_col_rows, max_width;
char *linebuf, *format, *stripped;
cols = max > 65 ? 1 : (65 / max);
lines = items <= cols ? 1 : items / cols+1;
window = window_find_closest(NULL, NULL, MSGLEVEL_CLIENTCRAP);
max_width = window->width;
cmdbuf = g_malloc(max+1); cmdbuf[max] = '\0';
for (line = 0, col = 0, skip = 1, tmp = cmdlist; line < lines; tmp = tmp->next)
{
rec = tmp->data;
if (--skip == 0)
{
skip = lines;
memset(cmdbuf, ' ', max);
memcpy(cmdbuf, rec->cmd, strlen(rec->cmd));
g_string_sprintfa(str, "%s ", cmdbuf);
cols++;
/* remove width of timestamp from max_width */
format_create_dest(&dest, NULL, NULL, MSGLEVEL_CLIENTCRAP, NULL);
format = format_get_line_start(current_theme, &dest, time(NULL));
if (format != NULL) {
stripped = strip_codes(format);
max_width -= strlen(stripped);
g_free(stripped);
g_free(format);
}
if (col == cols || tmp->next == NULL)
{
printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str->str);
g_string_truncate(str, 0);
col = 0; line++;
tmp = g_slist_nth(cmdlist, line-1); skip = 1;
/* calculate columns */
cols = get_max_column_count(cmdlist, get_cmd_length,
max_width, 1, 3, &columns, &rows);
cmdlist = columns_sort_list(cmdlist, rows);
/* rows in last column */
last_col_rows = rows-(cols*rows-g_slist_length(cmdlist));
if (last_col_rows == 0)
last_col_rows = rows;
str = g_string_new(NULL);
linebuf = g_malloc(max_width+1);
col = 0; row = 0;
for (tmp = cmdlist; tmp != NULL; tmp = tmp->next) {
COMMAND_REC *rec = tmp->data;
memset(linebuf, ' ', columns[col]);
linebuf[columns[col]] = '\0';
memcpy(linebuf, rec->cmd, strlen(rec->cmd));
g_string_append(str, linebuf);
if (++col == cols) {
printtext(NULL, NULL,
MSGLEVEL_CLIENTCRAP, "%s", str->str);
g_string_truncate(str, 0);
col = 0; row++;
if (row == last_col_rows)
cols--;
}
}
}
if (str->len != 0)
printtext_string(NULL, NULL, MSGLEVEL_CLIENTCRAP, str->str);
g_string_free(str, TRUE);
g_free(cmdbuf);
if (str->len != 0)
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s", str->str);
g_slist_free(cmdlist);
g_string_free(str, TRUE);
g_free(columns);
g_free(linebuf);
}
static int show_help_rec(COMMAND_REC *cmd)
@ -148,7 +175,7 @@ static void show_help(const char *data)
{
COMMAND_REC *rec, *last, *helpitem;
GSList *tmp, *cmdlist;
gint len, max, items, findlen;
gint items, findlen;
gboolean header, found;
g_return_if_fail(data != NULL);
@ -158,7 +185,7 @@ static void show_help(const char *data)
/* print command, sort by category */
cmdlist = NULL; last = NULL; header = FALSE; helpitem = NULL;
max = items = 0; findlen = strlen(data); found = FALSE;
items = 0; findlen = strlen(data); found = FALSE;
for (tmp = commands; tmp != NULL; last = rec, tmp = tmp->next)
{
rec = tmp->data;
@ -179,11 +206,11 @@ static void show_help(const char *data)
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
}
help_category(cmdlist, items, max);
help_category(cmdlist, items);
}
g_slist_free(cmdlist); cmdlist = NULL;
items = 0; max = 0;
items = 0;
}
if (last != NULL && g_strcasecmp(rec->cmd, last->cmd) == 0)
@ -201,8 +228,6 @@ static void show_help(const char *data)
else if (strchr(rec->cmd+findlen+1, ' ') == NULL)
{
/* not a subcommand (and matches the query) */
len = strlen(rec->cmd);
if (max < len) max = len;
items++;
cmdlist = g_slist_append(cmdlist, rec);
found = TRUE;
@ -236,7 +261,7 @@ static void show_help(const char *data)
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "");
printtext(NULL, NULL, MSGLEVEL_CLIENTCRAP, "%s:", last->category);
}
help_category(cmdlist, items, max);
help_category(cmdlist, items);
g_slist_free(cmdlist);
}
}

View File

@ -77,6 +77,9 @@ FORMAT_REC fecommon_core_formats[] = {
{ "talking_in", "You are now talking in {channel $0}", 1, { 0 } },
{ "not_in_channels", "You are not on any channels", 0 },
{ "current_channel", "Current channel {channel $0}", 1, { 0 } },
{ "names", "{names_users Users {names_channel $0}} $1", 2, { 0, 0 } },
{ "names_nick", "{names_nick $0 $1}", 2, { 0, 0 } },
{ "endofnames", "{channel $0}: Total of {hilight $1} nicks {comment {hilight $2} ops, {hilight $3} voices, {hilight $4} normal}", 5, { 0, 1, 1, 1, 1 } },
{ "chanlist_header", "You are on the following channels:", 0 },
{ "chanlist_line", "{channel $[-10]0} %|+$1 ($2): $3", 4, { 0, 0, 0, 0 } },
{ "chansetup_not_found", "Channel {channel $0} not found", 2, { 0, 0 } },

View File

@ -53,6 +53,9 @@ enum {
TXT_TALKING_IN,
TXT_NOT_IN_CHANNELS,
TXT_CURRENT_CHANNEL,
TXT_NAMES,
TXT_NAMES_NICK,
TXT_ENDOFNAMES,
TXT_CHANLIST_HEADER,
TXT_CHANLIST_LINE,
TXT_CHANSETUP_NOT_FOUND,

View File

@ -30,7 +30,9 @@
#include "irc-channels.h"
#include "nicklist.h"
#include "../core/module-formats.h"
#include "printtext.h"
#include "fe-channels.h"
static void event_received(IRC_SERVER_REC *server, const char *data);
@ -69,99 +71,14 @@ static void event_names_list(IRC_SERVER_REC *server, const char *data)
g_return_if_fail(data != NULL);
params = event_get_params(data, 4, NULL, NULL, &channel, &names);
if (irc_channel_find(server, channel) == NULL)
printformat(server, channel, MSGLEVEL_CRAP, IRCTXT_NAMES, channel, names);
if (irc_channel_find(server, channel) == NULL) {
printformat_module("fe-common/core", server, channel,
MSGLEVEL_CRAP, TXT_NAMES,
channel, names);
}
g_free(params);
}
static void display_sorted_nicks(CHANNEL_REC *channel, GSList *nicklist, gint items, gint max)
{
NICK_REC *rec;
GString *str;
GSList *tmp;
gint lines, cols, line, col, skip;
gchar *linebuf;
max++; /* op/voice */
str = g_string_new(NULL);
cols = max > 65 ? 1 : (65 / (max+3)); /* "[] " */
lines = items <= cols ? 1 : items/cols + 1;
if (lines > items) lines = items;
linebuf = g_malloc(max+1); linebuf[max] = '\0';
for (line = 0, col = 0, skip = 1, tmp = nicklist; line < lines; tmp = tmp->next)
{
rec = tmp->data;
if (--skip == 0)
{
char *ret, nickmode[2] = { 0, 0 };
skip = lines;
memset(linebuf, ' ', max);
nickmode[0] = rec->op ? '@' : rec->voice ? '+' : ' ';
memcpy(linebuf, rec->nick, strlen(rec->nick));
ret = format_get_text(MODULE_NAME, NULL,
channel->server, channel->name,
IRCTXT_NAMES_NICK, nickmode, linebuf);
g_string_append(str, ret);
g_free(ret);
cols++;
}
if (col == cols || tmp->next == NULL)
{
printtext(channel->server, channel->name, MSGLEVEL_CLIENTCRAP, "%s", str->str);
g_string_truncate(str, 0);
col = 0; line++;
tmp = g_slist_nth(nicklist, line-1); skip = 1;
}
}
if (str->len != 0)
printtext(channel->server, channel->name, MSGLEVEL_CLIENTCRAP, "%s", str->str);
g_string_free(str, TRUE);
g_free(linebuf);
}
static void display_nicks(CHANNEL_REC *channel)
{
NICK_REC *nick;
GSList *tmp, *nicklist, *sorted;
gint nicks, normal, voices, ops, len, max;
nicks = normal = voices = ops = 0;
nicklist = nicklist_getnicks(channel);
sorted = NULL;
/* sort the nicklist */
max = 0;
for (tmp = nicklist; tmp != NULL; tmp = tmp->next)
{
nick = tmp->data;
sorted = g_slist_insert_sorted(sorted, nick, (GCompareFunc) nicklist_compare);
if (nick->op)
ops++;
else if (nick->voice)
voices++;
else
normal++;
nicks++;
len = strlen(nick->nick);
if (len > max) max = len;
}
g_slist_free(nicklist);
/* display the nicks */
printformat(channel->server, channel->name, MSGLEVEL_CRAP, IRCTXT_NAMES, channel->name, "");
display_sorted_nicks(channel, sorted, nicks, max);
g_slist_free(sorted);
printformat(channel->server, channel->name, MSGLEVEL_CRAP, IRCTXT_ENDOFNAMES,
channel->name, nicks, ops, voices, normal);
}
static void event_end_of_names(IRC_SERVER_REC *server, const char *data)
{
char *params, *channel;
@ -172,10 +89,13 @@ static void event_end_of_names(IRC_SERVER_REC *server, const char *data)
params = event_get_params(data, 2, NULL, &channel);
chanrec = irc_channel_find(server, channel);
if (chanrec == NULL)
printformat(server, channel, MSGLEVEL_CRAP, IRCTXT_ENDOFNAMES, channel, 0, 0, 0, 0);
else
display_nicks(CHANNEL(chanrec));
if (chanrec != NULL)
fe_channels_nicklist(CHANNEL(chanrec));
else {
printformat_module("fe-common/core", server, channel,
MSGLEVEL_CRAP, TXT_ENDOFNAMES,
channel, 0, 0, 0, 0);
}
g_free(params);
}

View File

@ -59,9 +59,6 @@ FORMAT_REC fecommon_irc_formats[] = {
{ "channel_rejoin", "Channel {channel $0} is temporarily unavailable, this is normally because of netsplits. Irssi will now automatically try to rejoin back to this channel until the join is successful. Use /RMREJOINS command if you wish to abort this.", 1, { 0 } },
{ "inviting", "Inviting {nick $0} to {channel $1}", 2, { 0, 0 } },
{ "not_invited", "You have not been invited to a channel!", 0 },
{ "names", "{names_users Users {names_channel $0}} $1", 2, { 0, 0 } },
{ "names_nick", "{names_nick $0 $1}", 2, { 0, 0 } },
{ "endofnames", "{channel $0}: Total of {hilight $1} nicks {comment {hilight $2} ops, {hilight $3} voices, {hilight $4} normal}", 5, { 0, 1, 1, 1, 1 } },
{ "channel_created", "Channel {channelhilight $0} created $1", 2, { 0, 0 } },
{ "url", "Home page for {channelhilight $0}: $1", 2, { 0, 0 } },
{ "topic", "Topic for {channelhilight $0}: $1", 2, { 0, 0 } },

View File

@ -36,9 +36,6 @@ enum {
IRCTXT_CHANNEL_REJOIN,
IRCTXT_INVITING,
IRCTXT_NOT_INVITED,
IRCTXT_NAMES,
IRCTXT_NAMES_NICK,
IRCTXT_ENDOFNAMES,
IRCTXT_CHANNEL_CREATED,
IRCTXT_CHANNEL_URL,
IRCTXT_TOPIC,