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() {
// First update the original ephemeral message with "Processing..."
_, 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 {
log.Printf("Error updating interaction: %v", err)
@@ -63,16 +63,23 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
progressChan = nil
continue
}
// Update message w/ phase and real time progress
phaseEmoji := "⏬"
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,
prog.Phase,
prog.Status,
prog.Percent,
renderProgressBar(prog.PercentFloat),
prog.ETA,
prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
@@ -81,6 +88,8 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
if err != nil {
log.Printf("Error updating progress: %v", err)
}
}
case result := <-resultChan:
// Handle completion
if result.Success {

View File

@@ -1,6 +1,19 @@
package main
import (
"fmt"
"strings"
)
// Helper function to create string pointer
func ptr(s string) *string {
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 {
Status ytdlp.ProgressStatus
Percent string
PercentFloat float64
ETA time.Duration
Filename string
Phase string

View File

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