Skip to content

Commit d12b821

Browse files
authored
🔀 Merge pull request #15 from gluons/feat-print-all-aliases
Add `Get-Git-Aliases`
2 parents b2fba38 + 44400c3 commit d12b821

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/git-aliases.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$FunctionsToExport = @(
44
'Get-Git-CurrentBranch',
5+
'Get-Git-Aliases',
56
'g',
67
'ga',
78
'gaa',

src/utils.ps1

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
<#
2+
.SYNOPSIS
3+
Get current git branch.
4+
#>
15
function Get-Git-CurrentBranch {
26
git symbolic-ref --quiet HEAD *> $null
7+
38
if ($LASTEXITCODE -eq 0) {
49
return git rev-parse --abbrev-ref HEAD
510
} else {
@@ -12,3 +17,88 @@ function Remove-Alias ([string] $AliasName) {
1217
Remove-Item Alias:$AliasName -Force 2> $null
1318
}
1419
}
20+
21+
function Format-AliasDefinition {
22+
param (
23+
[Parameter(Mandatory = $true)][string] $Definition
24+
)
25+
26+
$definitionLines = $Definition.Trim() -split "`n" | ForEach-Object {
27+
$line = $_.TrimEnd()
28+
29+
# Trim 1 indent
30+
if ($_ -match "^`t") {
31+
return $line.Substring(1)
32+
}
33+
elseif ($_ -match '^ ') {
34+
return $line.Substring(4)
35+
}
36+
37+
return $line
38+
39+
}
40+
41+
return $definitionLines -join "`n"
42+
}
43+
44+
<#
45+
.SYNOPSIS
46+
Get git aliases' definition.
47+
.DESCRIPTION
48+
Get definition of all git aliases or specific alias.
49+
.EXAMPLE
50+
PS C:\> Get-Git-Aliases
51+
Get definition of all git aliases.
52+
.EXAMPLE
53+
PS C:\> Get-Git-Aliases -Alias gst
54+
Get definition of `gst` alias.
55+
#>
56+
function Get-Git-Aliases ([string] $Alias) {
57+
$esc = [char] 27
58+
$green = 32
59+
$magenta = 35
60+
61+
$Alias = $Alias.Trim()
62+
$aliases = Get-Command -Module git-aliases
63+
64+
if (-not ([string]::IsNullOrEmpty($Alias))) {
65+
$foundAliases = $aliases | Where-Object -Property Name -Value $Alias -EQ
66+
67+
if ($foundAliases -is [array]) {
68+
return Format-AliasDefinition($foundAliases[0].Definition)
69+
}
70+
else {
71+
return Format-AliasDefinition($foundAliases.Definition)
72+
}
73+
}
74+
75+
$aliases = $aliases | ForEach-Object {
76+
$name = $_.Name
77+
$definition = Format-AliasDefinition($_.Definition)
78+
$definition = "$definition`n" # Add 1 line break for some row space
79+
80+
return [PSCustomObject]@{
81+
Name = $name
82+
Definition = $definition
83+
}
84+
}
85+
86+
$cols = @(
87+
@{
88+
Name = 'Name'
89+
Expression = {
90+
# Print alias name in green
91+
"$esc[$($green)m$($_.Name)$esc[0m"
92+
}
93+
},
94+
@{
95+
Name = 'Definition'
96+
Expression = {
97+
# Print alias definition in yellow
98+
"$esc[$($magenta)m$($_.Definition)$esc[0m"
99+
}
100+
}
101+
)
102+
103+
return Format-Table -InputObject $aliases -AutoSize -Wrap -Property $cols
104+
}

0 commit comments

Comments
 (0)