Break sorting out into a common file so formats and modes use the same

code. Also add -O for sorting to the list commands. From Dane Jensen in
GitHub issue 4813.
This commit is contained in:
nicm
2026-02-02 10:08:30 +00:00
parent 66011fe48b
commit 3c3d9ce3a9
19 changed files with 946 additions and 674 deletions

View File

@@ -295,36 +295,48 @@ session_update_activity(struct session *s, struct timeval *from)
/* Find the next usable session. */
struct session *
session_next_session(struct session *s)
session_next_session(struct session *s, struct sort_criteria *sort_crit)
{
struct session *s2;
struct session **l;
u_int n, i;
if (RB_EMPTY(&sessions) || !session_alive(s))
return (NULL);
s2 = RB_NEXT(sessions, &sessions, s);
if (s2 == NULL)
s2 = RB_MIN(sessions, &sessions);
if (s2 == s)
return (NULL);
return (s2);
l = sort_get_sessions(&n, sort_crit);
for (i = 0; i < n; i++) {
if (l[i] == s)
break;
}
if (i == n)
fatalx("session %s not found in sorted list", s->name);
i++;
if (i == n)
i = 0;
return (l[i]);
}
/* Find the previous usable session. */
struct session *
session_previous_session(struct session *s)
session_previous_session(struct session *s, struct sort_criteria *sort_crit)
{
struct session *s2;
struct session **l;
u_int n, i;
if (RB_EMPTY(&sessions) || !session_alive(s))
return (NULL);
s2 = RB_PREV(sessions, &sessions, s);
if (s2 == NULL)
s2 = RB_MAX(sessions, &sessions);
if (s2 == s)
return (NULL);
return (s2);
l = sort_get_sessions(&n, sort_crit);
for (i = 0; i < n; i++) {
if (l[i] == s)
break;
}
if (i == n)
fatalx("session %s not found in sorted list", s->name);
if (i == 0)
i = n;
i--;
return (l[i]);
}
/* Attach a window to a session. */