Skip to content

Commit 7f9854a

Browse files
committed
Updated windows module
1 parent 89e6132 commit 7f9854a

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/executor.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,41 @@ pub struct CommandOutput {
1212
pub async fn execute_command(command: &str) -> Result<CommandOutput> {
1313
let shell_type = ShellType::detect();
1414
let (shell, args) = shell_type.get_shell_command();
15-
let formatted_command = shell_type.format_command(command);
15+
16+
let formatted_command = match shell_type {
17+
ShellType::PowerShell => {
18+
// Wrap PowerShell commands with proper formatting
19+
format!(
20+
"$OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8; \
21+
$FormatEnumerationLimit = -1; \
22+
$result = {}; \
23+
if ($result -is [System.Array]) {{ \
24+
$result | Format-Table -AutoSize -Wrap | Out-String -Width 120 \
25+
}} elseif ($null -ne $result) {{ \
26+
$result | Format-Table -AutoSize -Wrap | Out-String -Width 120 \
27+
}} else {{ \
28+
\"No output\" \
29+
}}",
30+
command
31+
)
32+
},
33+
_ => shell_type.format_command(command)
34+
};
1635

1736
let mut cmd = Command::new(shell);
1837
cmd.args(args).arg(&formatted_command);
1938

2039
let output = cmd.output()?;
2140

22-
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
23-
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
41+
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
42+
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
43+
44+
// Clean up the output by removing excessive newlines and whitespace
45+
let stdout = stdout
46+
.lines()
47+
.filter(|line| !line.trim().is_empty())
48+
.collect::<Vec<_>>()
49+
.join("\n");
2450

2551
// Note: PowerShell and CMD might write to stderr even on success
2652
let success = match shell_type {
@@ -29,8 +55,8 @@ pub async fn execute_command(command: &str) -> Result<CommandOutput> {
2955
};
3056

3157
Ok(CommandOutput {
32-
stdout,
33-
stderr,
58+
stdout: stdout.trim().to_string(),
59+
stderr: stderr.trim().to_string(),
3460
success
3561
})
3662
}

src/shell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl ShellType {
2828
pub fn get_shell_command(&self) -> (&str, &[&str]) {
2929
match self {
3030
ShellType::Bash => ("sh", &["-c"]),
31-
ShellType::PowerShell => ("powershell", &["-NoProfile", "-Command"]),
31+
ShellType::PowerShell => ("powershell", &["-NoProfile", "-NonInteractive", "-Command"]),
3232
ShellType::Cmd => ("cmd", &["/C"]),
3333
}
3434
}
@@ -45,8 +45,8 @@ impl ShellType {
4545
match self {
4646
ShellType::Bash => command.to_string(),
4747
ShellType::PowerShell => {
48-
// Escape single quotes and wrap in single quotes for PowerShell
49-
format!("'{}'", command.replace('\'', "''"))
48+
// PowerShell commands don't need single quote wrapping when using -Command
49+
command.to_string()
5050
},
5151
ShellType::Cmd => {
5252
// Escape special characters for CMD

0 commit comments

Comments
 (0)