Skip to content

Commit aa23533

Browse files
🩹 [Patch]: Update formatting of GitHubContext and GitHubRateLimitResource (#474)
## Description This pull request introduces updates to improve the handling and display of time-related properties in PowerShell scripts. The changes focus on replacing direct property access with the `Format-TimeSpan` function for better readability and consistency, while also adding color-coded visual indicators for certain time-related values. ### Time property updates: * 🪲 `src/formats/GitHubContext.Format.ps1xml`: Replaced `Remaining` with `TokenExpiresIn` for time calculations and display, and formatted the output using `Format-TimeSpan` for improved readability. * 🩹 `src/formats/GitHubRateLimitResource.Format.ps1xml`: Added a `ScriptBlock` to format the `ResetsIn` property using `Format-TimeSpan`. Introduced color-coded visual indicators for `ResetsIn` values based on their proximity to expiration, enhancing the user experience. ### Dependency update: * `src/header.ps1`: Added a `#Requires` directive for the `TimeSpan` module with version `3.0.0` to ensure compatibility with the new `Format-TimeSpan` function. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [x] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas
1 parent 7c13cbf commit aa23533

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class GitHubFormatter {
2+
static [string] FormatColorByRatio([double]$Ratio, [string]$Text) {
3+
$Ratio = [Math]::Min([Math]::Max($Ratio, 0), 1)
4+
5+
if ($Ratio -ge 1) {
6+
$r = 0
7+
$g = 255
8+
} elseif ($Ratio -le 0) {
9+
$r = 255
10+
$g = 0
11+
} elseif ($Ratio -ge 0.5) {
12+
$r = [Math]::Round(255 * (2 - 2 * $Ratio))
13+
$g = 255
14+
} else {
15+
$r = 255
16+
$g = [Math]::Round(255 * (2 * $Ratio))
17+
}
18+
$color = "`e[38;2;$r;$g;0m"
19+
$reset = "`e[0m"
20+
return "$color$Text$reset"
21+
}
22+
}

src/formats/GitHubContext.Format.ps1xml

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,45 +54,28 @@
5454
</TableColumnItem>
5555
<TableColumnItem>
5656
<ScriptBlock>
57-
if ($null -eq $_.Remaining) {
57+
if ($null -eq $_.TokenExpiresIn) {
5858
return
5959
}
6060

61-
if ($_.Remaining -lt 0) {
62-
$text = "Expired"
61+
if ($_.TokenExpiresIn -lt 0) {
62+
$text = 'Expired'
6363
} else {
64-
$text = "$($_.Remaining.Hours)h $($_.Remaining.Minutes)m
65-
$($_.Remaining.Seconds)s"
64+
$text = $_.TokenExpiresIn.ToString('hh\:mm\:ss')
6665
}
6766

6867
if ($Host.UI.SupportsVirtualTerminal -and
6968
($env:GITHUB_ACTIONS -ne 'true')) {
7069
switch ($_.AuthType) {
7170
'UAT' {
72-
$MaxValue = [TimeSpan]::FromHours(8)
71+
$maxValue = [TimeSpan]::FromHours(8)
7372
}
7473
'IAT' {
75-
$MaxValue = [TimeSpan]::FromHours(1)
74+
$maxValue = [TimeSpan]::FromHours(1)
7675
}
7776
}
78-
$ratio = [Math]::Min(($_.Remaining / $MaxValue), 1)
79-
80-
if ($ratio -ge 1) {
81-
$r = 0
82-
$g = 255
83-
} elseif ($ratio -le 0) {
84-
$r = 255
85-
$g = 0
86-
} elseif ($ratio -ge 0.5) {
87-
$r = [Math]::Round(255 * (2 - 2 * $ratio))
88-
$g = 255
89-
} else {
90-
$r = 255
91-
$g = [Math]::Round(255 * (2 * $ratio))
92-
}
93-
$b = 0
94-
$color = $PSStyle.Foreground.FromRgb($r, $g, $b)
95-
"$color$text$($PSStyle.Reset)"
77+
$ratio = [Math]::Min(($_.TokenExpiresIn / $maxValue), 1)
78+
[GitHubFormatter]::FormatColorByRatio($ratio, $text)
9679
} else {
9780
$text
9881
}

src/formats/GitHubRateLimitResource.Format.ps1xml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,33 @@
4646
<PropertyName>ResetsAt</PropertyName>
4747
</TableColumnItem>
4848
<TableColumnItem>
49-
<PropertyName>ResetsIn</PropertyName>
49+
<ScriptBlock>
50+
if ($null -eq $_.ResetsIn) {
51+
return
52+
}
53+
54+
if ($_.ResetsIn -lt 0) {
55+
$text = 'Expired'
56+
} else {
57+
$text = $_.ResetsIn.ToString('hh\:mm\:ss')
58+
}
59+
60+
if ($Host.UI.SupportsVirtualTerminal -and
61+
($env:GITHUB_ACTIONS -ne 'true')) {
62+
if ($_.Name -in @('source_import',
63+
'dependency_snapshots', 'code_scanning_autofix', 'search',
64+
'dependency_sbom', 'code_search')) {
65+
$maxValue = [TimeSpan]::FromMinutes(1)
66+
} else {
67+
$maxValue = [TimeSpan]::FromHours(1)
68+
}
69+
70+
$ratio = [Math]::Min(($_.ResetsIn / $maxValue), 1)
71+
[GitHubFormatter]::FormatColorByRatio($ratio, $text)
72+
} else {
73+
$text
74+
}
75+
</ScriptBlock>
5076
</TableColumnItem>
5177
</TableColumnItems>
5278
</TableRowEntry>

src/header.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains long links.')]
1+
#Requires -Modules @{ ModuleName = 'TimeSpan'; RequiredVersion = '3.0.1' }
2+
3+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains long links.')]
24
[CmdletBinding()]
35
param()

0 commit comments

Comments
 (0)