refactor: add modal/validation utils and partial App encapsulation
Quick wins refactoring (Variant 1): - Created src/utils/modal_handler.rs (120+ lines) - 4 functions for modal handling (close, confirm, yes/no) - ModalAction enum for type-safe processing - English and Russian keyboard layout support - 4 unit tests - Created src/utils/validation.rs (180+ lines) - 7 validation functions (empty, length, IDs, etc) - Covers all common validation patterns - 7 unit tests - Partial App encapsulation: - Made config field private (readonly via app.config()) - Added 30+ getter/setter methods - Updated ui/messages.rs to use config() - Updated documentation: - REFACTORING_OPPORTUNITIES.md: #1 Complete, #5 Partial - CONTEXT.md: Added quick wins section Tests: 563 passed, 0 failed Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
184
src/utils/modal_handler.rs
Normal file
184
src/utils/modal_handler.rs
Normal file
@@ -0,0 +1,184 @@
|
||||
//! Modal dialog utilities
|
||||
//!
|
||||
//! Переиспользуемая логика для обработки модальных окон (диалогов).
|
||||
|
||||
use crossterm::event::KeyCode;
|
||||
|
||||
/// Результат обработки клавиши в модальном окне.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ModalAction {
|
||||
/// Закрыть модалку (Escape была нажата)
|
||||
Close,
|
||||
/// Подтвердить действие (Enter была нажата)
|
||||
Confirm,
|
||||
/// Продолжить обработку ввода (другая клавиша)
|
||||
Continue,
|
||||
}
|
||||
|
||||
/// Обрабатывает стандартные клавиши для модальных окон.
|
||||
///
|
||||
/// Проверяет клавиши Escape (закрыть) и Enter (подтвердить).
|
||||
/// Если нажата другая клавиша, возвращает `Continue`.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `key_code` - код нажатой клавиши
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `ModalAction::Close` - если нажата Escape
|
||||
/// * `ModalAction::Confirm` - если нажата Enter
|
||||
/// * `ModalAction::Continue` - для других клавиш
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossterm::event::KeyCode;
|
||||
/// use tele_tui::utils::modal_handler::{handle_modal_key, ModalAction};
|
||||
///
|
||||
/// assert_eq!(handle_modal_key(KeyCode::Esc), ModalAction::Close);
|
||||
/// assert_eq!(handle_modal_key(KeyCode::Enter), ModalAction::Confirm);
|
||||
/// assert_eq!(handle_modal_key(KeyCode::Char('a')), ModalAction::Continue);
|
||||
/// ```
|
||||
pub fn handle_modal_key(key_code: KeyCode) -> ModalAction {
|
||||
match key_code {
|
||||
KeyCode::Esc => ModalAction::Close,
|
||||
KeyCode::Enter => ModalAction::Confirm,
|
||||
_ => ModalAction::Continue,
|
||||
}
|
||||
}
|
||||
|
||||
/// Проверяет, нужно ли закрыть модалку (нажата Escape).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossterm::event::KeyCode;
|
||||
/// use tele_tui::utils::modal_handler::should_close_modal;
|
||||
///
|
||||
/// assert!(should_close_modal(KeyCode::Esc));
|
||||
/// assert!(!should_close_modal(KeyCode::Enter));
|
||||
/// assert!(!should_close_modal(KeyCode::Char('q')));
|
||||
/// ```
|
||||
pub fn should_close_modal(key_code: KeyCode) -> bool {
|
||||
matches!(key_code, KeyCode::Esc)
|
||||
}
|
||||
|
||||
/// Проверяет, нужно ли подтвердить действие в модалке (нажата Enter).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossterm::event::KeyCode;
|
||||
/// use tele_tui::utils::modal_handler::should_confirm_modal;
|
||||
///
|
||||
/// assert!(should_confirm_modal(KeyCode::Enter));
|
||||
/// assert!(!should_confirm_modal(KeyCode::Esc));
|
||||
/// assert!(!should_confirm_modal(KeyCode::Char('y')));
|
||||
/// ```
|
||||
pub fn should_confirm_modal(key_code: KeyCode) -> bool {
|
||||
matches!(key_code, KeyCode::Enter)
|
||||
}
|
||||
|
||||
/// Обрабатывает клавиши для подтверждения Yes/No.
|
||||
///
|
||||
/// Поддерживает:
|
||||
/// - `y` / `Y` / `д` / `Д` - да (confirm)
|
||||
/// - `n` / `N` / `т` / `Т` - нет (close)
|
||||
/// - `Enter` - подтвердить (confirm)
|
||||
/// - `Esc` - отменить (close)
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `key_code` - код нажатой клавиши
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Some(true)` - подтверждение (yes/Enter)
|
||||
/// * `Some(false)` - отмена (no/Escape)
|
||||
/// * `None` - другая клавиша (продолжить ввод)
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use crossterm::event::KeyCode;
|
||||
/// use tele_tui::utils::modal_handler::handle_yes_no;
|
||||
///
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('y')), Some(true));
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('Y')), Some(true));
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('д')), Some(true)); // русская 'y'
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Enter), Some(true));
|
||||
///
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('n')), Some(false));
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('т')), Some(false)); // русская 'n'
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Esc), Some(false));
|
||||
///
|
||||
/// assert_eq!(handle_yes_no(KeyCode::Char('a')), None);
|
||||
/// ```
|
||||
pub fn handle_yes_no(key_code: KeyCode) -> Option<bool> {
|
||||
match key_code {
|
||||
// Yes - подтверждение (английская и русская раскладка)
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Char('д') | KeyCode::Char('Д') => {
|
||||
Some(true)
|
||||
}
|
||||
KeyCode::Enter => Some(true),
|
||||
|
||||
// No - отмена (английская и русская раскладка)
|
||||
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Char('т') | KeyCode::Char('Т') => {
|
||||
Some(false)
|
||||
}
|
||||
KeyCode::Esc => Some(false),
|
||||
|
||||
// Другие клавиши - продолжить
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_handle_modal_key() {
|
||||
assert_eq!(handle_modal_key(KeyCode::Esc), ModalAction::Close);
|
||||
assert_eq!(handle_modal_key(KeyCode::Enter), ModalAction::Confirm);
|
||||
assert_eq!(handle_modal_key(KeyCode::Char('a')), ModalAction::Continue);
|
||||
assert_eq!(handle_modal_key(KeyCode::Up), ModalAction::Continue);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_close_modal() {
|
||||
assert!(should_close_modal(KeyCode::Esc));
|
||||
assert!(!should_close_modal(KeyCode::Enter));
|
||||
assert!(!should_close_modal(KeyCode::Char('q')));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_should_confirm_modal() {
|
||||
assert!(should_confirm_modal(KeyCode::Enter));
|
||||
assert!(!should_confirm_modal(KeyCode::Esc));
|
||||
assert!(!should_confirm_modal(KeyCode::Char('y')));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_handle_yes_no() {
|
||||
// Yes variants
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('y')), Some(true));
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('Y')), Some(true));
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('д')), Some(true)); // Russian
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('Д')), Some(true)); // Russian
|
||||
assert_eq!(handle_yes_no(KeyCode::Enter), Some(true));
|
||||
|
||||
// No variants
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('n')), Some(false));
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('N')), Some(false));
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('т')), Some(false)); // Russian
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('Т')), Some(false)); // Russian
|
||||
assert_eq!(handle_yes_no(KeyCode::Esc), Some(false));
|
||||
|
||||
// Other keys
|
||||
assert_eq!(handle_yes_no(KeyCode::Char('a')), None);
|
||||
assert_eq!(handle_yes_no(KeyCode::Up), None);
|
||||
assert_eq!(handle_yes_no(KeyCode::Char(' ')), None);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user