Thanks, I have applied this but I just rolled server_sessions_count into
format.c instead of having a separate function to use once. It'll be in
GitHub next time it syncs up.



On Fri, Apr 28, 2023 at 08:32:41PM +0200, Magnus Groß wrote:
> This can be useful for example when the user wants to show the total
> number of sessions in the statusline.
> 
> Co-authored-by: Magnus Groß <mag...@mggross.com>
> ---
>  format.c | 10 ++++++++++
>  server.c | 15 +++++++++++++++
>  tmux.1   |  1 +
>  tmux.h   |  1 +
>  4 files changed, 27 insertions(+)
> 
> diff --git a/format.c b/format.c
> index 275d5218..e5728327 100644
> --- a/format.c
> +++ b/format.c
> @@ -2077,6 +2077,13 @@ format_cb_scroll_region_upper(struct format_tree *ft)
>       return (NULL);
>  }
>  
> +/* Callback for server_sessions. */
> +static void *
> +format_cb_server_sessions(__unused struct format_tree *ft)
> +{
> +     return (format_printf("%u", server_sessions_count()));
> +}
> +
>  /* Callback for session_attached. */
>  static void *
>  format_cb_session_attached(struct format_tree *ft)
> @@ -2980,6 +2987,9 @@ static const struct format_table_entry format_table[] = 
> {
>       { "scroll_region_upper", FORMAT_TABLE_STRING,
>         format_cb_scroll_region_upper
>       },
> +     { "server_sessions", FORMAT_TABLE_STRING,
> +       format_cb_server_sessions
> +     },
>       { "session_activity", FORMAT_TABLE_TIME,
>         format_cb_session_activity
>       },
> diff --git a/server.c b/server.c
> index 6fef468b..a0b62a71 100644
> --- a/server.c
> +++ b/server.c
> @@ -555,3 +555,18 @@ server_add_message(const char *fmt, ...)
>               free(msg);
>       }
>  }
> +
> +/* Count number of sessions. */
> +u_int
> +server_sessions_count()
> +{
> +     struct session  *s;
> +     u_int    n = 0;
> +
> +     n = 0;
> +     RB_FOREACH(s, sessions, &sessions) {
> +             n++;
> +     }
> +
> +     return (n);
> +}
> diff --git a/tmux.1 b/tmux.1
> index 92e2915f..7bf2bc4d 100644
> --- a/tmux.1
> +++ b/tmux.1
> @@ -5278,6 +5278,7 @@ The following variables are available, where 
> appropriate:
>  .It Li "selection_present" Ta "" Ta "1 if selection started in copy mode"
>  .It Li "selection_start_x" Ta "" Ta "X position of the start of the 
> selection"
>  .It Li "selection_start_y" Ta "" Ta "Y position of the start of the 
> selection"
> +.It Li "server_sessions" Ta "" Ta "Number of sessions"
>  .It Li "session_activity" Ta "" Ta "Time of session last activity"
>  .It Li "session_alerts" Ta "" Ta "List of window indexes with alerts"
>  .It Li "session_attached" Ta "" Ta "Number of clients session is attached to"
> diff --git a/tmux.h b/tmux.h
> index e2becb1c..1a4a14ee 100644
> --- a/tmux.h
> +++ b/tmux.h
> @@ -2653,6 +2653,7 @@ void     server_update_socket(void);
>  void  server_add_accept(int);
>  void printflike(1, 2) server_add_message(const char *, ...);
>  int   server_create_socket(int, char **);
> +u_int server_sessions_count(void);
>  
>  /* server-client.c */
>  RB_PROTOTYPE(client_windows, client_window, entry, server_client_window_cmp);
> -- 
> 2.40.1
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "tmux-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to tmux-users+unsubscr...@googlegroups.com.
> To view this discussion on the web, visit 
> https://groups.google.com/d/msgid/tmux-users/20230428183241.28221-1-magnus%40mggross.com.

-- 
You received this message because you are subscribed to the Google Groups 
"tmux-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to tmux-users+unsubscr...@googlegroups.com.
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/tmux-users/q2tac4lv42bdavbihszrsj3oc7ertztdnmkl7yi7ddirnjbqpi%40yqyxvgo43nxp.

Reply via email to