pub struct UIState {Show 15 fields
pub mode: UIMode,
pub last_log_mode: Option<UIMode>,
pub messages: Vec<ChatMessageEntry>,
pub chat_messages: Vec<(DateTime<Utc>, String)>,
pub logs: VecDeque<LogEntry>,
pub scroll_offset: usize,
pub log_scroll_offset: usize,
pub horizontal_scroll_offset: usize,
pub is_at_bottom_chat: bool,
pub is_at_bottom_log: bool,
pub input_buffer: String,
pub cursor_pos: usize,
pub terminal_size: (u16, u16),
pub max_log_entries: usize,
pub connected_peers_count: usize,
}Expand description
Represents the overall state of the user interface.
This struct holds all the data and configuration necessary to render the UI and respond to user interactions.
Fields§
§mode: UIModeThe current operational mode of the UI (Chat or Logs).
last_log_mode: Option<UIMode>Stores the last log mode for easy switching back.
messages: Vec<ChatMessageEntry>A vector of chat message entries for display.
chat_messages: Vec<(DateTime<Utc>, String)>A vector of generic chat messages, typically system messages or command output.
logs: VecDeque<LogEntry>A deque of log entries for the log view.
scroll_offset: usizeThe current vertical scroll offset for the chat view.
log_scroll_offset: usizeThe current vertical scroll offset for the log view.
horizontal_scroll_offset: usizeThe current horizontal scroll offset for text.
is_at_bottom_chat: boolIndicates if the chat view is scrolled to the bottom.
is_at_bottom_log: boolIndicates if the log view is scrolled to the bottom.
input_buffer: StringThe current content of the input buffer.
cursor_pos: usizeThe current cursor position within the input buffer.
terminal_size: (u16, u16)The current size of the terminal (width, height).
max_log_entries: usizeThe maximum number of log entries to retain.
connected_peers_count: usizeThe count of currently connected peers.
Implementations§
Source§impl UIState
impl UIState
Sourcepub fn add_message(&mut self, message: Message)
pub fn add_message(&mut self, message: Message)
Adds a new Message to the UI state.
This function stores the message along with its reception timestamp. If the UI is in chat mode and at the bottom of the scroll, it resets the scroll offset. Otherwise, it adjusts the scroll offset to keep new messages visible if not explicitly scrolled up.
§Arguments
message- TheMessageto add.
Sourcepub fn add_chat_message(&mut self, message: String)
pub fn add_chat_message(&mut self, message: String)
Adds a generic chat message string to the UI state.
This is typically used for system messages or user input echoes.
It handles scroll adjustment similarly to add_message.
§Arguments
message- The string content of the chat message.
Sourcepub fn add_history_output(&mut self, message: String)
pub fn add_history_output(&mut self, message: String)
Adds a block of history output to the UI state.
This is used for displaying multi-line output from commands like history.
Each line is prefixed with a special marker for rendering purposes.
§Arguments
message- The string content of the history output.
Sourcepub fn update_chat_scroll_state(&mut self, terminal_height: usize)
pub fn update_chat_scroll_state(&mut self, terminal_height: usize)
Updates the chat scroll state based on the current terminal height.
This ensures that the scroll_offset remains within valid bounds and
updates is_at_bottom_chat.
§Arguments
terminal_height- The current height of the terminal in lines.
Sourcepub fn calculate_total_chat_items(&self) -> usize
pub fn calculate_total_chat_items(&self) -> usize
Calculates the total number of displayable items in the chat view.
This includes both actual messages and generic chat messages.
§Returns
The total count of items that can be displayed.
Sourcepub fn jump_to_bottom_chat(&mut self)
pub fn jump_to_bottom_chat(&mut self)
Scrolls the chat view to the bottom.
Source§impl UIState
impl UIState
Sourcepub fn safe_insert_char(&mut self, c: char)
pub fn safe_insert_char(&mut self, c: char)
Inserts a character safely into the input buffer at the current cursor position.
Handles multi-byte characters correctly.
§Arguments
c- The character to insert.
Sourcepub fn safe_remove_char_before(&mut self) -> bool
pub fn safe_remove_char_before(&mut self) -> bool
Removes the character before the current cursor position safely.
Handles multi-byte characters correctly.
§Returns
true if a character was removed, false otherwise.
Sourcepub fn safe_remove_char_at(&mut self) -> bool
pub fn safe_remove_char_at(&mut self) -> bool
Removes the character at the current cursor position safely.
Handles multi-byte characters correctly.
§Returns
true if a character was removed, false otherwise.
Sourcepub fn safe_cursor_left(&mut self)
pub fn safe_cursor_left(&mut self)
Moves the cursor one position to the left.
Sourcepub fn safe_cursor_right(&mut self)
pub fn safe_cursor_right(&mut self)
Moves the cursor one position to the right.
Sourcepub fn safe_cursor_home(&mut self)
pub fn safe_cursor_home(&mut self)
Moves the cursor to the beginning of the input buffer.
Sourcepub fn safe_cursor_end(&mut self)
pub fn safe_cursor_end(&mut self)
Moves the cursor to the end of the input buffer.
Source§impl UIState
impl UIState
Sourcepub fn add_log_batch(&mut self, entries: Vec<LogEntry>)
pub fn add_log_batch(&mut self, entries: Vec<LogEntry>)
Adds a batch of new log entries to the UI state.
This function appends new log entries to the buffer, enforcing max_log_entries.
If the UI is in log mode and at the bottom of the scroll, it resets the
scroll offset. Otherwise, it adjusts the scroll offset to keep new
messages visible if not explicitly scrolled up.
§Arguments
entries- AVecofLogEntryto add.
Sourcepub fn refresh_logs(&mut self)
pub fn refresh_logs(&mut self)
Triggers a refresh of the log display.
This function resets the log scroll offset and marks the view as being at the bottom, useful when filter settings change.
Sourcepub fn update_log_scroll_state(&mut self, terminal_height: usize)
pub fn update_log_scroll_state(&mut self, terminal_height: usize)
Updates the log scroll state based on the current terminal height.
This ensures that the log_scroll_offset remains within valid bounds and
updates is_at_bottom_log.
§Arguments
terminal_height- The current height of the terminal in lines.
Sourcepub fn jump_to_bottom_log(&mut self)
pub fn jump_to_bottom_log(&mut self)
Scrolls the log view to the bottom.
Sourcepub fn filtered_logs(&self) -> Vec<&LogEntry>
pub fn filtered_logs(&self) -> Vec<&LogEntry>
Returns a vector of log entries filtered by the current UIMode::Logs settings.
Logs can be filtered by minimum Level and by a text filter string,
which can include exclusions prefixed with -.
Source§impl UIState
impl UIState
Sourcepub fn toggle_mode(&mut self)
pub fn toggle_mode(&mut self)
Toggles the UI mode between chat and logs.
When switching to log mode for the first time or from chat mode,
it initializes log mode settings if not already defined.
Resets scroll offsets and is_at_bottom flags upon mode change.
Sourcepub fn replace_messages(&mut self, messages: Vec<Message>)
pub fn replace_messages(&mut self, messages: Vec<Message>)
Replaces the current list of displayed messages with a new set.
Resets scroll offset to the bottom after replacing messages.
§Arguments
messages- AVecofMessages to display.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for UIState
impl RefUnwindSafe for UIState
impl Send for UIState
impl Sync for UIState
impl Unpin for UIState
impl UnwindSafe for UIState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more