Last active
April 3, 2023 15:00
-
-
Save thomastay/ae9e7245f752a195a5a82032a9060f13 to your computer and use it in GitHub Desktop.
Powershell fish prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function replaceHome($pathArray) { | |
# Check whether the first three paths are equal to HOME | |
# If it is, it substitutes it for ~ | |
# Change this accordingly, if your home path is more than three | |
# paths long. | |
$splitChar = [System.IO.Path]::DirectorySeparatorChar | |
if ( ($pathArray.Length -gt 2) -and | |
(($pathArray[0..2] -join $splitChar) -eq $HOME)) { | |
@("~") + $pathArray[3..$pathArray.Length] | |
} | |
else { | |
$pathArray | |
} | |
} | |
function fish_prompt { | |
$splitChar = [System.IO.Path]::DirectorySeparatorChar | |
$currLocation = (Get-Location) | |
if ($currLocation.ToString() -eq $HOME) { | |
# Special case the home directory | |
"~>" | |
} | |
else { | |
$current = Split-Path -Leaf -Path $currLocation | |
$parent = Split-Path -Parent -Path $currLocation | |
$folders = $parent.Split($splitChar, [System.StringSplitOptions]::RemoveEmptyEntries) | |
if ($folders.Length -lt 2) { | |
# If the path is 0 or 1, then ignore. | |
$currLocation.ToString() + ">" | |
} | |
else { | |
$folders = replaceHome($folders) # optional, to get the ~/Docs effect | |
# Replace everything except the first name | |
# Note: On line 35, (+) for array concat avoids issue of empty arr | |
$firstNameFolders = ForEach ($f in $folders[1..$folders.Length]) { $f[0] } | |
$folders = @($folders[0]) + $firstNameFolders + @($current) | |
($folders -join $splitChar) + ">" | |
} | |
} | |
} | |
function prompt { | |
fish_prompt | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EDIT Jun 29, 2020: Minor changes to cache (Get-Location), and use the -Join operator instead of the .NET string join.