Jeff Hardy avatar Jeff Hardy committed 92ed7ab

Initial commit of PowerShell profile

Comments (0)

Files changed (5)

Microsoft.PowerShell_profile.ps1

+Import-Module posh-hg
+
+function edit {
+    echo $params
+}
+
+function shorten-path([string] $path) { 
+   $loc = $path.Replace($env:USERPROFILE, '~')
+   
+   # shorten docs directory
+   $loc = $loc -replace '^~\\Documents\\Repositories','~\Docs\Repos'
+   $loc = $loc -replace '^~\\Documents','~\Docs'
+   
+   # remove prefix for UNC paths 
+   $loc = $loc -replace '^[^:]+::', '' 
+   
+   return $loc
+}
+
+function prompt {
+    $host.ui.rawui.WindowTitle = "$([net.dns]::GetHostName()):$pwd"
+    
+    $chost = [ConsoleColor]::Magenta
+    
+    write-host (shorten-path ($pwd).Path) -n
+    
+    # Mercurial Prompt
+    $Global:HgStatus = Get-HgStatus
+    Write-HgStatus $HgStatus
+
+    return " > "
+}
+
+if(-not (Test-Path Function:\DefaultTabExpansion)) {
+    Rename-Item Function:\TabExpansion DefaultTabExpansion
+}
+
+# Set up tab expansion and include hg expansion
+function TabExpansion($line, $lastWord) {
+    $lastBlock = [regex]::Split($line, '[|;]')[-1]
+    
+    switch -regex ($lastBlock) {
+        # mercurial and tortoisehg tab expansion
+        '(hg|hgtk) (.*)' { HgTabExpansion($lastBlock) }
+        # Fall back on existing tab expansion
+        default { DefaultTabExpansion $line $lastWord }
+    }
+}

Modules/posh-hg/HgPrompt.ps1

+$global:HgPromptSettings = New-Object PSObject -Property @{
+    BeforeText                = ' ['
+    BeforeForegroundColor     = [ConsoleColor]::Yellow
+    BeforeBackgroundColor     = $Host.UI.RawUI.BackgroundColor
+    
+    AfterText                 = ']'
+    AfterForegroundColor      = [ConsoleColor]::Yellow
+    AfterBackgroundColor      = $Host.UI.RawUI.BackgroundColor
+    
+    BranchForegroundColor    = [ConsoleColor]::Cyan
+    BranchBackgroundColor    = $Host.UI.RawUI.BackgroundColor
+    
+    WorkingForegroundColor    = [ConsoleColor]::Yellow
+    WorkingBackgroundColor    = $Host.UI.RawUI.BackgroundColor
+}
+
+function Write-HgStatus($status) {
+    if ($status) {
+        $s = $global:HgPromptSettings
+       
+        Write-Host $s.BeforeText -NoNewline -BackgroundColor $s.BeforeBackgroundColor -ForegroundColor $s.BeforeForegroundColor
+        Write-Host $status.Branch -NoNewline -BackgroundColor $s.BranchBackgroundColor -ForegroundColor $s.BranchForegroundColor
+        
+        if($status.Added) {
+          Write-Host " +$($status.Added)" -NoNewline -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
+        }
+        if($status.Modified) {
+          Write-Host " ~$($status.Modified)" -NoNewline -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
+        }
+        if($status.Deleted) {
+          Write-Host " -$($status.Deleted)" -NoNewline -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
+        }
+        
+        if ($status.Untracked) {
+          Write-Host " ?$($status.Untracked)" -NoNewline -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
+        }
+        
+        if($status.Missing) {
+           Write-Host " !$($status.Missing)" -NoNewline -BackgroundColor $s.WorkingBackgroundColor -ForegroundColor $s.WorkingForegroundColor
+        }
+      
+        Write-Host $s.AfterText -NoNewline -BackgroundColor $s.AfterBackgroundColor -ForegroundColor $s.AfterForegroundColor
+    }
+}

Modules/posh-hg/HgTabExpansion.ps1

+function HgTabExpansion($lastBlock) {
+  switch -regex ($lastBlock) { 
+    
+    #handles hg update <branch name>
+    #handles hg merge <branch name>
+    'hg (update|merge) (\S*)$' {
+      hgLocalBranches($matches[2])
+    }
+    
+    #Handles hg push <path>
+    #Handles hg pull <path>
+    'hg (push|pull) (-\S* )*(\S*)$' {
+      hgRemotes($matches[3])
+    }
+    
+    #handles hg help <cmd>
+    #handles hg <cmd>
+    'hg (help )?(\S*)$' {
+      hgCommands($matches[2]);
+    }
+
+    #handles hg <cmd> --<option>
+    'hg (\S+) (-\S* )*--(\S*)$' {
+      hgOptions $matches[1] $matches[3];
+    }
+    
+    #handles hg revert <path>
+    'hg revert (\S*)$' {
+      hgFiles $matches[1] 'M|A|R|!'
+    }
+    
+    #handles hg add <path>
+    'hg add (\S*)$' {
+      hgFiles $matches[1] '\?'
+    }
+    
+    #handles hgtk help <cmd>
+    #handles hgtk <cmd>
+    'hgtk (help )?(\S*)$' {
+      hgtkCommands($matches[2]);
+    }
+    
+  }
+}
+
+function hgFiles($filter, $pattern) {
+   hg status | 
+    foreach { 
+      if($_ -match "($pattern){1} (.*)") { 
+        $matches[2] 
+      } 
+    } |
+    where { $_ -like "*$filter*" } |
+    foreach { if($_ -like '* *') {  "'$_'"  } else { $_ } }
+}
+
+function hgRemotes($filter) {
+  hg paths | foreach {
+    $path = $_.Split("=")[0].Trim();
+    if($filter -and $path.StartsWith($filter)) {
+      $path
+    } elseif(-not $filter) {
+      $path
+    }
+  }
+}
+
+function hgCommands($filter) {
+  $cmdList = @()
+  $output = hg help
+  foreach($line in $output) {
+    if($line -match '^ (\S+) (.*)') {
+      $cmd = $matches[1]
+      if($filter -and $cmd.StartsWith($filter)) {
+        $cmdList += $cmd.Trim()
+      }
+      elseif(-not $filter) {
+        $cmdList += $cmd.Trim()
+      }
+    }
+  }
+  
+  $cmdList | sort
+}
+
+function hgLocalBranches($filter) {
+  hg branches | foreach {
+    if($_ -match "(\S+) .*") {
+      if($filter -and $matches[1].StartsWith($filter)) {
+        $matches[1]
+      }
+      elseif(-not $filter) {
+        $matches[1]
+      }
+    }
+  }
+}
+
+function hgOptions($cmd, $filter) {
+	$optList = @()
+	$output = hg help $cmd
+	foreach($line in $output) {
+		if($line -match '^ ((-\S)|  ) --(\S+) .*$') {
+			$opt = $matches[3]
+			if($filter -and $opt.StartsWith($filter)) {
+				$optList += '--' + $opt.Trim()
+			}
+			elseif(-not $filter) {
+				$optList += '--' + $opt.Trim()
+			}
+		}
+	}
+
+	$optList | sort
+}
+
+function hgtkCommands($filter) {
+  $cmdList = @()
+  $output = hgtk help
+  foreach($line in $output) {
+    if($line -match '^ (\S+) (.*)') {
+      $cmd = $matches[1]
+      if($filter -and $cmd.StartsWith($filter)) {
+        $cmdList += $cmd.Trim()
+      }
+      elseif(-not $filter) {
+        $cmdList += $cmd.Trim()
+      }
+    }
+  }
+  
+  $cmdList | sort 
+}

Modules/posh-hg/HgUtils.ps1

+function isHgDirectory() {
+  if(test-path ".hg") {
+    return $true;
+  }
+  
+  if(test-path ".git") {
+    return $false; #short circuit if git repo
+  }
+  
+  # Test within parent dirs
+  $checkIn = (Get-Item .).parent
+  while ($checkIn -ne $NULL) {
+      $pathToTest = $checkIn.fullname + '/.hg'
+      if ((Test-Path $pathToTest) -eq $TRUE) {
+          return $true
+      } else {
+          $checkIn = $checkIn.parent
+      }
+    }
+    
+    return $false
+}
+
+function Get-HgStatus {
+  if(isHgDirectory) {
+    $untracked = 0
+    $added = 0
+    $modified = 0
+    $deleted = 0
+    $missing = 0
+    
+    hg summary | foreach {   
+      switch -regex ($_) {
+        'branch: (\S*)' { $branch = $matches[1] }
+        'commit: (.*)' {
+          $matches[1].Split(",") | foreach {
+            switch -regex ($_.Trim()) {
+              '(\d+) modified' { $modified = $matches[1] }
+              '(\d+) added' { $added = $matches[1] }
+              '(\d+) removed' { $deleted = $matches[1] }
+              '(\d+) deleted' { $missing = $matches[1] }
+              '(\d+) unknown' { $untracked = $matches[1] }
+            }
+          } 
+        } 
+      } 
+    }
+    
+    return @{"Untracked" = $untracked;
+               "Added" = $added;
+               "Modified" = $modified;
+               "Deleted" = $deleted;
+               "Missing" = $missing;
+               "Branch" = $branch}
+   }
+}

Modules/posh-hg/posh-hg.psm1

+Push-Location $psScriptRoot
+. ./HgUtils.ps1
+. ./HgPrompt.ps1
+. ./HgTabExpansion.ps1
+Pop-Location
+
+Export-ModuleMember -Function @(
+  'Write-HgStatus',
+  'Get-HgStatus',
+  'HgTabExpansion'
+ )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.