Simple attempt at PowerShell integration #465
ZhangTianrong
started this conversation in
Show and tell
Replies: 1 comment
-
I updated it a bit function Invoke-Sgpt {
param (
[string]$InputText
)
# Ensure input is not empty
if ([string]::IsNullOrWhiteSpace($InputText)) {
Write-Host "🚫 No input provided for ShellGPT." -ForegroundColor Yellow
return
}
try {
# Inform the user that the request is being processed
Write-Host "🧠 Thinking... " -NoNewline -ForegroundColor Cyan
# Show a simple animation while waiting
$counter = 0
$job = Start-Job -ScriptBlock {
param($text)
$sgptResponse = sgpt --shell $text --no-interaction
return $sgptResponse
} -ArgumentList $InputText
while ($job.State -eq 'Running') {
Write-Host "." -NoNewline -ForegroundColor Cyan
Start-Sleep -Milliseconds 500
$counter++
if ($counter -ge 10) {
Write-Host "`r🧠 Still thinking... " -NoNewline -ForegroundColor Cyan
$counter = 0
}
}
$sgptResponse = Receive-Job -Job $job
Remove-Job -Job $job
# Clear the thinking animation
Write-Host "`r `r" -NoNewline
# Ensure response is not empty
if ([string]::IsNullOrWhiteSpace($sgptResponse)) {
Write-Host "😕 ShellGPT returned an empty response." -ForegroundColor Red
return
}
Write-Host "💡 Got it!" -ForegroundColor Green
return $sgptResponse
} catch {
# Handle potential errors
Write-Host "❌ Error invoking ShellGPT: $_" -ForegroundColor Red
}
}
function Set-SgptKeyBinding {
Set-PSReadLineKeyHandler -Chord Ctrl+l -ScriptBlock {
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)
if (-not [string]::IsNullOrWhiteSpace($line)) {
# Store the current line
$currentInput = $line
# Clear the entire line
[Microsoft.PowerShell.PSConsoleReadLine]::RevertLine()
# Invoke ShellGPT with the stored input
$sgptResponse = Invoke-Sgpt -InputText $currentInput
# If a valid response is returned, insert it into the now-empty line
if ($null -ne $sgptResponse -and -not [string]::IsNullOrWhiteSpace($sgptResponse)) {
[Microsoft.PowerShell.PSConsoleReadLine]::Insert($sgptResponse)
}
} else {
Write-Host "🤔 No input to send to ShellGPT." -ForegroundColor Yellow
}
}
}
# Bind the function to Ctrl+L
Set-SgptKeyBinding
Write-Host "✨ ShellGPT integration loaded! Press Ctrl+L to use." -ForegroundColor Magenta |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Mimicking what the
zsh
integration does, it seems intuitive to create apowershell
counterpart:Beta Was this translation helpful? Give feedback.
All reactions