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

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

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

问题

以下是已翻译的内容:

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

  1. use std::io::{stdout, Write};
  2. use std::process::Command;
  3. use termion::raw::IntoRawMode;
  4. struct KeyboardShortcut {
  5. key: char,
  6. description: &'static str,
  7. command: &'static str,
  8. }
  9. impl KeyboardShortcut {
  10. fn execute_command(&self, stdout: &mut impl Write) {
  11. Command::new("sh").arg("-c").arg(self.command).status();
  12. }
  13. }
  14. fn main() {
  15. let mut stdout = stdout().into_raw_mode().unwrap();
  16. // 在原始模式下执行一些操作
  17. let keyboard_shortcuts = vec![KeyboardShortcut {
  18. key: 'f',
  19. description: "Feat: adds a new feature to the application",
  20. command: "ls --color=always",
  21. }];
  22. stdout.flush().unwrap();
  23. // 在实际应用中,我使用了 `match`。
  24. keyboard_shortcuts[0].execute_command(&mut stdout);
  25. }

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

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

英文:

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

  1. use std::io::{stdout, Write};
  2. use std::process::Command;
  3. use termion::raw::IntoRawMode;
  4. struct KeyboardShortcut {
  5. key: char,
  6. description: &'static str,
  7. command: &'static str,
  8. }
  9. impl KeyboardShortcut {
  10. fn execute_command(&self, stdout: &mut impl Write) {
  11. Command::new("sh").arg("-c").arg(self.command).status();
  12. }
  13. }
  14. fn main() {
  15. let mut stdout = stdout().into_raw_mode().unwrap();
  16. // Do something in raw mode
  17. let keyboard_shortcuts = vec![KeyboardShortcut {
  18. key: 'f',
  19. description: "Feat: adds a new feature to the application",
  20. command: "ls --color=always",
  21. }];
  22. stdout.flush().unwrap();
  23. // In the actual application, I'm using `match`.
  24. keyboard_shortcuts[0].execute_command(&mut stdout)
  25. }

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

  1. 替代在 `execute_command` 中用 `sh` 包装命令,改用 [`script`](https://man7.org/linux/man-pages/man1/script.1.html) 来包装,如下所示:
  2. ```rust
  3. fn execute_command(&self, stdout: &mut impl Write) {
  4. Command::new("script").arg("-qec").arg(self.command).arg("/dev/null").status();
  5. }

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

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

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

现在应该以彩色打印。

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. fn execute_command(&amp;self, stdout: &amp;mut impl Write) {
  5. Command::new(&quot;script&quot;).arg(&quot;-qec&quot;).arg(self.command).arg(&quot;/dev/null&quot;).status();
  6. }
  7. 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.
  8. 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`.
  9. let keyboard_shortcuts = vec![KeyboardShortcut {
  10. key: &#39;f&#39;,
  11. description: &quot;Feat: adds a new feature to the application&quot;,
  12. command: &quot;ls --color=auto&quot;,
  13. }];
  14. Which should now print in colour.
  15. </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:

确定