cli_chat_core 0.1.0

Zero-heap, single-threaded CLI chat core for embedded devices.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! # Summary
//! Viewport rendering and UI drawing logic.
//!
//! # Description
//! Handles the split-screen ANSI rendering, drawing the divider,
//! and updating the locked input prompt line.

use crate::serial::SerialPort;
use crate::state::AppState;
use crate::state::ansi::{
    CLEAR_SCREEN, COLOR_DIVIDER, COLOR_INFO, COLOR_PROMPT, COLOR_RESET, COLOR_SYSTEM,
    CURSOR_DIVIDER_LINE, CURSOR_HOME, CURSOR_INPUT_LINE, CURSOR_SCROLL_BOTTOM, ERASE_LINE,
    RESTORE_CURSOR, SAVE_CURSOR, SCROLL_REGION_22,
};
use crate::types::Message;
use crate::utils::write_u32;

/// # Summary
/// Writes a string slice to the serial port byte-by-byte.
///
/// # Description
/// Iterates over the UTF-8 bytes of the string and transmits them synchronously.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::write_str(&mut port, "Hello");
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`crate::serial::SerialPort::write_byte`]
pub fn write_str(port: &mut impl SerialPort, string: &str) {
    for &byte in string.as_bytes() {
        port.write_byte(byte);
    }
}

/// # Summary
/// Initializes the ANSI split-screen viewport.
///
/// # Description
/// Clears the screen, sets the DECSTBM scrolling region to lines 1-22,
/// draws a divider on line 23, and moves the cursor to the input line 24.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::init_viewport(&mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_prompt`]
pub fn init_viewport(port: &mut impl SerialPort) {
    write_str(port, CLEAR_SCREEN);
    write_str(port, CURSOR_HOME);
    write_str(port, SCROLL_REGION_22);

    write_str(port, CURSOR_DIVIDER_LINE);
    write_str(port, COLOR_DIVIDER);

    for _ in 0..80 {
        port.write_byte(b'-');
    }

    write_str(port, COLOR_RESET);
    write_str(port, CURSOR_INPUT_LINE);
}

/// # Summary
/// Renders the interactive input prompt and current buffer.
///
/// # Description
/// Locks the cursor to line 24, erases the line, prints the active user's name
/// (or Guest), and outputs the current contents of the line editor buffer.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_prompt(&state, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// Never.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`init_viewport`]
pub fn render_prompt(state: &AppState, port: &mut impl SerialPort) {
    write_str(port, CURSOR_INPUT_LINE);
    write_str(port, ERASE_LINE);
    write_str(port, COLOR_PROMPT);

    if let Some(user_id) = state.current_user_id {
        let mut found = false;
        for user in &state.users {
            if user.id != user_id || user.status != 0 {
                continue;
            }
            let Ok(name) = user.name.as_str() else {
                continue;
            };
            if name.is_empty() {
                continue;
            }
            write_str(port, name);
            found = true;
            break;
        }

        if !found {
            write_str(port, "User");
            write_u32(port, u32::from(user_id));
        }
    } else {
        write_str(port, "Guest");
    }

    write_str(port, "> ");
    write_str(port, COLOR_RESET);

    let Ok(input) = state.input_buffer.as_str() else {
        return;
    };
    write_str(port, input);
}

/// # Summary
/// Renders a single message into the scrolling history viewport.
///
/// # Description
/// Saves the cursor, moves to the bottom of the scrolling region (line 22),
/// prints the message with a newline to trigger a scroll, and restores the cursor.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_message(&state, &msg, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_prompt`]
pub fn render_message(state: &AppState, message: &Message, port: &mut impl SerialPort) {
    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);

    write_str(port, "[");
    write_u32(port, message.tick);
    write_str(port, "] <");

    let mut found = false;
    for user in &state.users {
        if user.id != message.user_id || user.status != 0 {
            continue;
        }
        let Ok(name) = user.name.as_str() else {
            continue;
        };
        if name.is_empty() {
            continue;
        }
        write_str(port, name);
        found = true;
        break;
    }

    if !found {
        write_str(port, "User");
        write_u32(port, u32::from(message.user_id));
    }

    write_str(port, "> ");

    if let Ok(content) = message.content.as_str() {
        write_str(port, content);
    }

    write_str(port, "\r\n");
    write_str(port, RESTORE_CURSOR);
}

/// # Summary
/// Renders a local system message to the viewport.
///
/// # Description
/// Used to provide feedback for commands (e.g., errors, help text).
/// System messages are displayed in yellow and are not saved to persistent history.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_system_message(&mut port, 1, "Command not found");
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_message`]
pub fn render_system_message(port: &mut impl SerialPort, tick: u32, msg: &str) {
    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);

    write_str(port, COLOR_SYSTEM);
    write_str(port, "[");
    write_u32(port, tick);
    write_str(port, "] <System> ");
    write_str(port, msg);
    write_str(port, COLOR_RESET);
    write_str(port, "\r\n");

    write_str(port, RESTORE_CURSOR);
}

/// # Summary
/// Renders an empty line in the scrolling viewport.
///
/// # Description
/// Used to provide visual separation between system messages and user messages.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_empty_line(&mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_system_message`]
pub fn render_empty_line(port: &mut impl SerialPort) {
    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);
    write_str(port, "\r\n");
    write_str(port, RESTORE_CURSOR);
}

