fix: command-not-found uses stderr

This commit is contained in:
iff 2025-04-06 16:14:31 +02:00
parent a7a281b60e
commit 69a05ef574
3 changed files with 23 additions and 2 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- Not getting `command-not-found`'s output as it goes into `stderr`
## [0.7.0] - 2025-04-05
### Breaking

View file

@ -325,6 +325,21 @@ pub fn command_output(shell: &str, command: &str) -> String {
String::from_utf8_lossy(&output.stdout).to_string()
}
pub fn command_output_or_error(shell: &str, command: &str) -> String {
let output = std::process::Command::new(shell)
.arg("-c")
.arg(command)
.env("LC_ALL", "C")
.output()
.expect("failed to execute process");
if !output.stdout.is_empty() {
String::from_utf8_lossy(&output.stdout).to_string()
} else {
String::from_utf8_lossy(&output.stderr).to_string()
}
}
pub fn module_output(data: &Data, module: &str) -> Option<Vec<String>> {
let shell = &data.shell;
let executable = &data.split[0];

View file

@ -1,3 +1,4 @@
use crate::shell::command_output_or_error;
use crate::shell::{command_output, elevate, Data};
use colored::Colorize;
use std::io::stderr;
@ -152,7 +153,8 @@ pub fn get_packages(
}
_ => match package_manager.ends_with("command-not-found") {
true => {
let result = command_output(shell, &format!("{} {}", package_manager, executable));
let result =
command_output_or_error(shell, &format!("{} {}", package_manager, executable));
if result.is_empty() {
return None;
}
@ -167,7 +169,7 @@ pub fn get_packages(
None
}
false => {
eprintln!("{} Unsupported package manager", ":pay-respects".yellow());
eprintln!("{} Unsupported package manager", "pay-respects:".yellow());
None
}
},