[READ-ONLY] Mirror of https://github.com/bombshell-dev/tab. shell autocompletions for javascript CLIs
4

Configure Feed

Select the types of activity you want to include in your feed.

fix desc issue in pwsh

AmirSa12 (Oct 27, 2024, 5:49 PM +0330) 46fe611c b286387b

+52 -59
+2 -2
demo.ts
··· 131 131 .option("--strictPort", `[boolean] exit if specified port is already in use`) 132 132 .option( 133 133 "--force", 134 - `[boolean] force the optimizer to ignore the cache and re-bundle`, 134 + `[boolean] force the optimizer to ignore the cache and re-bundle` 135 135 ) 136 136 .action((root, options) => { 137 137 console.log(`Starting dev server at ${root || "."} with options:`, options); ··· 193 193 // Completion for --config 194 194 flagMap.set(optionKey, async (previousArgs, toComplete) => { 195 195 const configFiles = ["vite.config.ts", "vite.config.js"].filter( 196 - (file) => file.startsWith(toComplete), 196 + (file) => file.startsWith(toComplete) 197 197 ); 198 198 return configFiles.map((file) => ({ action: file })); 199 199 });
+50 -57
powershell.ts
··· 57 57 # Make sure the $Command is longer then the $CursorPosition before we truncate. 58 58 # This happens because the $Command does not include the last space. 59 59 if ($Command.Length -gt $CursorPosition) { 60 - $Command=$Command.Substring(0,$CursorPosition) 60 + $Command = $Command.Substring(0, $CursorPosition) 61 61 } 62 62 __${name}_debug "Truncated command: $Command" 63 63 ··· 68 68 $ShellCompDirectiveFilterDirs=${ShellCompDirectiveFilterDirs} 69 69 $ShellCompDirectiveKeepOrder=${ShellCompDirectiveKeepOrder} 70 70 71 - # Original exec value 72 - $originalExec = "${exec}" 73 - 74 - # Split the exec string by spaces to handle each part 75 - $execParts = $originalExec -split ' ' 76 - 77 - __${name}_debug "Value of exec: ${exec}" 78 - 79 71 # Prepare the command to request completions for the program. 80 72 # Split the command at the first space to separate the program and arguments. 81 - $Program,$Arguments = $Command.Split(" ",2) 73 + $Program, $Arguments = $Command.Split(" ", 2) 82 74 83 75 $RequestComp = "& ${exec} complete -- $Arguments" 84 76 __${name}_debug "RequestComp: $RequestComp" 85 77 86 - # We cannot use $WordToComplete because it 78 + # we cannot use $WordToComplete because it 87 79 # has the wrong values if the cursor was moved 88 80 # so use the last argument 89 81 if ($WordToComplete -ne "" ) { ··· 91 83 } 92 84 __${name}_debug "New WordToComplete: $WordToComplete" 93 85 86 + 94 87 # Check for flag with equal sign 95 88 $IsEqualFlag = ($WordToComplete -Like "--*=*" ) 96 89 if ( $IsEqualFlag ) { 97 90 __${name}_debug "Completing equal sign flag" 98 91 # Remove the flag part 99 - $Flag,$WordToComplete = $WordToComplete.Split("=",2) 92 + $Flag, $WordToComplete = $WordToComplete.Split("=", 2) 100 93 } 101 94 102 95 if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) { 103 96 # If the last parameter is complete (there is a space following it) 104 - # We add an extra empty parameter so we can indicate this to the program. 97 + # We add an extra empty parameter so we can indicate this to the go method. 105 98 __${name}_debug "Adding extra empty parameter" 106 99 # PowerShell 7.2+ changed the way how the arguments are passed to executables, 107 100 # so for pre-7.2 or when Legacy argument passing is enabled we need to use 108 - # \`"\`" to pass an empty argument, a "" or '' does not work!!! 109 101 if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or 110 102 ($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or 111 103 (($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and 112 104 $PSNativeCommandArgumentPassing -eq 'Legacy')) { 113 105 $RequestComp="$RequestComp" + ' \`"\`"' 114 106 } else { 115 - $RequestComp="$RequestComp" + ' ""' 107 + $RequestComp = "$RequestComp" + ' ""' 116 108 } 117 109 } 118 110 119 - __${name}_debug "Calling: $RequestComp" 120 - # First disable ActiveHelp which is not supported for PowerShell 111 + __${name}_debug "Calling $RequestComp" 112 + # First disable ActiveHelp which is not supported for Powershell 113 + $env:ActiveHelp = 0 121 114 122 - # Call the command, store the output in $out, and redirect stderr and stdout to null 123 - # $Out is an array containing each line per element 115 + # call the command store the output in $out and redirect stderr and stdout to null 116 + # $Out is an array contains each line per element 124 117 Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null 125 118 126 - # Get directive from last line 119 + # get directive from last line 127 120 [int]$Directive = $Out[-1].TrimStart(':') 128 121 if ($Directive -eq "") { 129 122 # There is no directive specified ··· 131 124 } 132 125 __${name}_debug "The completion directive is: $Directive" 133 126 134 - # Remove directive (last element) from out 127 + # remove directive (last element) from out 135 128 $Out = $Out | Where-Object { $_ -ne $Out[-1] } 136 129 __${name}_debug "The completions are: $Out" 137 130 138 131 if (($Directive -band $ShellCompDirectiveError) -ne 0 ) { 139 - # Error code. No completion. 140 - __${name}_debug "Received error from custom completion code" 132 + # Error code. No completion. 133 + __${name}_debug "Received error from custom completion go code" 141 134 return 142 135 } 143 136 144 137 $Longest = 0 145 138 [Array]$Values = $Out | ForEach-Object { 146 - # Split the output into name and description 147 - $Name, $Description = $_.Split("t",2) 139 + # Split the output in name and description 140 + $Name, $Description = $_.Split("\`t", 2) 148 141 __${name}_debug "Name: $Name Description: $Description" 149 142 150 - # Look for the longest completion to format things nicely 143 + # Look for the longest completion so that we can format things nicely 151 144 if ($Longest -lt $Name.Length) { 152 145 $Longest = $Name.Length 153 146 } 154 147 155 - # Set the description to a one-space string if none is set 156 - # This is needed because CompletionResult does not accept an empty string 148 + # Set the description to a one space string if there is none set. 149 + # This is needed because the CompletionResult does not accept an empty string as argument 157 150 if (-Not $Description) { 158 151 $Description = " " 159 152 } 160 - @{Name="$Name";Description="$Description"} 153 + @{ Name = "$Name"; Description = "$Description" } 161 154 } 162 155 156 + 163 157 $Space = " " 164 158 if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) { 165 - # Remove the space here 159 + # remove the space here 166 160 __${name}_debug "ShellCompDirectiveNoSpace is called" 167 161 $Space = "" 168 162 } 169 163 170 164 if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or 171 165 (($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) { 172 - __${name}_debug "ShellCompDirectiveFilterFileExt and ShellCompDirectiveFilterDirs are not supported" 166 + __${name}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported" 173 167 174 - # Return here to prevent the completion of the extensions 168 + # return here to prevent the completion of the extensions 175 169 return 176 170 } 177 171 178 172 $Values = $Values | Where-Object { 179 - # Filter the result 173 + # filter the result 180 174 $_.Name -like "$WordToComplete*" 181 175 182 176 # Join the flag back if we have an equal sign flag ··· 186 180 } 187 181 } 188 182 189 - # Sort the values in ascending order by name if keep order isn't passed 183 + # we sort the values in ascending order by name if keep order isn't passed 190 184 if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) { 191 185 $Values = $Values | Sort-Object -Property Name 192 186 } ··· 195 189 __${name}_debug "ShellCompDirectiveNoFileComp is called" 196 190 197 191 if ($Values.Length -eq 0) { 198 - # Just print an empty string here so the shell does not start to complete paths 199 - # We cannot use CompletionResult here because it does not accept an empty string 192 + # Just print an empty string here so the 193 + # shell does not start to complete paths. 194 + # We cannot use CompletionResult here because 195 + # it does not accept an empty string as argument. 200 196 "" 201 197 return 202 198 } 203 199 } 204 200 205 201 # Get the current mode 206 - $Mode = (Get-PSReadLineKeyHandler | Where-Object {$_.Key -eq "Tab" }).Function 202 + $Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function 207 203 __${name}_debug "Mode: $Mode" 208 204 209 205 $Values | ForEach-Object { 210 206 211 - # Store temporary because switch will overwrite $_ 207 + # store temporary because switch will overwrite $_ 212 208 $comp = $_ 213 209 214 210 # PowerShell supports three different completion modes 215 - # - TabCompleteNext (default Windows style) 211 + # - TabCompleteNext (default windows style - on each key press the next option is displayed) 216 212 # - Complete (works like bash) 217 213 # - MenuComplete (works like zsh) 218 - # Set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode> 214 + # You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode> 219 215 220 216 # CompletionResult Arguments: 221 - # 1) CompletionText: text to be used as the auto-completion result 222 - # 2) ListItemText: text to be displayed in the suggestion list 223 - # 3) ResultType: type of completion result 224 - # 4) ToolTip: text for the tooltip with details about the object 217 + # 1) CompletionText text to be used as the auto completion result 218 + # 2) ListItemText text to be displayed in the suggestion list 219 + # 3) ResultType type of completion result 220 + # 4) ToolTip text for the tooltip with details about the object 225 221 226 222 switch ($Mode) { 227 223 228 - # Bash-like 224 + # bash like 229 225 "Complete" { 230 226 231 227 if ($Values.Length -eq 1) { 232 228 __${name}_debug "Only one completion left" 233 229 234 - # Insert space after value 230 + # insert space after value 235 231 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") 236 232 237 233 } else { ··· 251 247 } 252 248 } 253 249 254 - # Zsh-like 250 + # zsh like 255 251 "MenuComplete" { 256 - # Insert space after value 257 - # MenuComplete will automatically show the ToolTip 252 + # insert space after value 253 + # MenuComplete will automatically show the ToolTip of 254 + # the highlighted value at the bottom of the suggestions. 258 255 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)") 259 256 } 260 257 261 - # Default and TabCompleteNext 258 + # TabCompleteNext and in case we get something unknown 262 259 Default { 263 - # Like MenuComplete but without adding a space 264 - # Description will not be shown as it's not possible with TabCompleteNext 260 + # Like MenuComplete but we don't want to add a space here because 261 + # the user need to press space anyway to get the completion. 262 + # Description will not be shown because that's not possible with TabCompleteNext 265 263 [System.Management.Automation.CompletionResult]::new($($comp.Name | __${name}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)") 266 264 } 267 265 } 268 266 269 267 } 270 268 } 271 - 272 - $Values | ForEach-Object { 273 - __${name}_debug "Completion suggestion: $($_.Name)" 274 - } 275 - 276 269 277 270 Register-ArgumentCompleter -CommandName '${name}' -ScriptBlock $__${nameForVar}CompleterBlock 278 271 `;