ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
discovery_tui.c File Reference

TUI-based service discovery wrapper for interactive server selection. More...

Go to the source code of this file.

Macros

#define ANSI_CLEAR   "\033[2J\033[H"
 ANSI escape codes for TUI.
 
#define ANSI_BOLD   "\033[1m"
 
#define ANSI_RESET   "\033[0m"
 
#define ANSI_CYAN   "\033[36m"
 
#define ANSI_GREEN   "\033[32m"
 
#define ANSI_YELLOW   "\033[33m"
 
#define ANSI_HIDE_CURSOR   "\033[?25l"
 
#define ANSI_SHOW_CURSOR   "\033[?25h"
 
#define ANSI_CLEAR_LINE   "\033[K"
 

Functions

discovery_tui_server_t * discovery_tui_query (const discovery_tui_config_t *config, int *out_count)
 TUI wrapper around core mDNS discovery.
 
void discovery_tui_free_results (discovery_tui_server_t *servers)
 Free results from mDNS discovery.
 
int discovery_tui_prompt_selection (const discovery_tui_server_t *servers, int count)
 Interactive server selection.
 
int discovery_tui_select (const discovery_tui_server_t *servers, int count)
 TUI-based server selection with formatted display.
 
const char * discovery_tui_get_best_address (const discovery_tui_server_t *server)
 Get best address for a server.
 

Detailed Description

TUI-based service discovery wrapper for interactive server selection.

Pure TUI wrapper that calls discovery_mdns_query() from discovery.c. Provides interactive terminal UI for server selection and address resolution.

Definition in file discovery_tui.c.

Macro Definition Documentation

◆ ANSI_BOLD

#define ANSI_BOLD   "\033[1m"

Definition at line 100 of file discovery_tui.c.

◆ ANSI_CLEAR

#define ANSI_CLEAR   "\033[2J\033[H"

ANSI escape codes for TUI.

Definition at line 99 of file discovery_tui.c.

◆ ANSI_CLEAR_LINE

#define ANSI_CLEAR_LINE   "\033[K"

Definition at line 107 of file discovery_tui.c.

◆ ANSI_CYAN

#define ANSI_CYAN   "\033[36m"

Definition at line 102 of file discovery_tui.c.

◆ ANSI_GREEN

#define ANSI_GREEN   "\033[32m"

Definition at line 103 of file discovery_tui.c.

◆ ANSI_HIDE_CURSOR

#define ANSI_HIDE_CURSOR   "\033[?25l"

Definition at line 105 of file discovery_tui.c.

◆ ANSI_RESET

#define ANSI_RESET   "\033[0m"

Definition at line 101 of file discovery_tui.c.

◆ ANSI_SHOW_CURSOR

#define ANSI_SHOW_CURSOR   "\033[?25h"

Definition at line 106 of file discovery_tui.c.

◆ ANSI_YELLOW

#define ANSI_YELLOW   "\033[33m"

Definition at line 104 of file discovery_tui.c.

Function Documentation

◆ discovery_tui_free_results()

void discovery_tui_free_results ( discovery_tui_server_t *  servers)

Free results from mDNS discovery.

Definition at line 47 of file discovery_tui.c.

47 {
49}
void discovery_mdns_destroy(discovery_tui_server_t *servers)
Free memory from mDNS discovery results.
Definition discovery.c:267

References discovery_mdns_destroy().

Referenced by client_main().

◆ discovery_tui_get_best_address()

const char * discovery_tui_get_best_address ( const discovery_tui_server_t *  server)

Get best address for a server.

Definition at line 208 of file discovery_tui.c.

208 {
209 if (!server) {
210 return "";
211 }
212
213 // Prefer IPv4 > name > IPv6
214 if (server->ipv4[0] != '\0') {
215 return server->ipv4;
216 }
217 if (server->name[0] != '\0') {
218 return server->name;
219 }
220 if (server->ipv6[0] != '\0') {
221 return server->ipv6;
222 }
223
224 return server->address; // Fallback to address field
225}

