aboutsummaryrefslogtreecommitdiff
path: root/src/util/formatting/mod.rs
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2021-10-22 19:08:59 +0200
committerMax Audron <audron@cocaine.farm>2021-10-22 19:09:39 +0200
commit309899168a086de88acf97fd6683387a7af7078c (patch)
tree846075c1e9af0d7139edae5597f1147b851ed2b2 /src/util/formatting/mod.rs
parentremove wolfram alpha url shortening (diff)
write tons of documentation and reorganize some modules
Diffstat (limited to 'src/util/formatting/mod.rs')
-rw-r--r--src/util/formatting/mod.rs57
1 files changed, 52 insertions, 5 deletions
diff --git a/src/util/formatting/mod.rs b/src/util/formatting/mod.rs
index 5cfdfa8..a20b48f 100644
--- a/src/util/formatting/mod.rs
+++ b/src/util/formatting/mod.rs
@@ -1,9 +1,31 @@
+//! Tools for formatting irc messages
+
mod truncate;
mod color;
pub use truncate::*;
pub use color::*;
+/// Turn strings bold, italic,underline, strikethrough, and monospace.
+///
+/// The Formatting trait is implemented on `String` and `&str` for your convenience
+///
+/// # Format a string
+/// ```
+/// use catinator::util::Formatting;
+///
+/// let text = "this should be bold";
+/// assert_eq!("\x02this should be bold\x02", text.bold())
+/// ```
+///
+/// # Use raw formatting code
+/// ```
+/// use catinator::util::Formatting;
+///
+/// let text = "this should be bold";
+/// let result = format!("{}{}", String::BOLD, text);
+/// assert_eq!("\x02this should be bold", result)
+/// ```
pub trait Formatting<'r> {
const BOLD: &'r str = "\x02";
const ITALIC: &'r str = "\x1D";
@@ -12,11 +34,11 @@ pub trait Formatting<'r> {
const MONOSPACE: &'r str = "\x11";
const COLOR: &'r str = "\x03";
- fn bold(self) -> Self;
- fn italic(self) -> Self;
- fn underline(self) -> Self;
- fn strikethrough(self) -> Self;
- fn monospace(self) -> Self;
+ fn bold(self) -> String;
+ fn italic(self) -> String;
+ fn underline(self) -> String;
+ fn strikethrough(self) -> String;
+ fn monospace(self) -> String;
// fn color(self, color: Color) -> Self;
}
@@ -65,6 +87,31 @@ impl<'r> Formatting<'r> for String {
// }
}
+impl<'r> Formatting<'r> for &str {
+ fn bold(self) -> String {
+ format!("{}{}{}", Self::BOLD, self, Self::BOLD)
+ }
+
+ fn italic(self) -> String {
+ format!("{}{}{}", Self::ITALIC, self, Self::ITALIC)
+ }
+
+ fn underline(self) -> String {
+ format!("{}{}{}", Self::UNDERLINE, self, Self::UNDERLINE)
+ }
+
+ fn strikethrough(self) -> String {
+ format!("{}{}{}", Self::STRIKETHROUGH, self, Self::STRIKETHROUGH)
+ }
+
+ fn monospace(self) -> String {
+ format!("{}{}{}", Self::MONOSPACE, self, Self::MONOSPACE)
+ }
+
+ // TODO implement color codes
+ // fn color(mut self, foreground: Option<Color>, background: Option<Color>) -> Self { }
+}
+
#[cfg(all(test, feature = "bench"))]
mod bench {
use super::*;