@@ -12,15 +12,41 @@ pub struct CommandOutput {
1212pub 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}
0 commit comments