Skip to content

Commit

Permalink
feat ✨: add flag to keep menu open, close #60
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Hang <[email protected]>
  • Loading branch information
Banh-Canh committed Jan 2, 2025
1 parent ae32b95 commit 40a3b61
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 126 deletions.
2 changes: 2 additions & 0 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var (
downloadFlag bool
keepOpenFlag bool
downloadDirFlag string
)

Expand All @@ -24,5 +25,6 @@ Run one of the available subcommands.`,
func init() {
RootCmd.AddCommand(queryCmd)
queryCmd.PersistentFlags().BoolVarP(&downloadFlag, "download", "d", false, "Download the selected video instead of watching it")
queryCmd.PersistentFlags().BoolVarP(&keepOpenFlag, "keep-open", "", false, "Reopen the menu after selecting the player")
queryCmd.PersistentFlags().StringVarP(&downloadDirFlag, "download-dir", "o", "", "Set download directory")
}
77 changes: 43 additions & 34 deletions cmd/query_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,55 @@ will be stored in there.`,
os.Exit(0)
}

utils.Logger.Info("Videos found in history.", zap.Int("video_count", len(result)))
fmt.Printf("Found %d videos in your history.\n", len(result))
for {
utils.Logger.Info("Videos found in history.", zap.Int("video_count", len(result)))
fmt.Printf("Found %d videos in your history.\n", len(result))

// Show the FZF menu to select a video
selectedVideo, err := youtube.YoutubeResultMenu(result, viper.GetString("invidious.instance"), viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("History search cancelled.")
os.Exit(0)
}
// Show the FZF menu to select a video
selectedVideo, err := youtube.YoutubeResultMenu(
result,
viper.GetString("invidious.instance"),
viper.GetString("invidious.proxy"),
)
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("History search cancelled.")
os.Exit(0)
}

// Construct the video URL for the selected video
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)
// Construct the video URL for the selected video
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)

// Handle download or playback
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
// Handle download or playback
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")

player.RunMPV(videoURL)
player.RunMPV(videoURL)

