Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.

Commit 87d6bcf

Browse files
authored
Merge pull request #26 from qubixq/settings-download-dir
Add configurable download directory in settings menu - Ayarlar menüsüne düzenlenebilen indirme dizini eklendi
2 parents b7b05cf + 493f77e commit 87d6bcf

File tree

3 files changed

+128
-10
lines changed

3 files changed

+128
-10
lines changed

internal/utils/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Config struct {
1010
DefaultSource string `json:"default_source"`
1111
HistoryLimit int `json:"history_limit"`
1212
DisableRPC *bool `json:"disable_rpc"`
13+
DownloadDir string `json:"download_dir"`
1314
}
1415

1516
// LoadConfig config'i yükler
@@ -24,6 +25,10 @@ func LoadConfig(path string) (*Config, error) {
2425
if err := json.NewDecoder(file).Decode(&cfg); err != nil {
2526
return nil, err
2627
}
28+
29+
if cfg.DownloadDir == "" {
30+
cfg.DownloadDir = DefaultDownloadDir()
31+
}
2732

2833
return &cfg, nil
2934
}

internal/utils/utils.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,42 @@ func Ptr[T any](val T) *T {
139139
return &val
140140
}
141141

142-
// VideosDir, platforma göre Videos klasörünü döner
143142
func VideosDir() string {
143+
cfg, err := LoadConfig(filepath.Join(ConfigDir(), "config.json"))
144+
if err == nil && cfg.DownloadDir != "" {
145+
return cfg.DownloadDir
146+
}
147+
148+
// fallback → eski davranış
149+
if runtime.GOOS == "windows" {
150+
userProfile := os.Getenv("USERPROFILE")
151+
if userProfile == "" {
152+
userProfile = "."
153+
}
154+
return filepath.Join(userProfile, "Videos")
155+
} else {
156+
home := os.Getenv("HOME")
157+
if home == "" {
158+
home = "."
159+
}
160+
return filepath.Join(home, "Videos")
161+
}
162+
}
163+
164+
// DefaultDownloadDir işletim sistemine göre varsayılan indirme dizinini döner
165+
func DefaultDownloadDir() string {
144166
if runtime.GOOS == "windows" {
145-
// Kullanıcı profil dizini → %USERPROFILE%
146167
userProfile := os.Getenv("USERPROFILE")
147168
if userProfile == "" {
148169
userProfile = "." // fallback
149170
}
150-
return filepath.Join(userProfile, "Videos")
171+
return filepath.Join(userProfile, "Downloads", "anitr-cli")
151172
} else {
152-
// Linux / Mac
153173
home := os.Getenv("HOME")
154174
if home == "" {
155175
home = "."
156176
}
157-
return filepath.Join(home, "Videos")
177+
return filepath.Join(home, "Downloads", "anitr-cli")
158178
}
159179
}
160180

@@ -182,6 +202,7 @@ func ConfigDir() string {
182202
}
183203
}
184204

205+
185206
var episodeRegex = regexp.MustCompile(`(?i)(\d+(?:\.\d+)?)\s*\.?\s*Bölüm`)
186207

187208
func ExtractSeasonEpisode(title string) (episode float64, err error) {

main.go

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414
"time"
15+
"encoding/json"
1516

1617
"github.com/axrona/anitr-cli/internal"
1718
"github.com/axrona/anitr-cli/internal/dl"
@@ -274,7 +275,7 @@ func mainMenu(cfx *App, timestamp time.Time) {
274275
ui.ClearScreen()
275276

276277
// Menü seçenekleri
277-
menuOptions := []string{"Anime Ara", "Kaynak Değiştir", "Geçmiş", "Çık"}
278+
menuOptions := []string{"Anime Ara", "Kaynak Değiştir", "Geçmiş", "Ayarlar", "Çık"}
278279

279280
// Kullanıcıya mevcut kaynağı göster
280281
label := fmt.Sprintf("Kaynak: %s", *cfx.selectedSource)
@@ -400,12 +401,75 @@ func mainMenu(cfx *App, timestamp time.Time) {
400401
cfx.selectedSource = &newSelectedSource
401402
}
402403

404+
case "Ayarlar":
405+
settingsMenu(cfx)
406+
403407
case "Çık":
404408
os.Exit(0)
405409
}
406410
}
407411
}
408412

