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,16 +63,23 @@ 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",
phaseEmoji,
prog.Phase)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(content),
})
if err != nil {
log.Printf("Error updating progress: %v", err)
} }
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s", } else {
phaseEmoji := "⏬"
content := fmt.Sprintf("%s %s: %s [eta: %s]\n📄 %s",
phaseEmoji, phaseEmoji,
prog.Phase, prog.Phase,
prog.Status, renderProgressBar(prog.PercentFloat),
prog.Percent,
prog.ETA, prog.ETA,
prog.Filename) prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
@@ -81,6 +88,8 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
if err != nil { if err != nil {
log.Printf("Error updating progress: %v", err) 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

@@ -37,6 +37,7 @@ type FormatOptions struct {
type ProgressUpdate struct { type ProgressUpdate struct {
Status ytdlp.ProgressStatus Status ytdlp.ProgressStatus
Percent string Percent string
PercentFloat float64
ETA time.Duration ETA time.Duration
Filename string Filename string
Phase string Phase string

View File

@@ -126,6 +126,7 @@ func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progress
progressChan <- ProgressUpdate{ progressChan <- ProgressUpdate{
Status: prog.Status, Status: prog.Status,
Percent: prog.PercentString(), Percent: prog.PercentString(),
PercentFloat: prog.Percent(),
ETA: prog.ETA(), ETA: prog.ETA(),
Filename: prog.Filename, Filename: prog.Filename,
Phase: phase, Phase: phase,