From f54ce86de9dfa3dc30569556728ae823c0a85168 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Fri, 15 Mar 2024 14:42:42 +0100 Subject: [PATCH] Bug #002 Fixed. Downloading gives visuell feedback --- main.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index b9a14e3..8f3403d 100644 --- a/main.go +++ b/main.go @@ -62,6 +62,7 @@ func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, mess } func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { + // Create a channel to send messages from the download process messages := make(chan string) defer close(messages) @@ -84,12 +85,26 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { } // Initial message sent to the channel for streaming - messages <- "Download started . . ." + messages <- "Download started..." + + // Start a ticker to send "..." every few seconds until we get real output + ticker := time.NewTicker(2 * time.Second) + go func() { + for range ticker.C { + messages <- ". . ." + } + }() // Read the output of the Python command and send it to the channel scanner := bufio.NewScanner(output) + firstOutputReceived := false for scanner.Scan() { - messages <- scanner.Text() // Sends output line by line to the stream + if !firstOutputReceived { + // Stop the ticker after receiving the first real output + ticker.Stop() + firstOutputReceived = true + } + messages <- scanner.Text() } // Wait for the command to complete @@ -98,6 +113,7 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) { messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error()) return } + return } func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {