You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Rust中输出字符串至终端提示符后,模拟用户输入效果?

在Rust中向终端提示符后写入命令的实现方案

要实现让Rust程序将命令字符串输出到当前shell的提示符之后,模拟用户手动输入的效果,核心是将内容写入终端的输入缓冲区而非普通标准输出,以下是两种可行方案:

方案一:跨平台使用rustyline库

rustyline是Rust的readline兼容库,可模拟shell提示符并向输入缓冲区插入内容。

  1. 添加依赖到Cargo.toml:
[dependencies]
rustyline = "12.0"
  1. 示例代码:
use rustyline::{DefaultEditor, Result};

fn main() -> Result<()> {
    let mut editor = DefaultEditor::new()?;
    // 要插入的目标命令
    let target_cmd = "echo hi";
    
    // 将命令写入输入缓冲区
    editor.insert_str(target_cmd)?;
    // 显示提示符并等待用户操作
    let _ = editor.readline("$ ")?;
    
    Ok(())
}

说明:此方案会生成程序自身的提示符,若需直接对接系统shell,建议使用下方Linux专属方案。

方案二:Linux下直接操作终端输入队列(推荐)

Linux系统提供TIOCSTI ioctl命令,可直接将字符写入终端的输入队列,系统shell会将其识别为用户输入。

  1. 添加依赖到Cargo.toml:
[dependencies]
nix = "0.27"
  1. 示例代码:
use nix::fcntl::{open, O_RDWR};
use nix::sys::ioctl::{ioctl, TIOCSTI};
use nix::unistd::close;
use std::ffi::CString;
use std::io;

fn main() -> io::Result<()> {
    // 打开当前控制终端
    let tty_fd = open("/dev/tty", O_RDWR)?;
    // 要写入的命令,若末尾不加\n,用户需按回车执行;加\n则自动执行
    let target_cmd = "echo hi";

    // 逐个字符写入终端输入队列
    for c in target_cmd.chars() {
        let c_buf = CString::new(c.to_string())?;
        unsafe {
            ioctl(tty_fd, TIOCSTI, c_buf.as_ptr() as *mut libc::c_void)
                .map_err(|e| io::Error::from_raw_os_error(e as i32))?;
        }
    }

    close(tty_fd)?;
    Ok(())
}

说明:该方案直接对接系统shell,运行后目标命令会出现在原shell提示符后,用户按下回车即可执行。macOS也支持TIOCSTI,Windows平台可改用WriteConsoleInput API实现类似逻辑。

内容的提问来源于stack exchange,提问作者Andy Longwill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 03:19:54