diff --git a/radicle-cli/src/terminal/format.rs b/radicle-cli/src/terminal/format.rs index afebaf69..a21812aa 100644 --- a/radicle-cli/src/terminal/format.rs +++ b/radicle-cli/src/terminal/format.rs @@ -45,6 +45,35 @@ pub fn did(did: &Did) -> Paint { Paint::new(format!("{}…{}", &nid[..7], &nid[nid.len() - 7..])) } +/// Remove html style comments from a string. +/// +/// The html comments must start at the beginning of a line and stop at the end. +pub fn strip_comments(s: &str) -> String { + let ends_with_newline = s.ends_with('\n'); + let mut is_comment = false; + let mut w = String::new(); + + for line in s.lines() { + if is_comment { + if line.ends_with("-->") { + is_comment = false; + } + continue; + } else if line.starts_with(""; + let exp = "\ + commit 2\n\ + "; + + let res = strip_comments(test); + assert_eq!(exp, res); + + let test = "\ + commit 2\n\ + -->"; + let exp = "\ + commit 2\n\ + -->"; + + let res = strip_comments(test); + assert_eq!(exp, res); + + let test = "\ + \n\ + -->"; + let exp = "\ + commit 2\n\ + \n\ + -->"; + + let res = strip_comments(test); + assert_eq!(exp, res); + } +} diff --git a/radicle-cli/src/terminal/patch.rs b/radicle-cli/src/terminal/patch.rs index e69ccb6f..fde98e07 100644 --- a/radicle-cli/src/terminal/patch.rs +++ b/radicle-cli/src/terminal/patch.rs @@ -31,6 +31,7 @@ impl Message { Message::Text(c) => Some(c), }; let comment = comment.unwrap_or_default(); + let comment = term::format::strip_comments(&comment); let comment = comment.trim(); Ok(comment.to_owned()) @@ -87,7 +88,6 @@ pub fn get_message( let display_msg = default_msg.trim_end(); let message = message.get(&format!("{display_msg}\n{PATCH_MSG}"))?; - let message = message.replace(PATCH_MSG.trim(), ""); // Delete help message. let (title, description) = message.split_once('\n').unwrap_or((&message, "")); let (title, description) = (title.trim().to_string(), description.trim().to_string()); @@ -105,7 +105,6 @@ pub fn get_message( /// Get a patch update message. pub fn get_update_message(message: term::patch::Message) -> io::Result { let message = message.get(REVISION_MSG)?; - let message = message.replace(REVISION_MSG.trim(), ""); let message = message.trim(); Ok(message.to_owned())