Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

Mercurial Prompt

Saturday, 5 November, 2011 @ 9:41 AM < Adam Boddington
Tags: Mercurial, PowerShell

After seeing Mark Embling's integration of Git into his PowerShell prompt, I wondered if I could do the same with Mercurial. Someone in Mark's comments did too, but rather than use what they came up with, I decided to have a go myself. Stuff like this is a great way to learn more PowerShell.

The idea is to detect when a directory is in a Mercurial repository, and if it is, show the Mercurial branch and status in the prompt. This should prevent accidental commits to the wrong branch (I've done that so many times in the last year), as well as give a neat little dashboard summarising WIP.

Example

I've already got a custom Prompt function, so I don't want to step all over that if I can help it. The HgPrompt function will write the Mercurial information directly to the host -- and that is it. The custom Prompt function will continue to handle the rest of the prompt and will call HgPrompt at the right time.

Also, with Mercurial, hg status displays M modified, A added and R removed files. It also displays ? unknown and ! missing files. That's potentially five numbers, so it would be nice if a number is only displayed if it is non-zero.

This is the adaption I came up with. It's a module, so it goes in the PowerShell modules directory in HgPrompt\HgPrompt.psm1.

# HgPrompt.psm1 by Adam Boddington
# http://stackingcode.com/blog/2011/11/05/mercurial-prompt
# Based on Git Functions by Mark Embling
# http://markembling.info/2009/09/my-ideal-powershell-prompt-with-git-integration

function Write-HgPrompt {
    if (-not (Test-HgRepository)) {
        return
    }
    $branch = Get-HgBranch
    $status = Get-HgStatus
    Write-Host " " -NoNewLine
    Write-Host $branch -ForegroundColor Cyan -NoNewLine
    Write-HgStatusItem $status M
    Write-HgStatusItem $status A
    Write-HgStatusItem $status R
    Write-HgStatusItem $status ? Red
    Write-HgStatusItem $status ! Red
}

function Test-HgRepository {
    if (Test-Path .hg) {
        return $true
    }
    # Test parent directories.
    $parent = (Get-Item .).Parent
    while ($parent -ne $null) {
        $pathToTest = Join-Path $parent.FullName .hg
        if (Test-Path $pathToTest) {
            return $true
        }
        $parent = $parent.Parent
    }
    return $false
}

function Get-HgBranch {
    $branch = hg branch
    return $branch
}

function Get-HgStatus {
    $status = @{ "M" = 0; "A" = 0; "R" = 0; "?" = 0; "!" = 0 }
    hg status | foreach {
        $name = $_.Substring(0, 1)
        $status[$name] += 1
    }
    return $status
}

function Write-HgStatusItem {
    param (
        $Status,
        $Name,
        $Color = "Yellow"
    )
    $value = $Status[$Name]
    if ($value -gt 0) {
        Write-Host (" " + $value + $Name) -ForegroundColor $Color -NoNewLine
    }
}

Export-ModuleMember Write-HgPrompt

To integrate this into my current PowerShell prompt, the Prompt function needs a little tweaking, mostly around using Write-Host instead of returning a string. The HgPrompt function is called before the final angle bracket.

Import-Module HgPrompt

function Prompt {
    $prompt = switch ($promptPathSize) {
        "N"     { "PS" }
        "S"     { "PS $(Split-Path $pwd -Leaf)" }
        default { "PS $pwd" }
    }
    Write-Host $prompt -NoNewLine
    Write-HgPrompt
    return "> "
}

That's it. Here is the prompt in action.

In Action

There are 0 comments.


Comments

Leave a Comment

Please register or login to leave a comment.


Older
PowerShell Prompt

Newer
Zenburn PowerShell

Older
PowerShell Prompt

Newer
Zenburn PowerShell

browse with Pivot


About


Projects

Building Neno


RSS
Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Tags

Architecture (13)
ASP.NET (2)
ASP.NET MVC (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Archives


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM