function get-hgStatus( $status = @('M','A','R','C','!','?','I') ){ hg status --all | where { $_ -match "^\s*[$Status]" } | foreach { $_ -replace "\s+", ',' } | ConvertFrom-Csv -Header Status, Path } function prompt { $branch = (hg branch); $status = get-hgStatus | Group-Object Status; $modified = $status | where { $_.name -eq 'M' } | select -expand count; $added = $status | where { $_.name -eq 'A' } | select -expand count; $removed = $status | where { $_.name -eq 'R' } | select -expand count; $untracked = $status | where { $_.name -eq '?' } | select -expand count; $missing = $status | where { $_.name -eq '!' } | select -expand count; Write-Host "PS $pwd" -NoNewline if ($branch){ write-host "[" -NoNewline write-host " $branch " -ForegroundColor White -NoNewline if ($added){ write-host " " -NoNewline write-host "+$added" -ForegroundColor green -NoNewline; } if ($modified){ write-host " " -NoNewline write-host $modified -ForegroundColor yellow -NoNewline; } if ($removed){ write-host " " -NoNewline write-host "-$removed" -ForegroundColor magenta -NoNewline; } if ($missing){ write-host " " -NoNewline write-host "!$missing" -ForegroundColor red -NoNewline; } if ($untracked){ write-host " " -NoNewline write-host "?$untracked" -ForegroundColor gray -NoNewline; } write-host "]" -NoNewline; } "> " }Note the use of a default value in the passed in $status variable, which is default set to an array of all the possible hg status flags. The hg-GetStatus function can be called individually like hg-GetStatus A to show all the added files in the repo. The script above uses the hg status command to get the status of the current hg repository in the folder. If the current folder is not a hg repository, a default prompt is shown instead. The output of the command hg status is converted into a structured object and then grouped by status using the Group-Object cmdlet. A pipeline is used to prepare the output of the hg status before it is transformed into the structured object. The count of each group is retrieved with the Select cmdlet, and using the -extract flag. The group counts are then shown to the user in the prompt and formatted with color. Make note of the use of -NoNewLine and -ForegroundColor. In addition, the Powershell function prompt will control how your prompt in the shell will look like, i.e. the command line. I put this prompt in the $profile file such that this is inited each time.
This is just an introduction to customizing the Powershell command line to a more suited hg aware prompt for developers using hg. There is actually already a better package available on the Internet for displaying the state of the hg repository in the folder displayed in Powershell, which is called hg posh. Check out hg posh on the following url:
hg posh
However, although hg posh is more correct, I like the look of this command prompt better ... For many hg users, this will suffice.. And here is our nice new Mercurial aware command line: