Skip to content

Commit 692b896

Browse files
committed
feat: add colorful output option and enhance logging for download progress
1 parent cf190ca commit 692b896

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

powershell/yt-download.ps1

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ $Defaults = @{
7878
templateNameTitle = "%(title)s.%(ext)s"
7979
UI_Path = ""
8080
UseBrowserCookies = $true
81+
UseColorfulOutput = $true
8182
useTitle = $true
8283
videoContainer = "mp4"
8384
videoQuality = "best"
@@ -258,16 +259,24 @@ function Out-ColoredLog {
258259
[string]$InputObject
259260
)
260261

262+
begin {
263+
$script:lastWasProgress = $false
264+
}
265+
261266
process {
262267
if ($InputObject -match '^(\[(?<tag>.+?)\])(?<remainder>.*)$') {
263268
$tag = $Matches['tag']
264269
$remainder = $Matches['remainder']
265270

266271
if ($tag -eq 'debug') {
272+
if ($script:lastWasProgress) { Write-Host "" }
267273
Write-Host $InputObject -ForegroundColor DarkGray
274+
$script:lastWasProgress = $false
268275
}
269276
elseif ($tag -eq 'info') {
277+
if ($script:lastWasProgress) { Write-Host "" }
270278
Write-Host $InputObject -ForegroundColor DarkCyan
279+
$script:lastWasProgress = $false
271280
}
272281
else {
273282
$color = switch ($tag) {
@@ -278,14 +287,51 @@ function Out-ColoredLog {
278287
default { 'White' }
279288
}
280289

281-
Write-Host "[$tag]" -ForegroundColor $color -NoNewline
282-
Write-Host $remainder
290+
if ($tag -eq 'download') {
291+
# Identify standard progress lines (ex: 96.1% or 100.0%)
292+
$isProgress = $remainder -match '\d+\.\d+%' -and $remainder -notmatch '100%\s+of'
293+
294+
if ($isProgress) {
295+
# Overwrite the current line using \r
296+
Write-Host "$([char]13)[$tag]" -ForegroundColor $color -NoNewline
297+
Write-Host "$remainder" -NoNewline
298+
$script:lastWasProgress = $true
299+
}
300+
else {
301+
# If this is the final '100% of' summary, we overwrite the last progress line
302+
# instead of adding a newline, fulfilling the update logic.
303+
if ($script:lastWasProgress -and $remainder -match '100%\s+of') {
304+
Write-Host "$([char]13)[$tag]" -ForegroundColor $color -NoNewline
305+
Write-Host $remainder
306+
}
307+
else {
308+
if ($script:lastWasProgress) { Write-Host "" }
309+
Write-Host "[$tag]" -ForegroundColor $color -NoNewline
310+
Write-Host $remainder
311+
}
312+
$script:lastWasProgress = $false
313+
}
314+
}
315+
else {
316+
if ($script:lastWasProgress) { Write-Host "" }
317+
Write-Host "[$tag]" -ForegroundColor $color -NoNewline
318+
Write-Host $remainder
319+
$script:lastWasProgress = $false
320+
}
283321
}
284322
}
285323
else {
286-
Write-Host $InputObject
324+
if (-not [string]::IsNullOrWhiteSpace($InputObject)) {
325+
if ($script:lastWasProgress) { Write-Host "" }
326+
Write-Host $InputObject
327+
$script:lastWasProgress = $false
328+
}
287329
}
288330
}
331+
332+
end {
333+
if ($script:lastWasProgress) { Write-Host "" }
334+
}
289335
}
290336
<#*==========================================================================
291337
* ℹ           GET PARAMETERS
@@ -672,7 +718,7 @@ if ($url -and -not $install -and -not $uninstall) {
672718
Write-Host "yt-dlp $optionsString`n" -ForegroundColor Magenta
673719
Write-Host "running command…(wait)`n" -ForegroundColor DarkGray
674720

675-
if ($IsTest) {
721+
if ($IsTest -or (-not $UseColorfulOutput)) {
676722
& yt-dlp $options
677723
}
678724
else {

0 commit comments

Comments
 (0)