Detect Console mode vs UI

For my git hooks I needed to distinguish if git command was called from console or from UI (Git Extensions), because I wanted to have console prompts in first case, and WPF GUI in second.

At some stage we found one very complex implementation which worked fine with cmd, PowerShell, Bash and Git Extensions, but did not work for Cygwin and TortoiseGit.
That implementation was not very reliable, because it relied on the parent process of git.exe process, that we checked inside the hook.

This check was working fine from commit-msg, however none of those checks worked for pre-rebase.

I tried all my best to find an implementation which will work with different hooks and environments.

And finally I found an extremely easy implementation.

function Test-RunningFromConsole
{
    try
    {
        $height = [Console]::WindowHeight
        return ($height -ne $null)
    }
    catch
    {
        return $false
    }
}

This check is absolutely reliable

Advertisements

About mnaoumov

Senior .NET Developer in Readify
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s