Skip to content

Commit

Permalink
Merge pull request #1 from Aryagorjipour/Add-Watch-Command
Browse files Browse the repository at this point in the history
feat: Add the watch command
  • Loading branch information
Aryagorjipour authored Nov 23, 2024
2 parents 8fd0847 + 4a29197 commit 5116952
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
40 changes: 39 additions & 1 deletion internal/manager/manager.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package manager

import (
"bufio"
"fmt"
"github.com/Aryagorjipour/smart-file-downloader/internal/task"
"os"
"strings"
"sync"
"time"

"github.com/Aryagorjipour/smart-file-downloader/internal/task"
)

// DownloadManager manages download tasks
Expand Down Expand Up @@ -92,3 +97,36 @@ func (dm *DownloadManager) GetTasks() map[int]*task.DownloadTask {
}
return copyTasks
}

func (mgr *DownloadManager) Watch() {

Check failure on line 101 in internal/manager/manager.go

View workflow job for this annotation

GitHub Actions / build

exported method DownloadManager.Watch should have comment or be unexported

Check failure on line 101 in internal/manager/manager.go

View workflow job for this annotation

GitHub Actions / build

receiver name mgr should be consistent with previous receiver name dm for DownloadManager
reader := bufio.NewReader(os.Stdin)
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

done := make(chan bool)

go func() {
for {
select {
case <-ticker.C:
mgr.ListDownloads()
case <-done:
return
}
}
}()

for {
input, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading input:", err)
continue
}
if strings.TrimSpace(input) == "q" {
break
}
}

done <- true
fmt.Println("Exited watch mode.")
}
18 changes: 6 additions & 12 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package ui
import (
"bufio"
"fmt"
"github.com/Aryagorjipour/smart-file-downloader/internal/manager"
"os"
"strconv"
"strings"
"time"

"github.com/Aryagorjipour/smart-file-downloader/internal/manager"
)

// Start starts the UI
Expand All @@ -16,23 +16,15 @@ func Start(mgr *manager.DownloadManager) {
fmt.Println("Concurrent File Downloader")
fmt.Printf("Downloads will be saved to: %s\n", mgr.DownloadDir)

// Start a goroutine to display progress periodically
go func() {
for len(mgr.Tasks) > 0 {
time.Sleep(2 * time.Second)
mgr.ListDownloads()
fmt.Println("------")
}
}()

for {
fmt.Println("Commands:")
fmt.Println("1. add <URL>")
fmt.Println("2. list")
fmt.Println("3. pause <ID>")
fmt.Println("4. resume <ID>")
fmt.Println("5. cancel <ID>")
fmt.Println("6. exit")
fmt.Println("6. watch")
fmt.Println("0. exit")
fmt.Print("Enter command: ")
input, err := reader.ReadString('\n')
if err != nil {
Expand Down Expand Up @@ -89,6 +81,8 @@ func Start(mgr *manager.DownloadManager) {
continue
}
mgr.CancelDownload(id)
case "watch":
mgr.Watch()
case "exit":
fmt.Println("Exiting...")
os.Exit(0)
Expand Down

0 comments on commit 5116952

Please sign in to comment.