Crates.io | rustyline_filehelper |
lib.rs | rustyline_filehelper |
version | 0.1.0 |
source | src |
created_at | 2024-09-10 00:29:41.996587 |
updated_at | 2024-09-10 00:29:41.996587 |
description | A simple file helper using rustyline |
homepage | |
repository | https://www.github.com/burstMembrane/rustyline_filehelper |
max_upload_size | |
id | 1369911 |
size | 28,877 |
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.
FilenameCompleter
from rustyline
.~
) for the home directory and checks for valid files and directories.cd
, pwd
, ls
, etc.Add the following to your Cargo.toml
:
[dependencies]
rustyline = "9.0"
anyhow = "1.0"
Here is an example of how to use FileHelper
with rustyline
:
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(())
}
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.
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.
Custom highlighting of input lines is applied to directories, valid files, and invalid files:
Several common shell commands such as cd
, ls
, pwd
, and clear
are recognized by the validator. Paths entered after these commands are validated.
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.
The library uses anyhow::Result
to simplify error handling for setting up the readline editor.
This project is licensed under the MIT License. See the LICENSE file for details.