ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
status.c File Reference

Status screen display with live log feed at FPS rate. More...

Go to the source code of this file.

Functions

const char * ui_status_log_controls (void)
void ui_status_log_init (void)
 Initialize status screen log capture system.
void ui_status_log_destroy (void)
 Cleanup status screen log capture system.
void ui_status_log_clear (void)
 Clear all log messages from status screen buffer.
asciichat_error_t ui_status_gather (network_server_t *server, const char *session_string, const char *ipv4_address, const char *ipv6_address, uint16_t port, time_t start_time, const char *mode_name, bool session_is_mdns_only, ui_status_t *out_status)
 Gather current status information.
void ui_status_display (const ui_status_t *status)
 Display status screen.
bool ui_status_display_interactive (const ui_status_t *status)
 Display status screen with keyboard input support Returns true if status screen should continue, false if Escape was pressed to exit.
void ui_status_update (network_server_t *server, const char *session_string, const char *ipv4_address, const char *ipv6_address, uint16_t port, time_t start_time, const char *mode_name, bool session_is_mdns_only, uint64_t *last_update_ns)
 Periodically update status display with live logs at FPS rate.

Detailed Description

Status screen display with live log feed at FPS rate.

Definition in file status.c.

Function Documentation

◆ ui_status_display()

void ui_status_display ( const ui_status_t * status)

Display status screen.

Outputs formatted status to stdout. Uses ANSI escape codes for clean display that doesn't interfere with logging output.

Parameters
statusStatus information

Definition at line 115 of file status.c.

115 {
116 if (!status) {
117 return;
118 }
119
120 // Only render the status screen if:
121 // 1. Terminal is interactive (default behavior), OR
122 // 2. User explicitly set --status-screen on command line (force it regardless
123 // of terminal) In non-interactive mode without explicit flag, logs flow to
124 // stdout/stderr normally
126 !flags_explicit_state_was_set("status-screen")) {
127 return;
128 }
129
130 // Redirect status screen to stderr when stdout is piped (not a TTY).
131 // This keeps stdout clean for frame data while status goes to stderr.
132 // Uses platform_isatty() so non-desktop platforms (iOS, WASM) can override.
133 if (!platform_isatty(STDOUT_FILENO)) {
134 terminal_screen_set_output_fd(STDERR_FILENO);
135 }
136
137 // If --grep pattern was provided, enter interactive grep mode with it
138 // pre-populated Only do this once (check if not already entering)
139 static bool grep_mode_entered = false;
140 if (!grep_mode_entered && grep_get_last_pattern() &&
141 grep_get_last_pattern()[0] != '\0') {
143 grep_mode_entered = true;
144 }
145
146 // Use terminal_screen abstraction for rendering
147 ui_status_t rendered = *status;
148 if (!rendered.detail)
149 rendered.detail = ui_status_log_controls();
150 terminal_screen_config_t config = {
151 .screen = UI_SCREEN_STATUS,
152 .output_fd =
153 platform_isatty(STDOUT_FILENO) ? STDOUT_FILENO : STDERR_FILENO,
154 .user_data_size = sizeof(*status),
155 .minimum_cols = 20,
156 .fixed_header_lines = 4,
157 .render_header = ui_status_render_header,
158 .user_data = &rendered,
159 .show_logs = true,
160 };
161
162 terminal_screen_render(&config);
163}
const char * grep_get_last_pattern(void)
Get the last (most recent) filter pattern string.
Definition grep.c:1198
void log_search_enter_mode(void)
Enter search mode (user pressed '/').
Definition search.c:233
void ui_status_render_header(frame_buffer_t *buf, terminal_size_t term_size, void *user_data)
bool terminal_is_interactive(void)
const char * ui_status_log_controls(void)
Definition status.c:33
Configuration for terminal screen rendering.
Status information for server/discovery service modes.
Definition status.h:43
const char * detail
Optional client controls in the address row.
Definition status.h:59
void terminal_screen_render(const terminal_screen_config_t *config)
Render a terminal screen with fixed header and scrolling logs.
void terminal_screen_set_output_fd(int fd)
Set output file descriptor for terminal screens (splash/status).
@ UI_SCREEN_STATUS
int platform_isatty(int fd)
Definition util.c:70

