diff options
Diffstat (limited to 'src/fragment.rs')
-rw-r--r-- | src/fragment.rs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/fragment.rs b/src/fragment.rs new file mode 100644 index 0000000..8663fd2 --- /dev/null +++ b/src/fragment.rs @@ -0,0 +1,109 @@ +use crate::*; + + +pub struct InkedFragment { + pub string: String, + pub colour: ColorSpec, +} + +impl InkedFragment { + pub fn from(string: impl Into<String>) -> Self { + Self { + string: string.into(), + colour: ColorSpec::new(), + } + } + + pub fn to_string(self) -> InkedString { + let mut string = InkedString::new(); + string.push(self); + string + } + + pub fn print(self) { self.to_string().print(); } + pub fn println(self) { self.to_string().println(); } + pub fn eprint(self) { self.to_string().eprint(); } + pub fn eprintln(self) { self.to_string().eprintln(); } + + // ---------------------------------------- + + pub fn set_colour(mut self, colour: Option<Colour>) -> Self { + match colour { + Some(colour) => self.colour.set_fg(Some(colour.into())), + None => self.colour.set_fg(None), + }; + self + } + + pub fn set_bold(mut self, bold: bool) -> Self { + self.colour.set_bold(bold); + self + } + + pub fn set_dim(mut self, dim: bool) -> Self { + self.colour.set_dimmed(dim); + self + } + + // ---------------------------------------- + + pub fn black(mut self) -> Self { + self.colour.set_fg(Some(Color::Black)); + self + } + + pub fn red(mut self) -> Self { + self.colour.set_fg(Some(Color::Red)); + self + } + + pub fn green(mut self) -> Self { + self.colour.set_fg(Some(Color::Green)); + self + } + + pub fn yellow(mut self) -> Self { + self.colour.set_fg(Some(Color::Yellow)); + self + } + + pub fn blue(mut self) -> Self { + self.colour.set_fg(Some(Color::Blue)); + self + } + + pub fn magenta(mut self) -> Self { + self.colour.set_fg(Some(Color::Magenta)); + self + } + + pub fn cyan(mut self) -> Self { + self.colour.set_fg(Some(Color::Cyan)); + self + } + + pub fn white(mut self) -> Self { + self.colour.set_fg(Some(Color::White)); + self + } + + pub fn bold(mut self) -> Self { + self.colour.set_bold(true); + self + } + + pub fn dim(mut self) -> Self { + self.colour.set_dimmed(true); + self + } +} + + +impl From<String> for InkedFragment { + fn from(string: std::string::String) -> Self { + Self { + string, + colour: ColorSpec::new(), + } + } +} |