Skip to content

Instantly share code, notes, and snippets.

@wkronemeijer
Created February 23, 2025 22:41
Show Gist options
  • Save wkronemeijer/29d39c84670d94495729de4332a13fed to your computer and use it in GitHub Desktop.
Save wkronemeijer/29d39c84670d94495729de4332a13fed to your computer and use it in GitHub Desktop.
################
# Fix encoding #
################
$OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
#####################
# Environement vars #
#####################
$Env:NODE_ENV = "development"
######################
# Setup fancy prompt #
######################
if ($Host.UI.SupportsVirtualTerminal) {
#################
# Configuration #
#################
$contextSize = 3
########
# Code #
########
function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent()
$admin = [Security.Principal.WindowsBuiltinRole]::Administrator
return (New-Object Security.Principal.WindowsPrincipal $user).IsInRole($admin)
}
function Get-UserName {
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
return $user.Split('\')[1] # Strip the domain
}
function Get-CurrentPath {
# TODO: Make this not fail with UNC paths
$path = $executionContext.SessionState.Path.CurrentLocation.ToString()
$parts = $path.Split("\")
$count = $parts.Count
if ($count -lt (2 + $contextSize)) {
return $parts -join "/"
}
$result = New-Object System.Collections.Generic.List[string]
$result.Add($parts[0])
$result.Add("…")
$contextSize..1 | ForEach-Object {
$result.Add($parts[$count - $_])
}
return $result -join "/"
}
function Get-Time {
return $(Get-Date -Format "HH:mm")
}
function Get-Elevated {
if (Test-Administrator) { "ADMIN " } else { "" }
}
# 1 set bold
# 22 reset bold
# 30 FG, 40 BG
# 90 Bright FG, 100 Bright BG
# +0 Black
# +1 Red
# +2 Green
# +3 Yellow
# +4 Blue
# +5 Magenta
# +6 Cyan
# +7 White
# +9 Unset
$reset = "`e[0m"
$line = "`u{e0b0}"
$start2time = "`e[30;103m"
$time2user = "`e[93;41m$line`e[37m"
$user2path = "`e[31;44m$line`e[97m"
$path2prompt = "`e[34;49m$line`e[39m"
$bold = "`e[1m"
$dlob = "`e[22m"
function global:Prompt {
# Domain prefix could be useful...I wonder if you can let it show up if it is not the standard domain.
$user = Get-UserName
$time = Get-Time
$path = Get-CurrentPath
$admin = Get-Elevated
return "$reset$start2time $time $time2user $admin$bold$user$dlob $user2path $path $path2prompt $reset"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment