Skip to content

Commit

Permalink
feat: add a command for opening youtube page in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
sarowish committed Dec 13, 2022
1 parent a8b48d9 commit 4ae4030
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub enum Command {
RefreshChannel,
RefreshChannels,
RefreshFailedChannels,
OpenInBrowser,
OpenInInvidious,
OpenInYoutube,
PlayVideo,
ToggleWatched,
ToggleHelp,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/config/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 4ae4030

Please sign in to comment.