dahlbyk / posh-git

A PowerShell environment for Git
http://dahlbyk.github.io/posh-git/
MIT License
7.7k stars 801 forks source link

Extract just git prompt part from $GitPromptScriptBlock #897

Closed nebula-it closed 1 year ago

nebula-it commented 2 years ago

System Details

Issue Description

I'm trying to use posh-git with a custom prompt. I have custom prompt logic already setup, plus it writes some of the stuff on right side of console. So all I need is [branch-name +1 ~0 -0 !] part from $GitPromptScriptBlock so I don't have to write that manually using values from Get-GitStatus. I almost got it using

$GitPromptSettings.DefaultPromptPath.Text = $null
$GitPromptSettings.DefaultPromptSuffix = " "

and then using this in prompt

$gitStatus =  & $GitPromptScriptBlock
Write-Host $gitStatus

However, this still prints a new line at the end. And using plain .substring or .split('[') doesnt work, I assume because of non standard chars in there.

I tried to use Write-VcsStatus as well but it still adds a new line. Can someone please guide me to get git status part without new line.

Cheers

Sorry, not sure if this is the right place but I couldnt find any other way to contact. If there is somewhere else where I should be asking for help, please feel to guide me.

dahlbyk commented 2 years ago

Try Write-Host -NoNewline

nebula-it commented 2 years ago

Dang, can't believe I didn't think of that. Anyways, thank you for quick answer @dahlbyk :)

nebula-it commented 1 year ago

Hey @dahlbyk , Is there an easy way to get length of $gitStatus in $gitStatus = & $GitPromptScriptBlock. I'm looking to add a right hand side prompt as well so need the number of characters that will be printed in my prompt for the calculation. Using $gitStatus.Length does not yield correct value as there are non-standard chars in there. e.g

> '[master ≡ +1 ~0 -0 | +2 ~1 -0 !]'.Length
32

v/s

>(& $GitPromptScriptBlock).length
154

I've gotten close by using some regex

 >$1gitStatus = & $GitPromptScriptBlock
 >([regex]::Matches($1gitStatus, "[[:print:]]*" )).Count
27

But it still misses few characters, even hardcoding those doesnt work well.

> ([regex]::Matches($1gitStatus, "[[:print:]]*|[≡↑↓↕×|+~-]" )).count
35

So, before I dig deep into this rabbit hole, just wanted to check with you if there is an easier way to get the number printed chars from $GitPromptScriptBlock Cheers

nebula-it commented 1 year ago

Managed to get it using $gitStatusLength = ($gitStatus -replace '\x1b\[[0-9;]*[a-z]', '').Length