Bug #002 Fixed. Downloading gives visuell feedback
This commit is contained in:
parent
77ef8df43f
commit
f54ce86de9
18
main.go
18
main.go
|
@ -62,6 +62,7 @@ func streamResponse(w http.ResponseWriter, fromLanguage, toLanguage string, mess
|
||||||
}
|
}
|
||||||
|
|
||||||
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
|
|
||||||
// Create a channel to send messages from the download process
|
// Create a channel to send messages from the download process
|
||||||
messages := make(chan string)
|
messages := make(chan string)
|
||||||
defer close(messages)
|
defer close(messages)
|
||||||
|
@ -86,10 +87,24 @@ func downloadPackages(w http.ResponseWriter, fromLanguage, toLanguage string) {
|
||||||
// Initial message sent to the channel for streaming
|
// 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
|
// Read the output of the Python command and send it to the channel
|
||||||
scanner := bufio.NewScanner(output)
|
scanner := bufio.NewScanner(output)
|
||||||
|
firstOutputReceived := false
|
||||||
for scanner.Scan() {
|
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
|
// 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())
|
messages <- fmt.Sprintf("Error waiting for Download: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {
|
func executeTranslator(w http.ResponseWriter, message, fromLanguage, toLanguage string) {
|
||||||
|
|
Loading…
Reference in New Issue