References ui_status_t::detail, grep_get_last_pattern(), log_search_enter_mode(), platform_isatty(), terminal_is_interactive(), terminal_screen_render(), terminal_screen_set_output_fd(), UI_SCREEN_STATUS, ui_status_log_controls(), and ui_status_render_header().

Referenced by ui_status_update().

◆ ui_status_display_interactive()

bool ui_status_display_interactive ( const ui_status_t * status)

Display status screen with keyboard input support Returns true if status screen should continue, false if Escape was pressed to exit.

Display status screen with interactive keyboard support.

Definition at line 170 of file status.c.

170 {
171 if (!status) {
172 return true;
173 }
174
175 // Only render the status screen if:
176 // 1. Terminal is interactive (default behavior), OR
177 // 2. User explicitly set --status-screen on command line (force it regardless
178 // of terminal) In non-interactive mode without explicit flag, logs flow to
179 // stdout/stderr normally
181 !flags_explicit_state_was_set("status-screen")) {
182 return true;
183 }
184
185 // If --grep pattern was provided, enter interactive grep mode with it
186 // pre-populated
187 static bool grep_mode_entered = false;
188 if (!grep_mode_entered && grep_get_last_pattern() &&
189 grep_get_last_pattern()[0] != '\0') {
191 grep_mode_entered = true;
192 }
193
194 // Status may use stderr when stdout is redirected. Check the descriptors
195 // used by this screen instead of requiring stdout to be a terminal.
196 int output_fd =
197 platform_isatty(STDOUT_FILENO) ? STDOUT_FILENO : STDERR_FILENO;
198 bool keyboard_enabled = false;
199 if (platform_isatty(STDIN_FILENO) && platform_isatty(output_fd) &&
200 keyboard_init() == ASCIICHAT_OK) {
201 keyboard_enabled = true;
202 }
203
204 // Use terminal_screen abstraction for rendering
205 ui_status_t rendered = *status;
206 if (!rendered.detail)
207 rendered.detail = ui_status_log_controls();
208 terminal_screen_config_t config = {
209 .screen = UI_SCREEN_STATUS,
210 .output_fd = output_fd,
211 .user_data_size = sizeof(*status),
212 .minimum_cols = 20,
213 .fixed_header_lines = 4,
214 .render_header = ui_status_render_header,
215 .user_data = &rendered,
216 .show_logs = true,
217 };
218
219 terminal_screen_render(&config);
220
221 // Poll keyboard for Escape to exit or for interactive grep
222 bool should_exit_status = false;
223 if (keyboard_enabled) {
224 keyboard_key_t key = ui_input_read_key(UI_SCREEN_STATUS);
226 return true;
227 } else if (key == KEY_BACKTICK) {
229 } else if (key == KEY_ESCAPE) {
230 // Escape key: cancel grep if active, otherwise exit status screen
231 if (log_search_is_active()) {
232 log_search_exit_mode(false); // Cancel grep without applying
233 } else {
234 should_exit_status = true; // Exit status screen
235 }
236 } else if (key != KEY_NONE && log_search_should_handle(key)) {
238 }
239 }
240
241 // Keyboard ownership lasts until session shutdown, including between status
242 // frames.
243
244 return !should_exit_status; // Return false if user wants to exit
245}
void ui_errno_stacks_toggle(void)
bool ui_errno_stacks_handle_key(keyboard_key_t key)
@ ASCIICHAT_OK
Definition error_codes.h:50
bool log_search_is_active(void)
Check if filtering is active.
Definition search.c:430
void log_search_exit_mode(bool accept)
Exit search mode.
Definition search.c:312
bool log_search_should_handle(int key)
Check if a key should be handled by grep module.
Definition search.c:441
asciichat_error_t log_search_handle_key(keyboard_key_t key)
Process keyboard input for grep.
Definition search.c:532
keyboard_key_t ui_input_read_key(ui_screen_t screen)
Definition tui/input.c:374

References ASCIICHAT_OK, ui_status_t::detail, grep_get_last_pattern(), log_search_enter_mode(), log_search_exit_mode(), log_search_handle_key(), log_search_is_active(), log_search_should_handle(), platform_isatty(), terminal_is_interactive(), terminal_screen_render(), ui_errno_stacks_handle_key(), ui_errno_stacks_toggle(), ui_input_read_key(), UI_SCREEN_STATUS, ui_status_log_controls(), and ui_status_render_header().

◆ ui_status_gather()

