.history/pwsh-wakatime_20210223112008.ps1

# wakatime for powershell
#
# include this file in your `$profile`
#
# Don't forget to create and configure your "~/.wakatime.cfg" file.
#
# based on bash-wakatime and conda

## PROMPT MANAGEMENT ###########################################################

<#
    .SYNOPSIS
        Modifies the current prompt to send wakatime a tick.
    .EXAMPLE
        Send-HeartbeatAtPrompt
 
        Causes a WakaTime heartbeat sent at the current session's prompt.
#>


# We use the same procedure to nest prompts as we did for nested tab completion.
if (Test-Path Function:\prompt) {
    Rename-Item Function:\prompt WakaTimePromptBackup
} else {
    function WakaTimePromptBackup() {
        # Restore a basic prompt if the definition is missing.
        "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
    }
}

function Test-Wakatime{

    $wakatime = $(where.exe wakatime);

    if($wakatime) {
        return $True;
    } else {
        return $False;
    }
}

$env:wakaDebug = $False

function Send-Wakatime(){
    if(!(Test-Wakatime)) {
        return;
    }
    $command = "";
    try {
        $historyItem = (Get-History | Select-Object -Last 1)
        $commandObj = ($historyItem | Select-Object -Property CommandLine).CommandLine
        $commandText = ([regex]::split($commandObj,"[ |;:]")[0])
        $command = $commandText.Replace("(","")
    } catch [Exception] {
        if($command -eq "") {
            $command = "error"
        }
    }
    $gitFolder = (Get-GitDirectory);
    Get-Job -State Completed|Where-Object{$_.Name.Contains("WakaJob")}|Remove-Job
    $job = Start-Job -Name "WakaJob" -ScriptBlock {
        param($command, $gitFolder)

        Write-Host "Sending wakatime"
        Write-Host "Waka command: $command"
        if($command -eq "") {
            return;
        }

        $PLUGIN_VERSION = "0.2";

        $wakaCommand = 'wakatime --write'
        $wakaCommand =$wakaCommand + " --plugin `"powershell-wakatime-iamkarlson-plugin/$PLUGIN_VERSION`""
        $wakaCommand =$wakaCommand + ' --entity-type app '
        $wakaCommand =$wakaCommand + ' --entity "'
        $wakaCommand =$wakaCommand +  $command
        $wakaCommand =$wakaCommand + '" '
        $wakaCommand =$wakaCommand + ' --language "PowerShell"'

        if($gitFolder -eq $null){
        } else {
            $gitFolder = (get-item ($gitFolder).Replace(".git",""))
            $wakaCommand =$wakaCommand + ' --project "'
            $wakaCommand =$wakaCommand + $gitFolder.Name
            $wakaCommand =$wakaCommand + '"'
        }
        $envwakaDebug=$env:wakaDebug
        Write-Host "wakaDebug: $envwakaDebug"
        $wakaCommand
        if($envwakaDebug){
            $wakaCommand |out-file ~/.powerwaka.log -Append
        }
        iex $wakaCommand
    } -ArgumentList $command, $gitFolder
}


function Send-HeartbeatAtPrompt() {
    function global:prompt() {
        version="1.0.0"
        entity=$(echo $(fc -ln -0) | cut -d ' ' -f1)
        [ -z "$entity" ] && return # $entity is empty or only whitespace
        $(git rev-parse --is-inside-work-tree 2> /dev/null) && local project="$(basename $(git rev-parse --show-toplevel))" || local project="Terminal"
        (wakatime --write --plugin "bash-wakatime/$version" --entity-type app --project "$project" --entity "$entity" 2>&1 > /dev/null &)
        if ($Env:WAKATIME_PROMPT_MODIFIER) {
            $Env:WAKATIME_PROMPT_MODIFIER | Write-Host -NoNewline
        }
        WakaTimePromptBackup;
    }
}