2 releases
new 0.2.1 | Feb 14, 2025 |
---|---|
0.2.0 | Feb 13, 2025 |
#200 in Command-line interface
270 downloads per month
38KB
402 lines
Shelgon 
Shelgon is a robust Rust framework for building interactive REPL (Read-Eval-Print Loop) applications and custom shells. It provides a flexible, type-safe foundation with built-in terminal UI capabilities using ratatui
.
Features
- 🛡️ Type-safe Command Execution - Like Shelgon's protective shell, your commands are wrapped in a type-safe interface
- 🔄 Async Runtime Integration - Built on tokio for high-performance async operations
- 🎨 Beautiful TUI - Powered by ratatui with support for styling and colors
- ⌨️ Rich Input Handling - Complete keyboard interaction support including:
- Command history
- Cursor movement
- Tab completion
- Ctrl+C/Ctrl+D handling
- 📝 Custom Context Support - Maintain state between commands with your own context type
- 📥 STDIN Support - Handle multi-line input for commands that need it
Installation
Add Shelgon to your Cargo.toml
:
[dependencies]
shelgon = "0.1.0"
tokio = { version = "1.43.0", features = ["full"] }
anyhow = "1.0.95"
Quick Start
Create a simple echo shell:
use shelgon::{command, renderer};
struct EchoExecutor {}
impl command::New for EchoExecutor {
fn new() -> anyhow::Result<(Self, ())> {
Ok((Self {}, ()))
}
}
impl command::Execute for EchoExecutor {
type Context = ();
fn prompt(&self, _: &Self::Context) -> String {
"$".to_string()
}
fn execute(
&self,
_: &mut Self::Context,
cmd: command::CommandInput,
) -> anyhow::Result<command::OutputAction> {
Ok(command::OutputAction::Command(command::CommandOutput {
prompt: cmd.prompt,
command: cmd.command.clone(),
stdin: cmd.stdin.unwrap_or_default(),
stdout: vec![cmd.command],
stderr: Vec::new(),
}))
}
}
fn main() -> anyhow::Result<()> {
let rt = tokio::runtime::Runtime::new()?;
let app = renderer::App::<EchoExecutor>::new(rt)?;
app.execute()
}
Evolution Guide: Building Your Own Shell 
Here's how to build a dragon-like shell with shelgon
:
- Define Your Executor: Create a type that implements
command::Execute
- Create Your Context: Design a context type to maintain state between commands
- Implement Command Logic: Add your command execution logic in the
execute
method - Add Tab Completion: Implement the
completion
method for smart suggestions - Handle STDIN: Use the
prepare
method to indicate which commands need input
Examples
Check out the examples directory for more advanced usage patterns, including:
echosh.rs
: A basic echo shell demonstrating core functionality
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Before contributing, please:
- Check existing issues or create a new one
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Built with 💙 by Nishant Joshi
Dependencies
~9–19MB
~266K SLoC