Save Icon Numbers Switch Next Git Branch with PowerShell and Oh-My-Posh
In this article, we will explore how to use PowerShell and Git commands to customize your command line prompt with Oh-My-Posh, a prompt theme engine for any shell that allows you to create a visually appealing and informative command line prompt. Specifically, we will cover how to display the current Git branch, the number of saved files, and the number of staged files, as well as how to add icons and a switch to easily toggle between visibility and invisibility of these details.
Prerequisites
To follow along with this article, you will need the following:
- PowerShell 7 or later
- Git
- Oh-My-Posh module for PowerShell
Displaying the Current Git Branch
To display the current Git branch in the command line prompt, you can use the following PowerShell code:
$gitBranch = (git branch --show-current).Trim()
Write-Host " $($gitBranch)" -NoNewlineThis code uses the git branch command to get the current Git branch, and then uses the Write-Host cmdlet to display it in the command line prompt. The -NoNewline parameter is used to prevent a new line from being added after the branch name, allowing it to be integrated seamlessly into the prompt.
Displaying the Number of Saved Files
To display the number of saved files in the command line prompt, you can use the following PowerShell code:
$savedFiles = (Get-ChildItem -Path $PWD -File -Recurse -Exclude .git).Count
Write-Host " $($savedFiles) files" -NoNewlineThis code uses the Get-ChildItem cmdlet to get a list of all files in the current directory (specified by the $PWD automatic variable) and its subdirectories, excluding the .git directory. The -File, -Recurse, and -Exclude parameters are used to specify that only files should be included, that the search should include subdirectories, and that the .git directory should be excluded, respectively. The Count property is then used to get the number of saved files, which is displayed in the command line prompt using the Write-Host cmdlet.
Displaying the Number of Staged Files
To display the number of staged files in the command line prompt, you can use the following PowerShell code:
$stagedFiles = (git diff --name-only --cached --diff-filter=ACMR).Count
Write-Host " $($stagedFiles) staged" -NoNewlineThis code uses the git diff command to get a list of all staged files. The --name-only, --cached, and --diff-filter=ACMR parameters are used to specify that only the names of the staged files should be included, that the comparison should be between the index and the last commit, and that only added, copied, modified, and renamed files should be included, respectively. The Count property is then used to get the number of staged files, which is displayed in the command line prompt using the Write-Host cmdlet.
Adding Icons and a Switch
To add icons and a switch to toggle the visibility of these details, you can modify the previous code as follows:
$visible = $true
function Toggle-Visibility {
$global:visible = !$visible
}
if ($visible) {
Write-Host " $('⌀') " -NoNewline
Write-Host " $($gitBranch) " -NoNewline
Write-Host " $($savedFiles) files " -NoNewline
Write-Host " $($stagedFiles) staged " -NoNewline
}
Write-Host " $('>') "This code creates a function called Toggle-Visibility to toggle the visibility of the details, and sets a variable $visible to keep track of the current visibility state. If $visible is $true, then the Git branch, the number of saved files, and the number of staged files are displayed using the Write-Host cmdlet. Icons are added using special characters (
⌀ for a circle, > for a right arrow). If $visible is $false, then only the right arrow icon is displayed.
Setting Up Oh-My-Posh
To set up Oh-My-Posh, you can use the following PowerShell code:
Install-Module -Name oh-my-posh -Scope CurrentUser
Set-PoshPrompt -Theme ClassicPoshThis code installs the Oh-My-Posh module and sets the prompt theme to ClassicPosh.
- We have explored how to use PowerShell and Git commands to customize the command line prompt with Oh-My-Posh
- We have covered how to display the current Git branch, the number of saved files, and the number of staged files
- We have added icons and a switch to toggle the visibility of these details
- We have set up Oh-My-Posh to display the custom prompt