Skip to content

Instantly share code, notes, and snippets.

@hermanussen
Last active July 17, 2024 07:10
Show Gist options
  • Select an option

  • Save hermanussen/36d72f4d216b2caa9ca699aa98cf44b9 to your computer and use it in GitHub Desktop.

Select an option

Save hermanussen/36d72f4d216b2caa9ca699aa98cf44b9 to your computer and use it in GitHub Desktop.

Revisions

  1. hermanussen revised this gist Jul 17, 2024. 1 changed file with 101 additions and 6 deletions.
    107 changes: 101 additions & 6 deletions PowerShell_profile.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,98 @@
    function ghcs {
    # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
    # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
    param(
    [Parameter()]
    [string]$Hostname,

    [ValidateSet('gh', 'git', 'shell')]
    [Alias('t')]
    [String]$Target = 'shell',

    [Parameter(Position=0, ValueFromRemainingArguments)]
    [string]$Prompt
    )
    begin {
    # Create temporary file to store potential command user wants to execute when exiting
    $executeCommandFile = New-TemporaryFile

    # Store original value of GH_* environment variable
    $envGhDebug = $Env:GH_DEBUG
    $envGhHost = $Env:GH_HOST
    }
    process {
    if ($PSBoundParameters['Debug']) {
    $Env:GH_DEBUG = 'api'
    }

    $Env:GH_HOST = $Hostname

    gh copilot suggest -t $Target -s "$executeCommandFile" $Prompt
    }
    end {
    # Execute command contained within temporary file if it is not empty
    if ($executeCommandFile.Length -gt 0) {
    # Extract command to execute from temporary file
    $executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()

    # Insert command into PowerShell up/down arrow key history
    [Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)

    # Insert command into PowerShell history
    $now = Get-Date
    $executeCommandHistoryItem = [PSCustomObject]@{
    CommandLine = $executeCommand
    ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::NotStarted
    StartExecutionTime = $now
    EndExecutionTime = $now.AddSeconds(1)
    }
    Add-History -InputObject $executeCommandHistoryItem

    # Execute command
    Write-Host "`n"
    Invoke-Expression $executeCommand
    }
    }
    clean {
    # Clean up temporary file used to store potential command user wants to execute when exiting
    Remove-Item -Path $executeCommandFile

    # Restore GH_* environment variables to their original value
    $Env:GH_DEBUG = $envGhDebug
    }
    }

    function ghce {
    # Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
    # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
    param(
    [Parameter()]
    [string]$Hostname,

    [Parameter(Position=0, ValueFromRemainingArguments)]
    [string[]]$Prompt
    )
    begin {
    # Store original value of GH_* environment variables
    $envGhDebug = $Env:GH_DEBUG
    $envGhHost = $Env:GH_HOST
    }
    process {
    if ($PSBoundParameters['Debug']) {
    $Env:GH_DEBUG = 'api'
    }

    $Env:GH_HOST = $Hostname

    gh copilot explain $Prompt
    }
    clean {
    # Restore GH_* environment variables to their original value
    $Env:GH_DEBUG = $envGhDebug
    $Env:GH_HOST = $envGhHost
    }
    }

    Function GhCopilotSuggestShell {
    gh copilot suggest -t shell "$args"
    }
    @@ -18,6 +113,10 @@ Function GhCopilotSuggestGitHub {
    gh copilot suggest -t gh "$args"
    }

    Function GhCopilotOops {
    gh copilot suggest -t shell "I am using Powershell Core (pswh) and I tried running the following command but it failed to do what I wanted or gave an error. Please correct any typos or errors. This is the command: $(Get-History -Count 2 | Select-Object -ExpandProperty CommandLine -Last 1)"
    }

    # By default use Powershell Core
    Set-Alias ?? GhCopilotSuggestPowerShellCore

    @@ -30,9 +129,5 @@ Set-Alias shell? GhCopilotSuggestShell
    Set-Alias git? GhCopilotSuggestGit
    Set-Alias gh? GhCopilotSuggestGitHub

    # Example usage:
    #
    # > ?? list all .csv files in the current folder without file extension
    #
    # Suggestion:
    # Get-ChildItem -Filter *.csv | foreach { $_.BaseName }
    # Register the oops command to fix a previous command (inspired by https://github.com/nvbn/thefuck)
    Set-Alias oops GhCopilotOops
  2. hermanussen revised this gist Nov 21, 2023. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion PowerShell_profile.ps1
    Original file line number Diff line number Diff line change
    @@ -28,4 +28,11 @@ Set-Alias powershell? GhCopilotSuggestPowerShell
    # Regular supported Copilot targets ("gh copilot suggest --help" for more info)
    Set-Alias shell? GhCopilotSuggestShell
    Set-Alias git? GhCopilotSuggestGit
    Set-Alias gh? GhCopilotSuggestGitHub
    Set-Alias gh? GhCopilotSuggestGitHub

    # Example usage:
    #
    # > ?? list all .csv files in the current folder without file extension
    #
    # Suggestion:
    # Get-ChildItem -Filter *.csv | foreach { $_.BaseName }
  3. hermanussen revised this gist Nov 21, 2023. No changes.
  4. hermanussen created this gist Nov 21, 2023.
    31 changes: 31 additions & 0 deletions PowerShell_profile.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    Function GhCopilotSuggestShell {
    gh copilot suggest -t shell "$args"
    }

    Function GhCopilotSuggestPowerShell {
    gh copilot suggest -t shell "$args using Windows Powershell"
    }

    Function GhCopilotSuggestPowerShellCore {
    gh copilot suggest -t shell "$args using Powershell Core (pswh)"
    }

    Function GhCopilotSuggestGit {
    gh copilot suggest -t git "$args"
    }

    Function GhCopilotSuggestGitHub {
    gh copilot suggest -t gh "$args"
    }

    # By default use Powershell Core
    Set-Alias ?? GhCopilotSuggestPowerShellCore

    # Explicit calls for Powershell related stuff
    Set-Alias pwsh? GhCopilotSuggestPowerShellCore
    Set-Alias powershell? GhCopilotSuggestPowerShell

    # Regular supported Copilot targets ("gh copilot suggest --help" for more info)
    Set-Alias shell? GhCopilotSuggestShell
    Set-Alias git? GhCopilotSuggestGit
    Set-Alias gh? GhCopilotSuggestGitHub