asciichat_error_t ui_status_gather ( network_server_t * server,
const char * session_string,
const char * ipv4_address,
const char * ipv6_address,
uint16_t port,
time_t start_time,
const char * mode_name,
bool session_is_mdns_only,
ui_status_t * out_status )

Gather current status information.

Collects current status information including connected count, bind addresses, and session string.

Parameters
serverInitialized TCP server structure
session_stringMemorable session string
ipv4_addressIPv4 bind address (can be NULL or empty)
ipv6_addressIPv6 bind address (can be NULL or empty)
portTCP listen port
start_timeServer start time
mode_nameMode name for display (e.g., "Server")
session_is_mdns_onlyWhether session is mDNS-only or ACDS
[out]out_statusOutput status structure
Returns
ASCIICHAT_OK on success

Definition at line 76 of file status.c.

79 {
80 if (!server || !out_status || !mode_name) {
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid parameters");
82 }
83
84 memset(out_status, 0, sizeof(*out_status));
85
86 // Copy session string
87 if (session_string) {
88 SAFE_STRNCPY(out_status->session_string, session_string,
89 sizeof(out_status->session_string));
90 }
91
92 // Format IPv4 address
93 out_status->ipv4_bound = (ipv4_address && ipv4_address[0] != '\0');
94 if (out_status->ipv4_bound) {
95 snprintf(out_status->ipv4_address, sizeof(out_status->ipv4_address),
96 "%s:%u", ipv4_address, port);
97 }
98
99 // Format IPv6 address
100 out_status->ipv6_bound = (ipv6_address && ipv6_address[0] != '\0');
101 if (out_status->ipv6_bound) {
102 snprintf(out_status->ipv6_address, sizeof(out_status->ipv6_address),
103 "[%s]:%u", ipv6_address, port);
104 }
105
106 out_status->port = port;
107 out_status->start_time = start_time;
108 out_status->mode_name = mode_name;
109 out_status->session_is_mdns_only = session_is_mdns_only;
111
112 return ASCIICHAT_OK;
113}
#define SET_ERRNO(code,...)
#define SAFE_STRNCPY(dst, src, size)
@ ERROR_INVALID_PARAM
size_t network_server_connection_count(network_server_t *s)
bool ipv4_bound
Whether IPv4 socket is bound.
Definition status.h:55
char ipv4_address[256]
Definition status.h:47
bool ipv6_bound
Whether IPv6 socket is bound.
Definition status.h:56
uint16_t port
TCP listen port.
Definition status.h:53
char ipv6_address[256]
Definition status.h:50
char session_string[64]
(e.g., "happy-sunset-ocean")
Definition status.h:45
const char * mode_name
Mode name (e.g., "Server", "Discovery Service").
Definition status.h:60
time_t start_time
Server start time (for uptime calculation).
Definition status.h:58
bool session_is_mdns_only
Whether session is mDNS-only or ACDS.
Definition status.h:57
size_t connected_count
Number of connected clients/servers.
Definition status.h:54

References ASCIICHAT_OK, ui_status_t::connected_count, ERROR_INVALID_PARAM, ui_status_t::ipv4_address, ui_status_t::ipv4_bound, ui_status_t::ipv6_address, ui_status_t::ipv6_bound, ui_status_t::mode_name, network_server_connection_count(), ui_status_t::port, SAFE_STRNCPY, ui_status_t::session_is_mdns_only, ui_status_t::session_string, SET_ERRNO, and ui_status_t::start_time.

Referenced by ui_status_update().

◆ ui_status_log_clear()

void ui_status_log_clear ( void )

Clear all log messages from status screen buffer.

Thread-safe. Resets buffer to empty state, discarding all captured logs. Used to clear initialization logs before status screen starts rendering.

Definition at line 66 of file status.c.

66 {
67 // Delegate to terminal_screen log abstraction
69}
void terminal_screen_log_clear(void)
Clear buffered logs for terminal screens.

References terminal_screen_log_clear().

◆ ui_status_log_controls()

const char * ui_status_log_controls ( void )

Shared controls line shown by every interactive log viewer.

Definition at line 33 of file status.c.

