mirror of
https://github.com/tmux/tmux.git
synced 2025-09-02 13:33:27 +00:00
Add a flag to reverse sort in the various choose modes, from Benjamin
Poirier in GitHub issue 1875.
This commit is contained in:
104
window-client.c
104
window-client.c
@ -75,6 +75,7 @@ static const char *window_client_sort_list[] = {
|
||||
"creation",
|
||||
"activity"
|
||||
};
|
||||
static struct mode_tree_sort_criteria *window_client_sort;
|
||||
|
||||
struct window_client_itemdata {
|
||||
struct client *c;
|
||||
@ -110,60 +111,48 @@ window_client_free_item(struct window_client_itemdata *item)
|
||||
}
|
||||
|
||||
static int
|
||||
window_client_cmp_name(const void *a0, const void *b0)
|
||||
window_client_cmp(const void *a0, const void *b0)
|
||||
{
|
||||
const struct window_client_itemdata *const *a = a0;
|
||||
const struct window_client_itemdata *const *b = b0;
|
||||
const struct window_client_itemdata *const *a = a0;
|
||||
const struct window_client_itemdata *const *b = b0;
|
||||
const struct window_client_itemdata *itema = *a;
|
||||
const struct window_client_itemdata *itemb = *b;
|
||||
struct client *ca = itema->c;
|
||||
struct client *cb = itemb->c;
|
||||
int result = 0;
|
||||
|
||||
return (strcmp((*a)->c->name, (*b)->c->name));
|
||||
}
|
||||
switch (window_client_sort->field) {
|
||||
case WINDOW_CLIENT_BY_SIZE:
|
||||
result = ca->tty.sx - cb->tty.sx;
|
||||
if (result == 0)
|
||||
result = ca->tty.sy - cb->tty.sy;
|
||||
break;
|
||||
case WINDOW_CLIENT_BY_CREATION_TIME:
|
||||
if (timercmp(&ca->creation_time, &cb->creation_time, >))
|
||||
result = -1;
|
||||
else if (timercmp(&ca->creation_time, &cb->creation_time, <))
|
||||
result = 1;
|
||||
break;
|
||||
case WINDOW_CLIENT_BY_ACTIVITY_TIME:
|
||||
if (timercmp(&ca->activity_time, &cb->activity_time, >))
|
||||
result = -1;
|
||||
else if (timercmp(&ca->activity_time, &cb->activity_time, <))
|
||||
result = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
static int
|
||||
window_client_cmp_size(const void *a0, const void *b0)
|
||||
{
|
||||
const struct window_client_itemdata *const *a = a0;
|
||||
const struct window_client_itemdata *const *b = b0;
|
||||
/* Use WINDOW_CLIENT_BY_NAME as default order and tie breaker. */
|
||||
if (result == 0)
|
||||
result = strcmp(ca->name, cb->name);
|
||||
|
||||
if ((*a)->c->tty.sx < (*b)->c->tty.sx)
|
||||
return (-1);
|
||||
if ((*a)->c->tty.sx > (*b)->c->tty.sx)
|
||||
return (1);
|
||||
if ((*a)->c->tty.sy < (*b)->c->tty.sy)
|
||||
return (-1);
|
||||
if ((*a)->c->tty.sy > (*b)->c->tty.sy)
|
||||
return (1);
|
||||
return (strcmp((*a)->c->name, (*b)->c->name));
|
||||
}
|
||||
|
||||
static int
|
||||
window_client_cmp_creation_time(const void *a0, const void *b0)
|
||||
{
|
||||
const struct window_client_itemdata *const *a = a0;
|
||||
const struct window_client_itemdata *const *b = b0;
|
||||
|
||||
if (timercmp(&(*a)->c->creation_time, &(*b)->c->creation_time, >))
|
||||
return (-1);
|
||||
if (timercmp(&(*a)->c->creation_time, &(*b)->c->creation_time, <))
|
||||
return (1);
|
||||
return (strcmp((*a)->c->name, (*b)->c->name));
|
||||
}
|
||||
|
||||
static int
|
||||
window_client_cmp_activity_time(const void *a0, const void *b0)
|
||||
{
|
||||
const struct window_client_itemdata *const *a = a0;
|
||||
const struct window_client_itemdata *const *b = b0;
|
||||
|
||||
if (timercmp(&(*a)->c->activity_time, &(*b)->c->activity_time, >))
|
||||
return (-1);
|
||||
if (timercmp(&(*a)->c->activity_time, &(*b)->c->activity_time, <))
|
||||
return (1);
|
||||
return (strcmp((*a)->c->name, (*b)->c->name));
|
||||
if (window_client_sort->reversed)
|
||||
result = -result;
|
||||
return (result);
|
||||
}
|
||||
|
||||
static void
|
||||
window_client_build(void *modedata, u_int sort_type, __unused uint64_t *tag,
|
||||
const char *filter)
|
||||
window_client_build(void *modedata, struct mode_tree_sort_criteria *sort_crit,
|
||||
__unused uint64_t *tag, const char *filter)
|
||||
{
|
||||
struct window_client_modedata *data = modedata;
|
||||
struct window_client_itemdata *item;
|
||||
@ -187,24 +176,9 @@ window_client_build(void *modedata, u_int sort_type, __unused uint64_t *tag,
|
||||
c->references++;
|
||||
}
|
||||
|
||||
switch (sort_type) {
|
||||
case WINDOW_CLIENT_BY_NAME:
|
||||
qsort(data->item_list, data->item_size, sizeof *data->item_list,
|
||||
window_client_cmp_name);
|
||||
break;
|
||||
case WINDOW_CLIENT_BY_SIZE:
|
||||
qsort(data->item_list, data->item_size, sizeof *data->item_list,
|
||||
window_client_cmp_size);
|
||||
break;
|
||||
case WINDOW_CLIENT_BY_CREATION_TIME:
|
||||
qsort(data->item_list, data->item_size, sizeof *data->item_list,
|
||||
window_client_cmp_creation_time);
|
||||
break;
|
||||
case WINDOW_CLIENT_BY_ACTIVITY_TIME:
|
||||
qsort(data->item_list, data->item_size, sizeof *data->item_list,
|
||||
window_client_cmp_activity_time);
|
||||
break;
|
||||
}
|
||||
window_client_sort = sort_crit;
|
||||
qsort(data->item_list, data->item_size, sizeof *data->item_list,
|
||||
window_client_cmp);
|
||||
|
||||
for (i = 0; i < data->item_size; i++) {
|
||||
item = data->item_list[i];
|
||||
|
Reference in New Issue
Block a user