将命令执行,就像它们直接在终端中执行一样。

huangapple go评论67阅读模式
英文:

Execute commands as if they're being executed directly in the terminal

问题

以下是已翻译的内容:

"这段代码定义了自定义终端命令(KeyboardShortcut)并执行它们:

use std::io::{stdout, Write};
use std::process::Command;
use termion::raw::IntoRawMode;

struct KeyboardShortcut {
    key: char,
    description: &'static str,
    command: &'static str,
}

impl KeyboardShortcut {
    fn execute_command(&self, stdout: &mut impl Write) {
        Command::new("sh").arg("-c").arg(self.command).status();
    }
}

fn main() {
    let mut stdout = stdout().into_raw_mode().unwrap();

    // 在原始模式下执行一些操作

    let keyboard_shortcuts = vec![KeyboardShortcut {
        key: 'f',
        description: "Feat: adds a new feature to the application",
        command: "ls --color=always",
    }];

    stdout.flush().unwrap();

    // 在实际应用中,我使用了 `match`。
    keyboard_shortcuts[0].execute_command(&mut stdout);
}

请注意,我不得不向命令添加 --color=always。这是因为如果命令不是直接在终端中执行(在这种情况下,它们是从Rust文件中执行的),它们将不会显示颜色。

有没有办法告诉 Command 以类似于在终端中直接执行命令的方式来执行命令?这样,我就不必每次执行命令时都启用颜色(使用标志)了吗?"

英文:

The following code defines custom terminal commands (KeyboardShortcut) and executes them:

use std::io::{stdout, Write};
use std::process::Command;
use termion::raw::IntoRawMode;

struct KeyboardShortcut {
    key: char,
    description: &'static str,
    command: &'static str,
}

impl KeyboardShortcut {
    fn execute_command(&self, stdout: &mut impl Write) {
        Command::new("sh").arg("-c").arg(self.command).status();
    }
}

fn main() {
    let mut stdout = stdout().into_raw_mode().unwrap();

    // Do something in raw mode

    let keyboard_shortcuts = vec![KeyboardShortcut {
        key: 'f',
        description: "Feat: adds a new feature to the application",
        command: "ls --color=always",
    }];

    stdout.flush().unwrap();

    // In the actual application, I'm using `match`.
    keyboard_shortcuts[0].execute_command(&mut stdout)
}

Notice that I had to add --color=always to the command. This is because commands won't display colors if they are not executed directly in the terminal (in this case, they are being executed from a Rust file).

Is there a way to tell Command to execute commands as if they were being executed directly in the terminal? So that I don't have enable colors (with flags) every time I want to execute a command?

Rust Playground

答案1

得分: 3

替代在 `execute_command` 中用 `sh` 包装命令,改用 [`script`](https://man7.org/linux/man-pages/man1/script.1.html) 来包装,如下所示:

```rust
fn execute_command(&self, stdout: &mut impl Write) {
    Command::new("script").arg("-qec").arg(self.command).arg("/dev/null").status();
}

附加的 .arg("/dev/null") 阻止 script 记录所有控制台输出,这实际上是其预期用途。

请注意,默认情况下 ls 不会以彩色打印 - 如果对您来说是彩色的,那是由于 shell 别名。不过,我们可以通过执行 ls --color=auto 来证明这个更改已经生效。

let keyboard_shortcuts = vec![KeyboardShortcut {
    key: 'f',
    description: "Feat: adds a new feature to the application",
    command: "ls --color=auto",
}];

现在应该以彩色打印。


<details>
<summary>英文:</summary>

Instead of wrapping the command with `sh` in `execute_command`, wrap it in [`script`](https://man7.org/linux/man-pages/man1/script.1.html) from `util-linux`, like so:

    fn execute_command(&amp;self, stdout: &amp;mut impl Write) {
        Command::new(&quot;script&quot;).arg(&quot;-qec&quot;).arg(self.command).arg(&quot;/dev/null&quot;).status();
    }

The additional `.arg(&quot;/dev/null&quot;)` stops `script` from writing a log of all console output, which is what its intended purpose actually is.

Note however that ls doesn&#39;t print in colour by default - if it does for you, it&#39;s because of a shell alias. However we can prove this change worked by executing `ls --color=auto`.

    let keyboard_shortcuts = vec![KeyboardShortcut {
        key: &#39;f&#39;,
        description: &quot;Feat: adds a new feature to the application&quot;,
        command: &quot;ls --color=auto&quot;,
    }];

Which should now print in colour.

</details>



huangapple
  • 本文由 发表于 2023年3月10日 00:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定