/// # Summary
/// Renders the welcome banner on startup.
///
/// # Description
/// Prints a formatted ASCII banner introducing the application and its basic usage.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_welcome(&mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`init_viewport`]
pub fn render_welcome(port: &mut impl SerialPort) {
    render_system_message(
        port,
        0,
        "==================================================",
    );
    render_system_message(port, 0, " CLI Chat Embedded");
    render_system_message(port, 0, " Zero-heap, bare-metal compatible chat core.");
    render_system_message(port, 0, " Type /help to see available commands.");
    render_system_message(
        port,
        0,
        "==================================================",
    );
}

/// # Summary
/// Renders the list of active users.
///
/// # Description
/// Iterates through the user registry and prints the ID and name of each active user.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_active_users(&state, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_system_message`]
pub fn render_active_users(state: &AppState, port: &mut impl SerialPort) {
    render_system_message(port, state.relative_tick, "--- Active Users ---");
    let mut count = 0;
    for user in &state.users {
        if user.status != 0 {
            continue;
        }
        count += 1;
        write_str(port, SAVE_CURSOR);
        write_str(port, CURSOR_SCROLL_BOTTOM);

        write_str(port, COLOR_INFO);
        write_str(port, "[");
        write_u32(port, state.relative_tick);
        write_str(port, "] <Info> ID: ");
        write_u32(port, u32::from(user.id));
        write_str(port, " | Name: ");

        let Ok(name) = user.name.as_str() else {
            write_str(port, "Unnamed");
            write_str(port, COLOR_RESET);
            write_str(port, "\r\n");
            write_str(port, RESTORE_CURSOR);
            continue;
        };

        if name.is_empty() {
            write_str(port, "Unnamed");
        } else {
            write_str(port, name);
        }

        write_str(port, COLOR_RESET);
        write_str(port, "\r\n");
        write_str(port, RESTORE_CURSOR);
    }

    if count == 0 {
        render_system_message(port, state.relative_tick, "No users registered.");
    }
}

/// # Summary
/// Renders the system status information.
///
/// # Description
/// Prints current uptime ticks and memory usage of the history buffer.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_status(&state, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_system_message`]
#[allow(clippy::cast_possible_truncation)]
pub fn render_status(state: &AppState, port: &mut impl SerialPort) {
    render_system_message(port, state.relative_tick, "--- System Status ---");

    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);

    write_str(port, COLOR_INFO);
    write_str(port, "[");
    write_u32(port, state.relative_tick);
    write_str(port, "] <Info> Uptime Ticks: ");
    write_u32(port, state.relative_tick);
    write_str(port, COLOR_RESET);
    write_str(port, "\r\n");
    write_str(port, RESTORE_CURSOR);

    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);

    write_str(port, COLOR_INFO);
    write_str(port, "[");
    write_u32(port, state.relative_tick);
    write_str(port, "] <Info> Messages in History: ");
    write_u32(port, state.history.len() as u32);
    write_str(port, " / ");
    write_u32(port, crate::types::MAX_HISTORY as u32);
    write_str(port, COLOR_RESET);
    write_str(port, "\r\n");
    write_str(port, RESTORE_CURSOR);
}

/// # Summary
/// Renders the time and epoch information.
///
/// # Description
/// Prints the current relative tick and the host boot epoch.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_time_info(&state, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_system_message`]
pub fn render_time_info(state: &AppState, port: &mut impl SerialPort) {
    render_system_message(port, state.relative_tick, "--- Time Info ---");

    write_str(port, SAVE_CURSOR);
    write_str(port, CURSOR_SCROLL_BOTTOM);

    write_str(port, COLOR_INFO);
    write_str(port, "[");
    write_u32(port, state.relative_tick);
    write_str(port, "] <Info> Boot Epoch: ");
    write_u32(port, state.boot_epoch);
    write_str(port, COLOR_RESET);
    write_str(port, "\r\n");
    write_str(port, RESTORE_CURSOR);
}

/// # Summary
/// Renders the application about information.
///
/// # Description
/// Prints the author and license information.
///
/// # Examples
/// ```rust,ignore
/// // crate::state::render::render_about(&state, &mut port);
/// ```
///
/// # Panics
/// Never.
///
/// # Errors
/// None.
///
/// # Safety
/// Safe.
///
/// # See Also
/// [`render_system_message`]
pub fn render_about(state: &AppState, port: &mut impl SerialPort) {
    render_system_message(port, state.relative_tick, "CLI Chat Embedded");
    render_system_message(port, state.relative_tick, "Author: Dzulkifli Anwar");
    render_system_message(port, state.relative_tick, "License: MIT");
}