Hackshell is a lightweight, customizable shell framework built in Rust. It provides an interactive command-line interface that can be easily extended with custom commands and integrated into your applications.
- Task Management: Background task spawning, monitoring, and killing
- Environment Variables: Built-in environment variable storage and manipulation
- Rich Command Set: Comes with essential built-in commands like
help,set,get,env, etc. - Command History: Persistent command history between sessions
- Custom Context: Bring your own application context for deep integration
Hackshell comes with several built-in commands:
env- List all environment variablesget <name>- Get the value of an environment variableset <name> <value>- Set an environment variableunset <name>- Remove an environment variablehelp- Show available commands and their descriptionssleep <seconds>- Sleep for the specified durationexit- Exit the shelltask- Manage background tasks
You can find complete examples in the examples directory. The following are quick examples.
use hackshell::{Hackshell, error::HackshellError};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new shell with a custom context (in this case just ())
let shell = Hackshell::<()>::new((), "basic> ")?;
shell.set_history_file("history.txt")?;
// Enter the shell loop
loop {
match shell.run() {
Ok(_) => {}
Err(e) => {
if matches!(e, HackshellError::Eof)
|| matches!(e, HackshellError::Interrupted)
|| matches!(e, HackshellError::Exit)
{
break;
}
eprintln!("Error: {}", e);
}
}
}
Ok(())
}You can extend Hackshell with your own commands:
use hackshell::{Hackshell, Command, CommandResult};
struct MyCommand;
impl Command<()> for MyCommand {
fn commands(&self) -> &'static [&'static str] {
&["mycmd"]
}
fn help(&self) -> &'static str {
"mycmd - My custom command"
}
fn run(&self, shell: &mut Hackshell<()>, args: &[String]) -> CommandResult {
println!("My custom command was called with args: {:?}", &args[1..]);
Ok(())
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut shell = Hackshell::new((), "hackshell> ")?;
shell.set_history_file("something.txt")?;
// Add your custom command
shell.add_command(MyCommand {});
// Run shell loop
// ...
Ok(())
}struct AppState {
config: RwLock<HashMap<String, String>>,
client: Mutex<DatabaseClient>,
}
// Your custom commands can now access the AppState
struct ConfigCommand;
impl Command<AppState> for ConfigCommand {
// Implementation omitted for brevity
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_state = AppState {
config: RwLock::new(HashMap::new()),
client: Mutex::new(DatabaseClient::connect("localhost:5432")?),
};
let mut shell = Hackshell::new(app_state, "myapp> ")?;
// ...
}Hackshell allows you to spawn and manage background tasks:
// Spawn a background task
shell.spawn("my-task", move |run| {
for i in 0..10 {
println!("Background task: {}\r", i);
sleep(Duration::from_secs(1));
}
});
// List active tasks
let tasks = shell.get_tasks();
for task in tasks {
println!("Task: {}, running for {}s", task.name, task.duration.as_secs());
}
// Kill a task
shell.kill("my-task")?;Add Hackshell to your Cargo.toml:
[dependencies]
hackshell = "0.3.16"This project is licensed under the MIT License - see the LICENSE file for details.