Skip to content

Fkernel653/ratin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ratin β€” Custom input field for Ratatui TUI

Rust Crates.io Docs.rs License Platform Rustfmt

Modular input field component for Ratatui terminal applications with character validation, cursor management, and customizable styling.

πŸš€ Quick Start

[dependencies]
ratin = "0.1.0"
use ratin::{InputField, CharValidator};

let validator = CharValidator::new()
    .allow_url_chars()
    .with_max_length(200);

let mut input = InputField::new()
    .with_validator(validator)
    .with_placeholder("Enter URL...")
    .with_max_length(100);

πŸ“‹ Features

Input Field

Feature Description
new() Create new input field
with_placeholder() Set placeholder text
with_block() Customize block widget
with_max_length() Set maximum input length
with_validator() Set character validator
get_text() Get current input text
clear() Clear input field
set_processing() Set processing state (locks input)
set_focused() Set focus state
set_error() Set error message

Character Validator

Method Description
new() Create new validator
with_allowed_chars() Set allowed characters (vector)
allow_letters() Allow only letters A-Z, a-z
allow_digits() Allow only digits 0-9
allow_alphanumeric() Allow letters and digits
allow_url_chars() Allow URL-safe characters
allow_email_chars() Allow email-safe characters
with_case_sensitive() Toggle case sensitivity

Styling

Method Description
new() Create new style
with_focused() Set focused border color
with_error() Set error border color
with_processing() Set processing border color
with_text_color() Set text color
with_placeholder_color() Set placeholder color

πŸ”§ Usage Examples

Basic Input

use ratin::InputField;

let mut input = InputField::new()
    .with_placeholder("Enter your name...")
    .with_max_length(50);

URL Input

use ratin::{InputField, CharValidator};

let validator = CharValidator::new()
    .allow_url_chars()
    .with_case_sensitive(false);

let mut input = InputField::new()
    .with_validator(validator)
    .with_placeholder("https://example.com...")
    .with_max_length(200);

Numeric Input

use ratin::{InputField, CharValidator};

let validator = CharValidator::new()
    .allow_digits()
    .with_max_length(10);

let mut input = InputField::new()
    .with_validator(validator)
    .with_placeholder("Enter number...")
    .with_max_length(10);

Custom Character Set

use ratin::{InputField, CharValidator};

let validator = CharValidator::new()
    .with_allowed_chars(vec!['a', 'b', 'c', '1', '2', '3']);

let mut input = InputField::new()
    .with_validator(validator)
    .with_placeholder("Enter a/b/c or 1/2/3...");

Custom Styling

use ratin::{InputField, InputStyle};
use ratatui::style::Color;

let style = InputStyle::new()
    .with_focused(Color::Green)
    .with_error(Color::Red)
    .with_processing(Color::Yellow)
    .with_text_color(Color::White)
    .with_placeholder_color(Color::DarkGray);

let mut input = InputField::new()
    .with_style(style)
    .with_placeholder("Styled input...");

Event Handling

use crossterm::event::{KeyCode};

loop {
    if let Event::Key(key) = event::read()? {
        if key.code == KeyCode::Enter {
            if let Err(e) = input.validate() {
                input.set_error(Some(e.to_string()));
            } else {
                let text = input.get_text();
                input.set_processing(true);
                // Process input...
            }
        } else if key.code == KeyCode::Esc {
            break;
        } else {
            input.handle_key(key.code, key.modifiers);
        }
    }
    
    terminal.draw(|f| {
        input.render(f, area);
    })?;
}

πŸ“¦ Installation

From crates.io

cargo add ratin

From GitHub

[dependencies]
ratin = { git = "https://github.com/Fkernel653/ratin" }

🧩 Module Structure

ratin/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lib.rs          # Public API
β”‚   β”œβ”€β”€ input.rs        # Main InputField logic
β”‚   β”œβ”€β”€ style.rs        # Styling and themes
β”‚   β”œβ”€β”€ validator.rs    # Character validation
β”‚   └── error.rs        # Error types

πŸ”Œ Requirements

Dependency Version Purpose
ratatui 0.24+ TUI framework
crossterm 0.27+ Terminal events

🎯 Key Features

  • Character Validation β€” Restrict input to specific characters
  • Cursor Management β€” Full cursor control with arrow keys
  • Placeholder Support β€” Show hints when empty
  • Processing State β€” Lock input during async operations
  • Error Handling β€” Visual error feedback
  • Max Length β€” Prevent overflow
  • Case Sensitivity β€” Optional case-sensitive validation
  • Clean API β€” Builder pattern for configuration

πŸ“„ License

MIT License β€” See License

Author: Fkernel653

Project: GitHub β€’ Crates.io β€’ Docs.rs

About

Custom input field component for Ratatui TUI applications

Resources

License

Stars

Watchers

Forks

Contributors

Languages