Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. $script:bg = [Console]::BackgroundColor;
  2. $script:first = $true;
  3. $script:last = 0;
  4.  
  5. function Write-PromptFancyEnd {
  6. Write-Host  -NoNewline -ForegroundColor $script:bg
  7.  
  8. $script:bg = [System.ConsoleColor]::Black
  9. }
  10.  
  11. function Write-PromptSegment {
  12. param(
  13. [Parameter(
  14. Position=0,
  15. Mandatory=$true,
  16. ValueFromPipeline=$true,
  17. ValueFromPipelineByPropertyName=$true
  18. )][string]$Text,
  19.  
  20. [Parameter(Position=1)][System.ConsoleColor] $Background = [Console]::BackgroundColor,
  21. [Parameter(Position=2)][System.ConsoleColor] $Foreground = [System.ConsoleColor]::White
  22. )
  23.  
  24. if(!$script:first) {
  25. Write-Host  -NoNewline -BackgroundColor $Background -ForegroundColor $script:bg
  26. } else {
  27. $script:first = $false
  28. }
  29.  
  30. Write-Host $text -NoNewline -BackgroundColor $Background -ForegroundColor $Foreground
  31.  
  32. $script:bg = $background;
  33. }
  34.  
  35. function Get-FancyDir {
  36. return $(Get-Location).ToString().Replace($env:USERPROFILE, '~').Replace('\', '  ');
  37. }
  38.  
  39. function Get-GitBranch {
  40. $HEAD = Get-Content $(Join-Path $(Get-GitDirectory) HEAD)
  41. if($HEAD -like 'ref: refs/heads/*') {
  42. return $HEAD -replace 'ref: refs/heads/(.*?)', "$1";
  43. } else {
  44. return $HEAD.Substring(0, 8);
  45. }
  46. }
  47.  
  48. function Get-GitStatusInfo {
  49. $info = Get-GitStatus
  50. $message = ''
  51. if ($info.HasIndex) { $message += "✚$($info.Index.Count)" }
  52. if ($info.HasWorking -or $info.HasUntracked) { $message += "…$($info.Working.Count)" }
  53. if ($info.BehindBy -gt 0) { $message += "↓$($info.BehindBy)" }
  54. if ($info.AheadBy -gt 0) { $message += "↑$($info.AheadBy)" }
  55. if ($info.StashCount -gt 0) { $message += "⚑$($info.StashCount)" }
  56. $message
  57. }
  58.  
  59. function Write-PromptStatus {
  60. if($script:last) {
  61. Write-PromptSegment ' ✔ ' Green Black
  62. } else {
  63. Write-PromptSegment " ✖ $lastexitcode " Red White
  64. }
  65. }
  66.  
  67. function Write-PromptUser {
  68. if($global:admin) {
  69. Write-PromptSegment ' # ADMIN ' Magenta White;
  70. } else {
  71. Write-PromptSegment " $env:USERNAME " Yellow Black;
  72. }
  73. }
  74.  
  75. function Write-PromptVirtualEnv {
  76. if($env:VIRTUAL_ENV) {
  77. Write-PromptSegment " $(split-path $env:VIRTUAL_ENV -leaf) " Cyan Black
  78. }
  79. }
  80.  
  81. function Write-PromptDir {
  82. Write-PromptSegment " $(Get-FancyDir) " DarkGray White
  83. }
  84.  
  85. # Depends on posh-git
  86. function Write-PromptGit {
  87. if(Get-GitDirectory) {
  88. Write-PromptSegment "  $(Get-GitBranch)$(Get-GitStatusInfo) " Blue White
  89. }
  90. }
  91.  
  92. function prompt {
  93. $script:last = $?;
  94. $script:first = $true;
  95.  
  96. Write-PromptStatus
  97. Write-PromptUser
  98. Write-PromptVirtualEnv
  99. Write-PromptDir
  100. Write-PromptGit
  101.  
  102. Write-PromptFancyEnd
  103.  
  104. return ' '
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement