implement progress bar

This commit is contained in:
2026-03-04 21:06:41 -05:00
parent b3d779374b
commit 3cac63ba82
4 changed files with 51 additions and 27 deletions

View File

@@ -50,7 +50,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
go func() { go func() {
// First update the original ephemeral message with "Processing..." // First update the original ephemeral message with "Processing..."
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(fmt.Sprintf("🔄 Processing download...\nURL: %s\nVideo: %s\nAudio: %s", url, videoFormatID, audioFormatID)), Content: ptr(fmt.Sprintf("Downloading video: %s", renderProgressBar(0))),
}) })
if err != nil { if err != nil {
log.Printf("Error updating interaction: %v", err) log.Printf("Error updating interaction: %v", err)
@@ -63,24 +63,33 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
progressChan = nil progressChan = nil
continue continue
} }
// Update message w/ phase and real time progress
phaseEmoji := "⏬"
if prog.Phase == "post-processing" { if prog.Phase == "post-processing" {
phaseEmoji = "⚙️" phaseEmoji := "⚙️"
} content := fmt.Sprintf("%s %s",
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s", phaseEmoji,
phaseEmoji, prog.Phase)
prog.Phase, _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
prog.Status, Content: ptr(content),
prog.Percent, })
prog.ETA, if err != nil {
prog.Filename) log.Printf("Error updating progress: %v", err)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ }
Content: ptr(content), } else {
}) phaseEmoji := "⏬"
if err != nil { content := fmt.Sprintf("%s %s: %s [eta: %s]\n📄 %s",
log.Printf("Error updating progress: %v", err) phaseEmoji,
prog.Phase,
renderProgressBar(prog.PercentFloat),
prog.ETA,
prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(content),
})
if err != nil {
log.Printf("Error updating progress: %v", err)
}
} }
case result := <-resultChan: case result := <-resultChan:
// Handle completion // Handle completion
if result.Success { if result.Success {

View File

@@ -1,6 +1,19 @@
package main package main
import (
"fmt"
"strings"
)
// Helper function to create string pointer // Helper function to create string pointer
func ptr(s string) *string { func ptr(s string) *string {
return &s return &s
} }
const progressBarLength = 20
func renderProgressBar(percent float64) string {
filled := int(float64(progressBarLength) * percent / 100)
bar := strings.Repeat("█", filled) + strings.Repeat(" ", progressBarLength-filled)
return fmt.Sprintf("[%s] %.0f%%", bar, percent)
}

View File

@@ -35,11 +35,12 @@ type FormatOptions struct {
} }
type ProgressUpdate struct { type ProgressUpdate struct {
Status ytdlp.ProgressStatus Status ytdlp.ProgressStatus
Percent string Percent string
ETA time.Duration PercentFloat float64
Filename string ETA time.Duration
Phase string Filename string
Phase string
} }
// InteractionState holds the state for a specific interaction // InteractionState holds the state for a specific interaction

View File

@@ -124,11 +124,12 @@ func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progress
lastPhase = phase lastPhase = phase
progressChan <- ProgressUpdate{ progressChan <- ProgressUpdate{
Status: prog.Status, Status: prog.Status,
Percent: prog.PercentString(), Percent: prog.PercentString(),
ETA: prog.ETA(), PercentFloat: prog.Percent(),
Filename: prog.Filename, ETA: prog.ETA(),
Phase: phase, Filename: prog.Filename,
Phase: phase,
} }
}). }).
Output("%(title)s.%(ext)s") Output("%(title)s.%(ext)s")