Shelgon 是一个强大的 Rust 框架,用于构建交互式 REPL (Read-Eval-Print Loop) 应用和自定义 shell。 它提供了一个灵活、类型安全的基础,并使用 ratatui 内置了终端 UI 功能。

Crates.io Documentation License: MIT

特性

安装

将 Shelgon 添加到你的 Cargo.toml

[dependencies]
shelgon = "0.1.0"
tokio = { version = "1.43.0", features = ["full"] }
anyhow = "1.0.95"

快速开始

创建一个简单的 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()
}

进化指南:构建你自己的 Shell

以下是如何使用 shelgon 构建一个像龙一样的 shell:

  1. 定义你的 Executor:创建一个实现 command::Execute 的类型
  2. 创建你的 Context:设计一个 context 类型来维护命令之间的状态
  3. 实现命令逻辑:在 execute 方法中添加你的命令执行逻辑
  4. 添加 Tab 补全:实现 completion 方法以获得智能建议
  5. 处理 STDIN:使用 prepare 方法来指示哪些命令需要输入

示例

查看 examples 目录,了解更高级的用法模式,包括:

贡献

欢迎贡献! 请随时提交 Pull Request。 在贡献之前,请:

  1. 检查现有的 issue 或创建一个新的 issue
  2. Fork 仓库
  3. 创建你的特性分支 (git checkout -b feature/amazing-feature)
  4. 提交你的更改 (git commit -m 'Add some amazing feature')
  5. 推送到分支 (git push origin feature/amazing-feature)
  6. 打开一个 Pull Request

许可协议

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。 Built by Human, Documented by LLM.