PowerLine.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env powershell
using namespace System.Collections.Generic

Add-Type -Path $PSScriptRoot/PowerLine.cs


if(!$PowerLinePrompt) {
    [PowerLine.Prompt]$Script:PowerLinePrompt = @(,[PowerLine.Line]::New(
        @{ bg = "Cyan";     fg = "White"; text = { $MyInvocation.HistoryId } },
        @{ bg = "DarkBlue"; fg = "White"; text = { $pwd } }
    ))
    # Get-Location -Stack doesn't work when we define the scriptblock in the module -- not sure why
    # [PowerLine.Block]@{ bg = "cyan"; fg = "white"; text = { if($pushd = (Get-Location -Stack).count) { "»" + $pushd } } }
}

function Get-Elapsed {
   [CmdletBinding()]
   param(
      [Parameter()]
      [int]$Id,

      [Parameter()]
      [string]$Format = "{0:h\:mm\:ss\.ffff}"
   )
   $LastCommand = Get-History -Count 1 @PSBoundParameters
   if(!$LastCommand) { return "" }
   $Duration = $LastCommand.EndExecutionTime - $LastCommand.StartExecutionTime
   $Format -f $Duration
}

function Set-PowerLinePrompt {
    if($null -eq $script:OldPrompt) {
        $script:OldPrompt = $function:global:prompt
        $MyInvocation.MyCommand.Module.OnRemove = {
            $function:global:prompt = $script:OldPrompt
        }
    }

    $function:global:prompt =  {

        # FIRST, make a note if there was an error in the previous command
        $err = !$?

        try {
            if($PowerLinePrompt.SetTitle) {
                # Put the path in the title ... (don't restrict this to the FileSystem)
                $Host.UI.RawUI.WindowTitle = "{0} - {1} ({2})" -f $global:WindowTitlePrefix, (Convert-Path $pwd),  $pwd.Provider.Name
            }
            if($PowerLinePrompt.SetCwd) {
                # Make sure Windows & .Net know where we are
                # They can only handle the FileSystem, and not in .Net Core
                [Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
            }
        } catch {}

        if($Host.UI.SupportsVirtualTerminal) {
            $PowerLinePrompt.ToString()
        } else {
            "> "
        }
    }
}

Export-ModuleMember -Function Set-PowerLinePrompt, Get-Elapsed -Variable PowerLinePrompt