413+
func settingsMenu(cfx *App) {
414+
cfg, err := utils.LoadConfig(filepath.Join(utils.ConfigDir(), "config.json"))
415+
if err != nil {
416+
cfg = &utils.Config{}
417+
}
418+
419+
for {
420+
menuOptions := []string{
421+
fmt.Sprintf("İndirme dizinini değiştir : %s", cfg.DownloadDir),
422+
"Geri",
423+
}
424+
425+
selectedChoice, err := showSelection(*cfx, menuOptions, "Ayarlar")
426+
if errors.Is(err, tui.ErrGoBack) {
427+
return
428+
}
429+
if err != nil {
430+
cfx.logger.LogError(err)
431+
continue
432+
}
433+
434+
switch selectedChoice {
435+
case menuOptions[0]: // İndirme dizinini değiştir
436+
// varsayılan gösterimi ~/ ile
437+
homeDir := os.Getenv("HOME")
438+
displayDir := cfg.DownloadDir
439+
if strings.HasPrefix(cfg.DownloadDir, homeDir) {
440+
displayDir = "~" + cfg.DownloadDir[len(homeDir):]
441+
}
442+
443+
fmt.Printf("Yeni dizin (Enter ile değiştirme) [%s]: ", displayDir)
444+
var input string
445+
fmt.Scanln(&input)
446+
if input != "" {
447+
// Eğer input ~/ ile başlıyorsa HOME ile değiştir
448+
if strings.HasPrefix(input, "~") {
449+
input = filepath.Join(homeDir, input[1:])
450+
}
451+
cfg.DownloadDir = input
452+
453+
os.MkdirAll(utils.ConfigDir(), 0o755)
454+
f, err := os.Create(filepath.Join(utils.ConfigDir(), "config.json"))
455+
if err == nil {
456+
defer f.Close()
457+
enc := json.NewEncoder(f)
458+
enc.SetIndent("", " ")
459+
enc.Encode(cfg)
460+
}
461+
fmt.Println("Dizin güncellendi!")
462+
time.Sleep(1200 * time.Millisecond)
463+
ui.ClearScreen()
464+
}
465+
466+
case menuOptions[1]: // Geri
467+
return
468+
}
469+
}
470+
}
471+
472+
409473
// Anime geçmişini listeleyen fonksiyon
410474
func anitrHistory(params internal.UiParams, source string, historyLimit int, logger *utils.Logger) (selectedAnime string, animeId string, lastEpisodeIdx int, err error) {
411475
// Loading spinner başlat
@@ -1093,12 +1157,40 @@ func playAnimeLoop(
10931157
continue
10941158
}
10951159
selectedFansubIdx = slices.Index(fansubNames, selected)
1160+
1161+
1162+
// Movie / Bölüm indir
1163+
case "Bölüm indir", "Movie indir":
1164+
ui.ClearScreen()
1165+
1166+
cfg, err := utils.LoadConfig(filepath.Join(utils.ConfigDir(), "config.json"))
1167+
if err != nil {
1168+
cfg = &utils.Config{} // eğer config yoksa varsayılan config oluştur
1169+
}
10961170

1097-
// Movie / Bölüm indir
1098-
case "Bölüm indir", "Movie indir":
1099-
ui.ClearScreen()
1171+
if cfg.DownloadDir == "" {
1172+
defaultDir := utils.DefaultDownloadDir()
1173+
fmt.Printf("Videoları nereye indirmek istersiniz? (Varsayılan: %s): ", defaultDir)
1174+
var input string
1175+
fmt.Scanln(&input)
1176+
if input == "" {
1177+
input = defaultDir
1178+
}
1179+
cfg.DownloadDir = input
1180+
1181+
// Config dosyasına kaydet
1182+
os.MkdirAll(utils.ConfigDir(), 0o755)
1183+
f, err := os.Create(filepath.Join(utils.ConfigDir(), "config.json"))
1184+
if err == nil {
1185+
defer f.Close()
1186+
enc := json.NewEncoder(f)
1187+
enc.SetIndent("", " ")
1188+
enc.Encode(cfg)
1189+
}
1190+
}
11001191

1101-
downloader, err := dl.NewDownloader(filepath.Join(utils.VideosDir(), "anitr-cli"))
1192+
// Downloader için cfg.DownloadDir kullan
1193+
downloader, err := dl.NewDownloader(cfg.DownloadDir)
11021194
if err != nil {
11031195
switch {
11041196
case errors.Is(err, dl.ErrNoDownloader):

0 commit comments

Comments
 (0)