From 4ae403086e59d575bf25e16466aee50874da1a13 Mon Sep 17 00:00:00 2001 From: Berke Enercan Date: Tue, 13 Dec 2022 10:06:30 +0300 Subject: [PATCH] feat: add a command for opening youtube page in browser --- CHANGELOG.md | 1 + README.md | 3 ++- src/app.rs | 29 +++++++++++++++++++++++++++-- src/commands.rs | 6 ++++-- src/config/keys.rs | 3 ++- src/help.rs | 5 +++-- src/input.rs | 3 ++- 7 files changed, 41 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9792eb..0d4b2b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Fetch instances from api.invidious.io after starting the tui. - Hide barely visable columns. +- `O` key binding opens the channel or video Youtube page in browser. ### Changed - Change `modify channels` help text to `pick channels` diff --git a/README.md b/README.md index 503b707..ebe8d96 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,8 @@ fg = "Green" "r" = "refresh_channel" # Refresh videos of the selected channel "R" = "refresh_channels" # Refresh videos of every channel "F" = "refresh_failed_channels" # Refresh videos of channels which their latest refresh was a failure -"o" = "open_in_browser" # Open channel or video in browser +"o" = "open_in_invidious" # Open channel or video Invidious page in browser +"O" = "open_in_youtube" # Open channel or video Youtube page in browser "p" = "play_video" # Play selected video in a video player (default: mpv) "m" = "toggle_watched" # Mark/unmark selected video as watched "ctrl-h" = "toggle_help" # Toggle help window diff --git a/src/app.rs b/src/app.rs index efcd0fa..0378a1a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -381,7 +381,7 @@ impl App { } } - pub fn open_in_browser(&mut self) { + pub fn open_in_invidious(&mut self) { let Some(instance) = &self.instance else { self.set_error_message("No Invidious instances available."); return; @@ -402,7 +402,32 @@ impl App { }, }; - let browser_process = || webbrowser::open(&url); + self.open_in_browser(&url); + } + + pub fn open_in_youtube(&mut self) { + const YOUTUBE_URL: &str = "https://www.youtube.com"; + + let url = match self.selected { + Selected::Channels => match self.get_current_channel() { + Some(current_channel) => { + format!("{}/channel/{}", YOUTUBE_URL, current_channel.channel_id) + } + None => return, + }, + Selected::Videos => match self.get_current_video() { + Some(current_video) => { + format!("{}/watch?v={}", YOUTUBE_URL, current_video.video_id) + } + None => return, + }, + }; + + self.open_in_browser(&url); + } + + pub fn open_in_browser(&mut self, url: &str) { + let browser_process = || webbrowser::open(url); #[cfg(unix)] let res = self.run_detached(browser_process); diff --git a/src/commands.rs b/src/commands.rs index cd15eac..b28f404 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -20,7 +20,8 @@ pub enum Command { RefreshChannel, RefreshChannels, RefreshFailedChannels, - OpenInBrowser, + OpenInInvidious, + OpenInYoutube, PlayVideo, ToggleWatched, ToggleHelp, @@ -53,7 +54,8 @@ impl TryFrom<&str> for Command { "refresh_channel" => Command::RefreshChannel, "refresh_channels" => Command::RefreshChannels, "refresh_failed_channels" => Command::RefreshFailedChannels, - "open_in_browser" => Command::OpenInBrowser, + "open_in_invidious" => Command::OpenInInvidious, + "open_in_youtube" => Command::OpenInYoutube, "play_video" => Command::PlayVideo, "toggle_watched" => Command::ToggleWatched, "toggle_help" => Command::ToggleHelp, diff --git a/src/config/keys.rs b/src/config/keys.rs index 5c772f6..566c52b 100644 --- a/src/config/keys.rs +++ b/src/config/keys.rs @@ -102,7 +102,8 @@ impl Default for KeyBindings { insert_binding!(general, "r", Command::RefreshChannel); insert_binding!(general, "R", Command::RefreshChannels); insert_binding!(general, "F", Command::RefreshFailedChannels); - insert_binding!(general, "o", Command::OpenInBrowser); + insert_binding!(general, "o", Command::OpenInInvidious); + insert_binding!(general, "O", Command::OpenInYoutube); insert_binding!(general, "p", Command::PlayVideo); insert_binding!(general, "m", Command::ToggleWatched); insert_binding!(general, "ctrl-h", Command::ToggleHelp); diff --git a/src/help.rs b/src/help.rs index b9152d5..dce1ca4 100644 --- a/src/help.rs +++ b/src/help.rs @@ -2,7 +2,7 @@ use crate::KEY_BINDINGS; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use std::ops::{Deref, DerefMut}; -const DESCRIPTIONS_LEN: usize = 26; +const DESCRIPTIONS_LEN: usize = 27; const DESCRIPTIONS: [&str; DESCRIPTIONS_LEN] = [ "Switch to subscriptions mode", "Switch to latest videos mode", @@ -24,7 +24,8 @@ const DESCRIPTIONS: [&str; DESCRIPTIONS_LEN] = [ "Refresh videos of the selected channel", "Refresh videos of every channel", "Refresh videos of channels which their latest refresh was a failure", - "Open channel or video in browser", + "Open channel or video Invidious page in browser", + "Open channel or video Youtube page in browser", "Play video in video player", "Mark/unmark video as watched", "Toggle help window", diff --git a/src/input.rs b/src/input.rs index d6c6a52..9b8fe3b 100644 --- a/src/input.rs +++ b/src/input.rs @@ -58,7 +58,8 @@ fn handle_key_normal_mode(key: KeyEvent, app: &mut App) -> bool { Command::RefreshChannel => app.refresh_channel(), Command::RefreshChannels => app.refresh_channels(), Command::RefreshFailedChannels => app.refresh_failed_channels(), - Command::OpenInBrowser => app.open_in_browser(), + Command::OpenInInvidious => app.open_in_invidious(), + Command::OpenInYoutube => app.open_in_youtube(), Command::PlayVideo => app.play_video(), Command::ToggleWatched => app.toggle_watched(), Command::ToggleHelp => app.toggle_help(),