Functions/Modify-Prompt.ps1

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# ============================================================================
# Modify-Prompt.ps1 --------------------------------------------------------
# ============================================================================

[System.Environment]::SetEnvironmentVariable("VIRTUAL_ENV_DISABLE_PROMPT","True","process")

# function -----------------------------------------------------------------
# ----------------------------------------------------------------------------
Function Prompt {

    Write-PromptTime -Color "DarkCyan"              # display current time
    Write-PromptAdmin -Value "Admin" -Color "Red"   # display admin

    Write-Host ([Regex]::Replace($(Get-Location),"\\.+\\","\~\")) -NoNewline -ForegroundColor "Gray"
    
    # Write-Host " " -NoNewline -ForegroundColor "Gray"

    If ((Get-Module).Name -contains "Posh-Git") {   # displays git status
        Write-VcsStatus
        Write-Host " " -NoNewline -ForegroundColor "Gray"
    }

    Write-PromptEnvStatus -Env "Venv" -Value (Split-Path $Env:VIRTUAL_ENV -leaf)
    Write-PromptEnvStatus -Env "Papis" -Value $Env:PAPIS_LIB
}

# function -----------------------------------------------------------------
# ----------------------------------------------------------------------------
function Write-PromptEnvStatus {

    [CmdletBinding(PositionalBinding=$True)]

    [OutputType([Void])]

    Param (

        [Parameter(Position=1, Mandatory=$True)]
        [System.String] $Env,

        [Parameter(Position=2, Mandatory=$True)]
        [System.String] $Value,

        [Parameter(Mandatory=$False)]
        [System.String] $EnvColor = "DarkGray",

        [Parameter(Mandatory=$False)]
        [System.String] $ParenColor = "Yellow",

        [Parameter(Mandatory=$False)]
        [System.String] $ValueColor = "Cyan"

    )
    
    Process{

        If ($Value) {       
            Write-Host $Env -NoNewline -ForegroundColor $EnvColor
            Write-Host "["  -NoNewline -ForegroundColor $ParenColor
            Write-Host  $Value -NoNewline -ForegroundColor $ValueColor
            Write-Host  "] " -NoNewline -ForegroundColor $ParenColor
        }
    }
}

# function -----------------------------------------------------------------
# ----------------------------------------------------------------------------
function Write-PromptAdmin{

    [CmdletBinding(PositionalBinding=$True)]

    [OutputType([Void])]

    Param (

        [Parameter(Position=1, Mandatory=$False)]
        [System.String] $Value = "Admin",

        [Parameter(Position=2, Mandatory=$False)]
        [System.String] $Color = "Red"

    )

    Process{

        # check whether the host as administrator rights
        $user = [Security.Principal.WindowsIdentity]::GetCurrent();
        $checkAs = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
        If ($checkAs) { 
            Write-Host "($Value) " -NoNewline -ForegroundColor $Color
        }
    }
}

# function -----------------------------------------------------------------
# ----------------------------------------------------------------------------
function Write-PromptTime {

    [CmdletBinding(PositionalBinding=$True)]

    [OutputType([Void])]

    Param (

        [Parameter(Position=1, Mandatory=$False)]
        [System.String] $Color = "DarkCyan"

    )

    Process{

        Write-Host "[" -NoNewline -ForegroundColor $Color
        Write-Host (Get-Date -UFormat %R) -NoNewline -ForegroundColor $Color
        Write-Host "] " -NoNewline -ForegroundColor $Color
    
    }
}

# function -----------------------------------------------------------------
# ----------------------------------------------------------------------------
function Write-PromptLocation {

    [CmdletBinding(PositionalBinding=$True)]

    [OutputType([Void])]

    Param (

        [Parameter(Position=1, Mandatory=$False)]
        [System.String] $Color = "Gray"

    )

    Process {

        Write-Host ([Regex]::Replace($(Get-Location),"\\.+\\","\~\")) -NoNewline -ForegroundColor $Color
        Write-Host " " -NoNewline

    }
}