Guest User

Untitled

a guest
Feb 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. class CustomPromptPath {
  2.  
  3. static [bool]PathEquals([System.IO.DirectoryInfo]$a, [System.IO.DirectoryInfo]$b) {
  4. return $a.FullName.Equals($b.FullName, 'OrdinalIgnoreCase')
  5. }
  6.  
  7. static [String]GetCustomPromptPath([String]$homePath, [String]$currentPath) {
  8. $homeItem = Get-Item $homePath
  9. $current = Get-Item $currentPath
  10.  
  11. $stack = New-Object 'System.Collections.Generic.Stack[string]'
  12.  
  13. while ($null -ne $current) {
  14.  
  15. if ([CustomPromptPath]::PathEquals($current, $homeItem)) {
  16. $stack.Push('~')
  17. $current = $null
  18. continue
  19. }
  20.  
  21. if ($stack.Count -eq 0 -or $null -eq $current.Parent) { $stack.Push($current.Name) }
  22. else { $stack.Push($current.Name[0]) }
  23.  
  24. $current = $current.Parent
  25. }
  26.  
  27. $builder = New-Object 'System.Text.StringBuilder'
  28. while ($stack.Count -gt 0) {
  29. if ($builder.Length -gt 0 -and $builder[$builder.Length - 1] -ne '\') { $builder.Append('\') }
  30. $builder.Append($stack.Pop())
  31. }
  32.  
  33. return $builder.ToString()
  34. }
  35. }
  36.  
  37.  
  38. function prompt {
  39. Write-Host $([CustomPromptPath]::GetCustomPromptPath($HOME, $PWD)) -ForegroundColor Yellow -NoNewline
  40. return " > "
  41. }
Add Comment
Please, Sign In to add comment