在rust中执行命令行输出中文乱码解决办法

如果你使用标准的依赖库执行命令中包含中文的话, 就会发现中文乱码,如果你的输出中没有中文,就可以正常输出,因为windows的命令行默认使用的是gbk编码。。。。。

rust 复制代码
#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {
    #[cfg(target_os = "windows")]
    let output = {
        // 先设置控制台编码为UTF-8
        let _ = tokio::process::Command::new("powershell")
            .arg("-Command")
            .arg("chcp 65001 | Out-Null")
            .creation_flags(0x08000000)
            .status()
            .await;
        tokio::process::Command::new("powershell")
            .arg("-Command")
            .arg(&command)
            .creation_flags(0x08000000)
            .output()
            .await
            .map_err(|e| e.to_string())?
    };

    #[cfg(not(target_os = "windows"))]
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(&command)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    if output.status.success() {
        print!(
            "Command output: {}",
            String::from_utf8_lossy(&output.stdout)
        );
        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    } else {
        Err(String::from_utf8_lossy(&output.stderr).to_string())
    }
}

解决办法

使用标准的编码依赖库encoding_rs = "0.8"

完整代码:

rust 复制代码
#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {
    #[cfg(target_os = "windows")]
    let output = tokio::process::Command::new("powershell")
        .arg("-Command")
        .arg(&command)
        .creation_flags(0x08000000)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    #[cfg(not(target_os = "windows"))]
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(&command)
        .output()
        .await
        .map_err(|e| e.to_string())?;

    if output.status.success() {
        #[cfg(target_os = "windows")]
        {
            // 在Windows上尝试从GBK转换为UTF-8
            let (decoded, _, _) = GBK.decode(&output.stdout);
            Ok(decoded.into_owned())
        }
        #[cfg(not(target_os = "windows"))]
        {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        }
    } else {
        #[cfg(target_os = "windows")]
        {
            let (decoded, _, _) = GBK.decode(&output.stderr);
            Err(decoded.into_owned())
        }
        #[cfg(not(target_os = "windows"))]
        {
            Err(String::from_utf8_lossy(&output.stderr).to_string())
        }
    }
}
相关推荐
vvilkim5 分钟前
Flutter 常用组件详解:Text、Button、Image、ListView 和 GridView
前端·flutter
豆豆(设计前端)11 分钟前
在 JavaScript 中,你可以使用 Date 对象来获取 当前日期 和 当前时间、当前年份。
开发语言·javascript·ecmascript
vvilkim11 分钟前
Flutter 命名路由与参数传递完全指南
前端·flutter
NA12 分钟前
redis
前端
你真好看_12 分钟前
6年低代码 零代码 系统二开人员的角度,看低代码 到底有多好用!!!
前端
JC_You_Know20 分钟前
边缘计算一:现代前端架构演进图谱 —— 从 SPA 到边缘渲染
前端·人工智能·边缘计算
DoraBigHead22 分钟前
深入 JavaScript 作用域机制:透视 V8 引擎背后的执行秘密
前端·javascript
薛定谔的算法24 分钟前
JavaScript闭包深度解析:从基础概念到柯里化实践
javascript
Tu_Jipang24 分钟前
前端从零搭建企业级后台系统实战指南
前端
快起来别睡了25 分钟前
CSS定位的奥秘:从文档流到position,一文讲透前端布局核心!
前端·css·程序员