// Add to watch history if history is enabled
if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
// Add to watch history if history is enabled
if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
}
}
if !keepOpenFlag {
break
}
}
},
Expand Down
83 changes: 46 additions & 37 deletions cmd/query_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,55 @@ Press enter to run any of the videos.`,
os.Exit(0)
}

utils.Logger.Info("Videos found.", zap.Int("video_count", len(*result)))
fmt.Printf("Found %d videos for query: %s\n", len(*result), query)

// Display search results in FZF menu
selectedVideo, err := youtube.YoutubeResultMenu(*result, viper.GetString("invidious.instance"), viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("Search cancelled.")
os.Exit(0)
}

// Build the video URL
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)
for {
utils.Logger.Info("Videos found.", zap.Int("video_count", len(*result)))
fmt.Printf("Found %d videos for query: %s\n", len(*result), query)

// Display search results in FZF menu
selectedVideo, err := youtube.YoutubeResultMenu(
*result,
viper.GetString("invidious.instance"),
viper.GetString("invidious.proxy"),
)
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("Search cancelled.")
os.Exit(0)
}

// Handle video playback or download
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
// Build the video URL
videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)

// Handle video playback or download
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")

player.RunMPV(videoURL)

// Add to watch history if enabled
if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
}
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")

player.RunMPV(videoURL)

// Add to watch history if enabled
if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
if !keepOpenFlag {
break
}
}
},
Expand Down
77 changes: 43 additions & 34 deletions cmd/query_subscribed.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,52 @@ It will also only pick from the 50 most relevants subscribed channels in your Yo
os.Exit(1)
}
}

utils.Logger.Info("Retrieved videos from subscribed channels.", zap.Int("video_count", len(*result)))
fmt.Printf("Found %d videos from subscribed channels.\n", len(*result))

selectedVideo, err := youtube.YoutubeResultMenu(*result, viper.GetString("invidious.instance"), viper.GetString("invidious.proxy"))
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("Video selection cancelled.")
os.Exit(0)
}
utils.Logger.Info("Selected video for playback.", zap.String("video_id", selectedVideo.VideoID))
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)

videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
for {
utils.Logger.Info("Retrieved videos from subscribed channels.", zap.Int("video_count", len(*result)))
fmt.Printf("Found %d videos from subscribed channels.\n", len(*result))

selectedVideo, err := youtube.YoutubeResultMenu(
*result,
viper.GetString("invidious.instance"),
viper.GetString("invidious.proxy"),
)
if err != nil {
utils.Logger.Info("FZF menu closed.")
fmt.Println("Video selection cancelled.")
os.Exit(0)
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")
utils.Logger.Info("Selected video for playback.", zap.String("video_id", selectedVideo.VideoID))
fmt.Printf("Selected video: %s\n", selectedVideo.VideoID)

videoURL := "https://www.youtube.com/watch?v=" + selectedVideo.VideoID
if downloadFlag {
var downloadDirStr string
if downloadDirFlag != "" {
downloadDirStr = downloadDirFlag // Use the flag if set
} else {
downloadDirStr = viper.GetString("download_dir") // Use config value if flag is not set
}
utils.Logger.Info("Downloading selected video with yt-dlp.", zap.String("video_url", videoURL))
downloadDir := downloadDirStr
fmt.Println("Downloading selected video...")
download.RunYTDLP(videoURL, downloadDir)
fmt.Println("Download completed.")
} else {
utils.Logger.Info("Playing selected video in MPV.", zap.String("video_url", videoURL))
fmt.Println("Playing selected video in MPV...")

player.RunMPV(videoURL)
player.RunMPV(videoURL)

if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
if viper.GetBool("history.enable") {
historyFilePath := filepath.Join(configDir, "watched_history.json")
youtube.FeedHistory(selectedVideo, historyFilePath)
utils.Logger.Info("Video added to watch history.", zap.String("video_id", selectedVideo.VideoID))
fmt.Println("Video added to watch history.")
}

}
if !keepOpenFlag {
break
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion docs/ytui.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ytui [flags]
* [ytui query](ytui_query.md) - Run queries for videos through different patterns
* [ytui show](ytui_show.md) - show items through different patterns

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
8 changes: 5 additions & 3 deletions docs/ytui_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ Run one of the available subcommands.
### Options

```
-d, --download Download the selected video instead of watching it
-h, --help help for query
-d, --download Download the selected video instead of watching it
-o, --download-dir string Set download directory
-h, --help help for query
--keep-open Reopen the menu after selecting the player
```

### Options inherited from parent commands
Expand All @@ -29,4 +31,4 @@ Run one of the available subcommands.
* [ytui query search](ytui_query_search.md) - Search for videos on Youtube/Invidious using keywords
* [ytui query subscribed](ytui_query_subscribed.md) - Search for videos from your subscribed channels

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
8 changes: 5 additions & 3 deletions docs/ytui_query_history.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ ytui query history [flags]
### Options inherited from parent commands

```
-d, --download Download the selected video instead of watching it
-l, --log-level string Override log level (debug, info, error)
-d, --download Download the selected video instead of watching it
-o, --download-dir string Set download directory
--keep-open Reopen the menu after selecting the player
-l, --log-level string Override log level (debug, info, error)
```

### SEE ALSO

* [ytui query](ytui_query.md) - Run queries for videos through different patterns

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
8 changes: 5 additions & 3 deletions docs/ytui_query_search.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ ytui query search <text> [flags]
### Options inherited from parent commands

```
-d, --download Download the selected video instead of watching it
-l, --log-level string Override log level (debug, info, error)
-d, --download Download the selected video instead of watching it
-o, --download-dir string Set download directory
--keep-open Reopen the menu after selecting the player
-l, --log-level string Override log level (debug, info, error)
```

### SEE ALSO

* [ytui query](ytui_query.md) - Run queries for videos through different patterns

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
8 changes: 5 additions & 3 deletions docs/ytui_query_subscribed.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ ytui query subscribed [flags]
### Options inherited from parent commands

```
-d, --download Download the selected video instead of watching it
-l, --log-level string Override log level (debug, info, error)
-d, --download Download the selected video instead of watching it
-o, --download-dir string Set download directory
--keep-open Reopen the menu after selecting the player
-l, --log-level string Override log level (debug, info, error)
```

### SEE ALSO

* [ytui query](ytui_query.md) - Run queries for videos through different patterns

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
4 changes: 2 additions & 2 deletions docs/ytui_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Run one of the available subcommands.
### SEE ALSO

* [ytui](ytui.md) - ytui TUI.
* [ytui show subscribed](ytui_show_subscribed.md) - show subscribed channels
* [ytui show subscribed](ytui_show_subscribed.md) - Show subscribed channels

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025
12 changes: 6 additions & 6 deletions docs/ytui_show_subscribed.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
## ytui show subscribed

show subscribed channels
Show subscribed channels

### Synopsis


show your subscribed channels.
If you set the configuration "local: false" in the configuration file, it will prompt you to google login,
to retrieve your user informations. You must also configure your OAuth2 client in Google Dev Console first.
Show your subscribed channels.
If you set the configuration "local: false" in the configuration file, it will prompt you to Google login
to retrieve your user information. You must also configure your OAuth2 client in Google Dev Console first.

It will also only pick from the 50 most relevants subscribed channels in your Youtube account.
It will also only pick from the 50 most relevant subscribed channels in your Youtube account.

```
ytui show subscribed [flags]
Expand All @@ -31,4 +31,4 @@ ytui show subscribed [flags]

* [ytui show](ytui_show.md) - show items through different patterns

###### Auto generated by spf13/cobra on 30-Sep-2024
###### Auto generated by spf13/cobra on 2-Jan-2025

0 comments on commit 40a3b61

Please sign in to comment.