Referenced by client_main(), discovery_tui_prompt_selection(), and discovery_tui_select().

◆ discovery_tui_prompt_selection()

int discovery_tui_prompt_selection ( const discovery_tui_server_t *  servers,
int  count 
)

Interactive server selection.

Definition at line 54 of file discovery_tui.c.

54 {
55 if (!servers || count <= 0) {
56 return -1;
57 }
58
59 // Display available servers
60 printf("\nAvailable ascii-chat servers on LAN:\n");
61 for (int i = 0; i < count; i++) {
62 const discovery_tui_server_t *srv = &servers[i];
63 const char *addr = discovery_tui_get_best_address(srv);
64 printf(" %d. %s (%s:%u)\n", i + 1, srv->name, addr, srv->port);
65 }
66
67 // Prompt for selection
68 printf("\nSelect server (1-%d) or press Enter to cancel: ", count);
69 fflush(stdout);
70
71 // Read user input
72 char input[32];
73 if (fgets(input, sizeof(input), stdin) == NULL) {
74 printf("\n");
75 return -1; // EOF or error
76 }
77
78 // Check for empty input (Enter pressed)
79 if (input[0] == '\n' || input[0] == '\r' || input[0] == '\0') {
80 return -1; // User cancelled
81 }
82
83 // Parse input as number
84 char *endptr;
85 long selection = strtol(input, &endptr, 10);
86
87 // Validate input
88 if (selection < 1 || selection > count) {
89 printf("āš ļø Invalid selection. Please enter a number between 1 and %d\n", count);
90 return discovery_tui_prompt_selection(servers, count); // Re-prompt
91 }
92
93 return (int)(selection - 1); // Convert to 0-based index
94}
int discovery_tui_prompt_selection(const discovery_tui_server_t *servers, int count)
Interactive server selection.
const char * discovery_tui_get_best_address(const discovery_tui_server_t *server)
Get best address for a server.

References discovery_tui_get_best_address(), and discovery_tui_prompt_selection().

Referenced by discovery_tui_prompt_selection().

◆ discovery_tui_query()

discovery_tui_server_t * discovery_tui_query ( const discovery_tui_config_t *  config,
int *  out_count 
)

TUI wrapper around core mDNS discovery.

Calls discovery_mdns_query() from discovery.c with TUI-friendly configuration.

Definition at line 27 of file discovery_tui.c.

27 {
28 if (!out_count) {
29 SET_ERRNO(ERROR_INVALID_PARAM, "out_count pointer is NULL");
30 return NULL;
31 }
32
33 *out_count = 0;
34
35 // Apply defaults if needed
36 int timeout_ms = (config && config->timeout_ms > 0) ? config->timeout_ms : 2000;
37 int max_servers = (config && config->max_servers > 0) ? config->max_servers : 20;
38 bool quiet = (config && config->quiet);
39
40 // Call the core mDNS discovery function from discovery.c
41 return discovery_mdns_query(timeout_ms, max_servers, quiet, out_count);
42}
discovery_tui_server_t * discovery_mdns_query(int timeout_ms, int max_servers, bool quiet, int *out_count)
Public mDNS query function used by both parallel discovery and TUI wrapper.
Definition discovery.c:179

References discovery_mdns_query().

Referenced by client_main().

◆ discovery_tui_select()

int discovery_tui_select ( const discovery_tui_server_t *  servers,
int  count 
)

TUI-based server selection with formatted display.

Displays discovered servers in a terminal UI with the following features:

  • Clears terminal and displays formatted server list
  • Shows "No results" message if no servers available
  • Allows numeric input for selection
  • Shows helpful prompts and icons
Parameters
serversArray of discovered servers
countNumber of servers
Returns
0-based index of selected server, or -1 to cancel

Definition at line 122 of file discovery_tui.c.

