Commit c347aad699
Unsigned
Layout: unified · split
src/global/functions/run_command.rs +64 −9
| @@ -18,11 +18,9 @@ use tui_additions::framework::Framework; | |||
| 18 | 18 | ||
| 19 | /// runs text command - command from the command line (not TUI) which response is just a string | 19 | /// runs text command - command from the command line (not TUI) which response is just a string |
| 20 | pub fn text_command(command: &str) -> Option<String> { | 20 | pub fn text_command(command: &str) -> Option<String> { |
| 21 | match command | 21 | let command_parts: Vec<&str> = command.split_ascii_whitespace().collect(); |
| 22 | .split_ascii_whitespace() | 22 | |
| 23 | .collect::<Vec<_>>() | 23 | match command_parts.as_slice() { |
| 24 | .as_slice() | ||
| 25 | { | ||
| 26 | ["help"] => Some(help_msg( | 24 | ["help"] => Some(help_msg( |
| 27 | &CommandsRemapConfig::load(WriteConfig::Dont).unwrap(), | 25 | &CommandsRemapConfig::load(WriteConfig::Dont).unwrap(), |
| 28 | )), | 26 | )), |
| @@ -31,7 +29,64 @@ pub fn text_command(command: &str) -> Option<String> { | |||
| 31 | env!("CARGO_PKG_NAME"), | 29 | env!("CARGO_PKG_NAME"), |
| 32 | env!("CARGO_PKG_VERSION") | 30 | env!("CARGO_PKG_VERSION") |
| 33 | )), | 31 | )), |
| 34 | _ => None, | 32 | ["loadpage", page, ..] => { |
| 33 | // Validate loadpage commands from command line to prevent hanging in raw screen | ||
| 34 | match *page { | ||
| 35 | "popular" | "trending" | "watchhistory" | "feed" | "library" => { | ||
| 36 | if command_parts.len() != 2 { | ||
| 37 | return Some(format!("Usage: `loadpage {}`", page)); | ||
| 38 | } | ||
| 39 | None // Let it proceed to TUI | ||
| 40 | } | ||
| 41 | "channel" | "video" | "playlist" => { | ||
| 42 | if command_parts.len() != 3 { | ||
| 43 | return Some(format!("Usage: `loadpage {} {{id/url}}`", page)); | ||
| 44 | } | ||
| 45 | None // Let it proceed to TUI | ||
| 46 | } | ||
| 47 | "search" => { | ||
| 48 | if command_parts.len() < 3 { | ||
| 49 | return Some("Usage: `loadpage search {query}`".to_string()); | ||
| 50 | } | ||
| 51 | None // Let it proceed to TUI | ||
| 52 | } | ||
| 53 | _ => Some(format!("Unknown page: `{}`", page)), | ||
| 54 | } | ||
| 55 | } | ||
| 56 | _ => { | ||
| 57 | // Check if this is a remapped command that would become a loadpage command | ||
| 58 | if let Some(remapped_cmd) = CommandsRemapConfig::load(WriteConfig::Dont) | ||
| 59 | .unwrap() | ||
| 60 | .get(&command_parts) | ||
| 61 | { | ||
| 62 | // If it remaps to a loadpage command, validate it | ||
| 63 | if remapped_cmd.starts_with("loadpage ") { | ||
| 64 | let remapped_parts: Vec<&str> = remapped_cmd.split_ascii_whitespace().collect(); | ||
| 65 | if remapped_parts.len() >= 2 { | ||
| 66 | let page = remapped_parts[1]; | ||
| 67 | match page { | ||
| 68 | "popular" | "trending" | "watchhistory" | "feed" | "library" => { | ||
| 69 | if command_parts.len() != 1 { | ||
| 70 | return Some(format!("Usage: `{}`", command_parts[0])); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | "channel" | "video" | "playlist" => { | ||
| 74 | if command_parts.len() != 2 { | ||
| 75 | return Some(format!("Usage: `{} {{id/url}}`", command_parts[0])); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | "search" => { | ||
| 79 | if command_parts.len() < 2 { | ||
| 80 | return Some(format!("Usage: `{} {{query}}`", command_parts[0])); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | _ => {} | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | None | ||
| 89 | } | ||
| 35 | } | 90 | } |
| 36 | } | 91 | } |
| 37 | 92 | ||
| @@ -127,7 +182,7 @@ pub fn run_single_command( | |||
| 127 | "feed" => Some(Page::Feed), | 182 | "feed" => Some(Page::Feed), |
| 128 | "library" => Some(Page::MainMenu(MainMenuPage::Library)), | 183 | "library" => Some(Page::MainMenu(MainMenuPage::Library)), |
| 129 | "channel" => { | 184 | "channel" => { |
| 130 | if command.len() == 2 { | 185 | if command.len() != 3 { |
| 131 | *framework.data.global.get_mut::<Message>().unwrap() = | 186 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 132 | Message::Message(String::from("Usage: `loadpage channel {id/url}`")); | 187 | Message::Message(String::from("Usage: `loadpage channel {id/url}`")); |
| 133 | return; | 188 | return; |
| @@ -146,7 +201,7 @@ pub fn run_single_command( | |||
| 146 | } | 201 | } |
| 147 | } | 202 | } |
| 148 | "video" => { | 203 | "video" => { |
| 149 | if command.len() == 2 { | 204 | if command.len() != 3 { |
| 150 | *framework.data.global.get_mut::<Message>().unwrap() = | 205 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 151 | Message::Message(String::from("Usage: `loadpage video {id/url}`")); | 206 | Message::Message(String::from("Usage: `loadpage video {id/url}`")); |
| 152 | return; | 207 | return; |
| @@ -162,7 +217,7 @@ pub fn run_single_command( | |||
| 162 | } | 217 | } |
| 163 | } | 218 | } |
| 164 | "playlist" => { | 219 | "playlist" => { |
| 165 | if command.len() == 2 { | 220 | if command.len() != 3 { |
| 166 | *framework.data.global.get_mut::<Message>().unwrap() = | 221 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 167 | Message::Message(String::from("Usage: `loadpage playlist {id/url}`")); | 222 | Message::Message(String::from("Usage: `loadpage playlist {id/url}`")); |
| 168 | return; | 223 | return; |