diff --git a/src/main.rs b/src/main.rs index cd97b1f7..6d835b4c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::{ }; use term::{clear_terminal, press_enter_prompt}; -use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile}; +use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, url_replacer::UrlReplacer}; mod app_state; mod cargo_toml; @@ -22,6 +22,7 @@ mod list; mod run; mod term; mod watch; +mod url_replacer; const CURRENT_FORMAT_VERSION: u8 = 1; @@ -100,6 +101,8 @@ fn main() -> Result { info_file.final_message.unwrap_or_default(), )?; + let replacer = UrlReplacer::new(&args.base_url); + // Show the welcome message if the state file doesn't exist yet. if let Some(welcome_message) = info_file.welcome_message { match state_file_status { @@ -184,7 +187,9 @@ fn main() -> Result { if let Some(name) = name { app_state.set_current_exercise_by_name(&name)?; } - println!("{}", app_state.current_exercise().hint); + + let hint = app_state.current_exercise().hint; + println!("{}", replacer.replace(hint)); } // Handled in an earlier match. Some(Subcommands::Init | Subcommands::Dev(_)) => (), diff --git a/src/url_replacer.rs b/src/url_replacer.rs new file mode 100644 index 00000000..5b63e390 --- /dev/null +++ b/src/url_replacer.rs @@ -0,0 +1,21 @@ +pub struct UrlReplacer <'a> { + base_url: &'a Option +} + +const EN_BASE_URL: &str = "https://doc.rust-lang.org/book"; + +impl <'a> UrlReplacer <'a> { + pub fn new(base_url: &'a Option) -> Self { + Self { + base_url + } + } + + pub fn replace(&self, hint: &str) -> String { + if let Some(base_url) = self.base_url { + hint.replace(EN_BASE_URL, base_url) + } else { + hint.to_owned() + } + } +}