122 {
123 if (!servers || count <= 0) {
124 // No servers found - return special code
125 // Message will be printed at exit in client main
126 return -1;
127 }
128
129 // Lock terminal to prevent concurrent logging from overwriting TUI
130 bool prev_lock_state = log_lock_terminal();
131
132 // Clear terminal
133 log_plain("%s", ANSI_CLEAR);
134
135 // Display header
136 log_plain("\n");
137 log_plain("%s╭─ šŸ” ascii-chat Server Discovery %s────────────╮%s\n", ANSI_BOLD, ANSI_RESET, ANSI_BOLD);
138 log_plain("│%s\n", ANSI_RESET);
139 log_plain("%s│%s Found %d server%s on your local network:%s\n", ANSI_BOLD, ANSI_GREEN, count, count == 1 ? "" : "s",
140 ANSI_RESET);
141 log_plain("%s│%s\n", ANSI_BOLD, ANSI_RESET);
142
143 // Display server list with formatting
144 for (int i = 0; i < count; i++) {
145 const discovery_tui_server_t *srv = &servers[i];
146 const char *addr = discovery_tui_get_best_address(srv);
147
148 log_plain("%s│%s ", ANSI_BOLD, ANSI_RESET);
149 log_plain("%s[%d]%s %-30s %s%s:%u%s", ANSI_CYAN, i + 1, ANSI_RESET, srv->name, ANSI_YELLOW, addr, srv->port,
150 ANSI_RESET);
151 log_plain("\n");
152 }
153
154 log_plain("%s│%s\n", ANSI_BOLD, ANSI_RESET);
155 log_plain("%s╰────────────────────────────────────────────╯%s\n", ANSI_BOLD, ANSI_RESET);
156
157 // Prompt for selection
158 log_plain("\n");
159 log_plain("Enter server number (1-%d) or press Enter to cancel: ", count);
160 fflush(stdout);
161
162 // Unlock before waiting for input (user might take time)
163 log_unlock_terminal(prev_lock_state);
164
165 // Read user input
166 char input[32];
167 if (fgets(input, sizeof(input), stdin) == NULL) {
168 return -1;
169 }
170
171 // Check for empty input (Enter pressed)
172 if (input[0] == '\n' || input[0] == '\r' || input[0] == '\0') {
173 return -1;
174 }
175
176 // Parse input as number
177 char *endptr;
178 long selection = strtol(input, &endptr, 10);
179
180 // Validate input
181 if (selection < 1 || selection > count) {
182 printf("%sError:%s Please enter a number between 1 and %d\n\n", ANSI_YELLOW, ANSI_RESET, count);
183 return discovery_tui_select(servers, count); // Re-prompt
184 }
185
186 // Lock terminal again for final output
187 prev_lock_state = log_lock_terminal();
188
189 // Clear screen and show connection status
190 log_plain("%s", ANSI_CLEAR);
191 log_plain("\n");
192 log_plain("%sšŸ”— Connecting to %s...%s\n", ANSI_GREEN, servers[selection - 1].name, ANSI_RESET);
193 log_plain("\n");
194 fflush(stdout);
195
196 // Brief delay so user can see the selection before logs overwrite it
198
199 // Unlock terminal now that TUI is complete
200 log_unlock_terminal(prev_lock_state);
201
202 return (int)(selection - 1); // Convert to 0-based index
203}
#define ANSI_BOLD
#define ANSI_CLEAR
ANSI escape codes for TUI.
int discovery_tui_select(const discovery_tui_server_t *servers, int count)
TUI-based server selection with formatted display.
#define ANSI_CYAN
#define ANSI_GREEN
#define ANSI_YELLOW
#define ANSI_RESET
bool log_lock_terminal(void)
void log_unlock_terminal(bool previous_state)
void platform_sleep_ms(unsigned int ms)

References ANSI_BOLD, ANSI_CLEAR, ANSI_CYAN, ANSI_GREEN, ANSI_RESET, ANSI_YELLOW, discovery_tui_get_best_address(), discovery_tui_select(), log_lock_terminal(), log_unlock_terminal(), and platform_sleep_ms().

Referenced by client_main(), and discovery_tui_select().