33 {
34 static _Thread_local char controls[512];
35 snprintf(controls, sizeof(controls),
36 "%s / %s: close | %s: search logs | %s: keyboard controls | %s / "
37 "%s: one log | "
38 "%s / %s: page | %s / %s: oldest/newest",
40 colored_string(LOG_COLOR_WARN, "Esc"), "/",
45 colored_string(LOG_COLOR_WARN, "PageDown"),
48 return controls;
49}
@ LOG_COLOR_WARN
Definition color.h:10
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.

References colored_string(), and LOG_COLOR_WARN.

Referenced by ui_session_logs_render(), ui_status_display(), and ui_status_display_interactive().

◆ ui_status_log_destroy()

void ui_status_log_destroy ( void )

Cleanup status screen log capture system.

Call when shutting down. Frees internal log buffer.

Definition at line 59 of file status.c.

59 {
60 // Unregister from logger before destroying
62 // Cleanup the shared terminal screen log buffer
64}
void log_clear_session_log_buffer(void)
Unregister the session log buffer.
Definition log/log.c:2272
void terminal_screen_log_destroy(void)
Standard log cleanup for terminal screens.

References log_clear_session_log_buffer(), and terminal_screen_log_destroy().

Referenced by session_server_like_run().

◆ ui_status_log_init()

void ui_status_log_init ( void )

Initialize status screen log capture system.

Must be called before using status screen. Creates internal log buffer.

Definition at line 51 of file status.c.

51 {
52 // Initialize the shared terminal screen log buffer
54 if (buf) {
56 }
57}
struct session_log_buffer session_log_buffer_t
Thread-safe fixed-size history ring for session-screen log lines.
void log_set_session_log_buffer(session_log_buffer_t *buf)
Register a session log buffer with the logger.
Definition log/log.c:2263
session_log_buffer_t * terminal_screen_log_init(void)
Initialize session log buffer for terminal screens.

References log_set_session_log_buffer(), and terminal_screen_log_init().

Referenced by session_server_like_run().

◆ ui_status_update()

void ui_status_update ( network_server_t * server,
const char * session_string,
const char * ipv4_address,
const char * ipv6_address,
uint16_t port,
time_t start_time,
const char * mode_name,
bool session_is_mdns_only,
uint64_t * last_update_ns )

Periodically update status display with live logs at FPS rate.

Gathers and displays status with live log feed if enough time has passed since last update (based on GET_OPTION(fps)). Updates at 60 Hz by default.

Parameters
serverInitialized TCP server structure
session_stringMemorable session string
ipv4_addressIPv4 bind address (can be NULL or empty)
ipv6_addressIPv6 bind address (can be NULL or empty)
portTCP listen port
start_timeServer start time
mode_nameMode name for display (e.g., "Server")
session_is_mdns_onlyWhether session is mDNS-only or ACDS
[in,out]last_update_nsLast update time in microseconds (from platform_get_monotonic_time_us)

Definition at line 247 of file status.c.

250 {
251 if (!server || !last_update_ns) {
252 return;
253 }
254
255 // Update at FPS rate (60 Hz = 16.67ms by default)
256 uint32_t fps = GET_OPTION(fps);
257 if (fps == 0) {
258 fps = 60; // Default
259 }
260
261 // Calculate frame interval in microseconds
262 uint64_t frame_interval_us = US_PER_SEC_INT / fps;
263
264 // Get current time in microseconds using platform abstraction
265 uint64_t now_us = platform_get_monotonic_time_us();
266
267 // Check if enough time has passed
268 if ((now_us - *last_update_ns) < frame_interval_us) {
269 return; // Too soon, skip frame
270 }
271
272 ui_status_t status;
273 if (ui_status_gather(server, session_string, ipv4_address, ipv6_address, port,
274 start_time, mode_name, session_is_mdns_only,
275 &status) == ASCIICHAT_OK) {
276 ui_status_display(&status);
277 *last_update_ns = now_us;
278 }
279}
unsigned int uint32_t
unsigned long long uint64_t
#define US_PER_SEC_INT
Definition time.h:177
#define GET_OPTION(field)
Definition options.h:1339
asciichat_error_t ui_status_gather(network_server_t *server, const char *session_string, const char *ipv4_address, const char *ipv6_address, uint16_t port, time_t start_time, const char *mode_name, bool session_is_mdns_only, ui_status_t *out_status)
Gather current status information.
Definition status.c:76
void ui_status_display(const ui_status_t *status)
Display status screen.
Definition status.c:115

References ASCIICHAT_OK, GET_OPTION, ui_status_display(), ui_status_gather(), and US_PER_SEC_INT.