Skip to content

Commit

Permalink
Add _line + _line_safe methods to MessageBuilder
Browse files Browse the repository at this point in the history
Add new methods to MessageBuilder to push content similar to the other
methods, except with the addition of appending a newline afterwards.

This should help prettify some MessageBuilder usage.
  • Loading branch information
Roughsketch authored and Zeyla Hellyer committed May 28, 2017
1 parent 71f3dbb commit 543b604
Showing 1 changed file with 250 additions and 0 deletions.
250 changes: 250 additions & 0 deletions src/utils/message_builder.rs
Expand Up @@ -341,6 +341,126 @@ impl MessageBuilder {
self
}

/// Pushes the given text with a newline appended to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_line("hello").push("world").build();
///
/// assert_eq!(content, "hello\nworld");
/// ```
pub fn push_line(mut self, content: &str) -> Self {
self = self.push(content);
self.0.push('\n');

self
}

/// Pushes inlined monospace text with an added newline to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_mono_line("hello").push("world").build();
///
/// assert_eq!(content, "`hello`\nworld");
/// ```
pub fn push_mono_line(mut self, content: &str) -> Self {
self = self.push_mono(content);
self.0.push('\n');

self
}

/// Pushes an inlined italicized text with an added newline to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_italic_line("hello").push("world").build();
///
/// assert_eq!(content, "_hello_\nworld");
/// ```
pub fn push_italic_line(mut self, content: &str) -> Self {
self = self.push_italic(content);
self.0.push('\n');

self
}

/// Pushes an inline bold text with an added newline to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_bold_line("hello").push("world").build();
///
/// assert_eq!(content, "**hello**\nworld");
/// ```
pub fn push_bold_line(mut self, content: &str) -> Self {
self = self.push_bold(content);
self.0.push('\n');

self
}

/// Pushes an underlined inline text with an added newline to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_underline_line("hello").push("world").build();
///
/// assert_eq!(content, "__hello__\nworld");
/// ```
pub fn push_underline_line(mut self, content: &str) -> Self {
self = self.push_underline(content);
self.0.push('\n');

self
}

/// Pushes a strikethrough inline text with a newline added to the content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_strike_line("hello").push("world").build();
///
/// assert_eq!(content, "~~hello~~\nworld");
/// ```
pub fn push_strike_line(mut self, content: &str) -> Self {
self = self.push_strike(content);
self.0.push('\n');

self
}

/// Pushes text to your message, but normalizing content - that means
/// ensuring that there's no unwanted formatting, mention spam etc.
pub fn push_safe(mut self, content: &str) -> Self {
Expand Down Expand Up @@ -418,6 +538,136 @@ impl MessageBuilder {
self
}

/// Pushes text with a newline appended to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new().push_line_safe("Hello @everyone").push("How are you?").build();
///
/// assert_eq!(content, "Hello @\u{200B}everyone\nHow are you?");
/// ```
pub fn push_line_safe(mut self, content: &str) -> Self {
self = self.push_safe(content);
self.0.push('\n');

self
}

/// Pushes an inline monospaced text with added newline to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new()
/// .push_mono_line_safe("`hello @everyone`")
/// .push("world").build();
///
/// assert_eq!(content, "`'hello @\u{200B}everyone'`\nworld");
/// ```
pub fn push_mono_line_safe(mut self, content: &str) -> Self {
self = self.push_mono_safe(content);
self.0.push('\n');

self
}

/// Pushes an inline italicized text with added newline to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new()
/// .push_italic_line_safe("@everyone")
/// .push("Isn't a mention.").build();
///
/// assert_eq!(content, "_@\u{200B}everyone_\nIsn't a mention.");
/// ```
pub fn push_italic_line_safe(mut self, content: &str) -> Self {
self = self.push_italic_safe(content);
self.0.push('\n');

self
}

/// Pushes an inline bold text with added newline to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new()
/// .push_bold_line_safe("@everyone")
/// .push("Isn't a mention.").build();
///
/// assert_eq!(content, "**@\u{200B}everyone**\nIsn't a mention.");
/// ```
pub fn push_bold_line_safe(mut self, content: &str) -> Self {
self = self.push_bold_safe(content);
self.0.push('\n');

self
}

/// Pushes an underlined inline text with added newline to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new()
/// .push_underline_line_safe("@everyone")
/// .push("Isn't a mention.").build();
///
/// assert_eq!(content, "__@\u{200B}everyone__\nIsn't a mention.");
/// ```
pub fn push_underline_line_safe(mut self, content: &str) -> Self {
self = self.push_underline_safe(content);
self.0.push('\n');

self
}

/// Pushes a strikethrough inline text with added newline to the content normalizing content.
///
/// # Examples
///
/// Push content and then append a newline:
///
/// ```rust
/// use serenity::utils::MessageBuilder;
///
/// let content = MessageBuilder::new()
/// .push_strike_line_safe("@everyone")
/// .push("Isn't a mention.").build();
///
/// assert_eq!(content, "~~@\u{200B}everyone~~\nIsn't a mention.");
/// ```
pub fn push_strike_line_safe(mut self, content: &str) -> Self {
self = self.push_strike_safe(content);
self.0.push('\n');

self
}

/// Mentions the [`Role`] in the built message.
///
/// This accepts anything that converts _into_ a [`RoleId`]. Refer to
Expand Down

0 comments on commit 543b604

Please sign in to comment.