# FileHelper for Rustyline A Rust library that extends `rustyline`, a readline-like library in Rust, by providing a `FileHelper` for filename completion, file extension validation, and path-based validation with optional highlighting. ## Features - **Filename Completion**: Automatically complete file and directory names in the current context using `FilenameCompleter` from `rustyline`. - **Allowed Extensions**: You can specify a list of allowed file extensions. If no extensions are provided, all files are considered valid. - **Highlighting**: Provides custom highlighting of file paths, directories, and invalid paths. - **Directories**: Highlighted in blue. - **Valid Files**: Highlighted in green. - **Invalid Files**: Highlighted in red. - **Path Validation**: Expands tilde (`~`) for the home directory and checks for valid files and directories. - **Command Support**: Built-in validation for basic shell commands such as `cd`, `pwd`, `ls`, etc. ## Usage ### Installation Add the following to your `Cargo.toml`: ```toml [dependencies] rustyline = "9.0" anyhow = "1.0" ``` ### Example Here is an example of how to use `FileHelper` with `rustyline`: ```rust use anyhow::Result; use rustyline::{Editor}; use my_file_helper::setup_editor; fn main() -> Result<()> { // Specify allowed file extensions (optional) let allowed_extensions = Some(vec!["txt".to_string(), "rs".to_string()]); // Setup editor with file completion and validation let mut rl = setup_editor(allowed_extensions)?; loop { let readline = rl.readline("> "); match readline { Ok(line) => { if line == "exit" { break; } println!("Line: {}", line); }, Err(err) => { eprintln!("Error: {:?}", err); break; } } } Ok(()) } ``` ### Features Breakdown #### Filename Completion `FileHelper` uses `rustyline::completion::FilenameCompleter` to provide filename autocompletion based on the user's input. This helps in command-line applications where users need to select files. #### Allowed Extensions `FileHelper` supports an optional vector of allowed file extensions. You can define specific extensions or use wildcards (`*`). If an extension is not listed, the file will be considered invalid during validation. #### Highlighting Custom highlighting of input lines is applied to directories, valid files, and invalid files: - **Directories** are shown in **blue**. - **Valid Files** (according to allowed extensions) are shown in **green**. - **Invalid Files** (with disallowed extensions) are shown in **red**. #### Command Support Several common shell commands such as `cd`, `ls`, `pwd`, and `clear` are recognized by the validator. Paths entered after these commands are validated. ### Path Validation The validator expands `~` to the user's home directory and checks whether the input corresponds to an existing file or directory. If a file does not exist or does not match allowed extensions, a helpful error message is shown. ### Error Handling The library uses `anyhow::Result` to simplify error handling for setting up the readline editor. ## License This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.