在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())
        }
    }
}
相关推荐
超人不会飛几秒前
就着HTTP聊聊SSE的前世今生
前端·javascript·http
蓝胖子的多啦A梦3 分钟前
Vue+element 日期时间组件选择器精确到分钟,禁止选秒的配置
前端·javascript·vue.js·elementui·时间选选择器·样式修改
夏天想6 分钟前
vue2+elementui使用compressorjs压缩上传的图片
前端·javascript·elementui
The_cute_cat7 分钟前
JavaScript的初步学习
开发语言·javascript·学习
海天胜景10 分钟前
vue3 el-table 列增加 自定义排序逻辑
javascript·vue.js·elementui
今晚打老虎z14 分钟前
dotnet-env: .NET 开发者的环境变量加载工具
前端·chrome·.net
用户38022585982420 分钟前
vue3源码解析:diff算法之patchChildren函数分析
前端·vue.js
烛阴25 分钟前
XPath 进阶:掌握高级选择器与路径表达式
前端·javascript
小鱼小鱼干28 分钟前
【JS/Vue3】关于Vue引用透传
前端
JavaDog程序狗30 分钟前
【前端】HTML+JS 实现超燃小球分裂全过程
前端