diff --git a/src/formattedtext/formattedtext.rs b/src/formattedtext/formattedtext.rs new file mode 100644 index 0000000..45b5a5d --- /dev/null +++ b/src/formattedtext/formattedtext.rs @@ -0,0 +1,33 @@ +use std::ops::Add; + + +#[derive(Debug)] +#[derive(Clone)] +pub struct FormattedText { + pub(super) text: String +} + +impl ToString for FormattedText { + fn to_string(&self) -> String { return self.text.clone(); } +} + +impl FormattedText { + pub fn new(s: String) -> FormattedText { + return FormattedText { + text: s + } + } + + pub fn push(&mut self, s: &str) { + self.text.push_str(s); + } +} + + +impl Add for FormattedText { + type Output = Self; + + fn add(self, other: Self) -> Self::Output { + return FormattedText::new(format!("{}{}", self.text, other.text)); + } +} \ No newline at end of file diff --git a/src/formattedtext/mod.rs b/src/formattedtext/mod.rs new file mode 100644 index 0000000..d30b4f7 --- /dev/null +++ b/src/formattedtext/mod.rs @@ -0,0 +1,12 @@ +mod formattedtext; +pub use formattedtext::FormattedText; + + +// Select write implementation by target system +cfg_if::cfg_if! { + if #[cfg(target_family = "unix")] { + mod unix_backend; + } else if #[cfg(target_arch = "wasm32")] { + mod wasm_backend; + } +} \ No newline at end of file diff --git a/src/formattedtext.rs b/src/formattedtext/unix_backend.rs similarity index 87% rename from src/formattedtext.rs rename to src/formattedtext/unix_backend.rs index ed7c6ef..857ffed 100644 --- a/src/formattedtext.rs +++ b/src/formattedtext/unix_backend.rs @@ -1,34 +1,12 @@ +use super::FormattedText; use std::io::Write; +use crate::context::Context; + use termion::raw::RawTerminal; use termion::color; use termion::style; use termion::clear; use termion::cursor; -use std::ops::Add; -use crate::context::Context; - - -#[derive(Debug)] -#[derive(Clone)] -pub struct FormattedText { - text: String -} - -impl ToString for FormattedText { - fn to_string(&self) -> String { return self.text.clone(); } -} - - -fn format_map_none(c: char) -> Option { - Some(match c { - 'n'|'i'|'t'|'a'| - 'e'|'c'|'s'|'r'| - 'p' - => { "".to_string() }, - _ => { return None } - }) -} - fn format_map_ansi(c: char) -> Option { Some(match c { @@ -65,6 +43,17 @@ fn format_map_ansi(c: char) -> Option { } + +fn format_map_none(c: char) -> Option { + Some(match c { + 'n'|'i'|'t'|'a'| + 'e'|'c'|'s'|'r'| + 'p' + => { "".to_string() }, + _ => { return None } + }) +} + // style::reset also resets color. // Make sure color comes AFTER style reset. fn format_map_full(c: char) -> Option { @@ -102,9 +91,6 @@ fn format_map_full(c: char) -> Option { }) } - - - impl FormattedText { pub fn newline(stdout: &mut RawTerminal) -> Result<(), std::io::Error> { write!(stdout, "\n")?; @@ -119,20 +105,6 @@ impl FormattedText { _ => unreachable!("Invalid term_color_type") } } -} - - -impl FormattedText { - pub fn new(s: String) -> FormattedText { - return FormattedText { - text: s - } - } - - pub fn push(&mut self, s: &str) { - self.text.push_str(s); - } - pub fn write(&self, context: &Context, stdout: &mut RawTerminal) -> Result<(), std::io::Error> { @@ -155,7 +127,7 @@ impl FormattedText { '[' => { let a = chars.next().unwrap(); - // Handle double [[ as escaped [ + // Treat double [[ as escaped [ if a == '[' { s.push('['); } let b = chars.next().unwrap(); @@ -189,13 +161,4 @@ impl FormattedText { write!(stdout, "\r{}", s)?; return Ok(()); } -} - - -impl Add for FormattedText { - type Output = Self; - - fn add(self, other: Self) -> Self::Output { - return FormattedText::new(format!("{}{}", self.text, other.text)); - } } \ No newline at end of file diff --git a/src/formattedtext/wasm_backend.rs b/src/formattedtext/wasm_backend.rs new file mode 100644 index 0000000..8235705 --- /dev/null +++ b/src/formattedtext/wasm_backend.rs @@ -0,0 +1,12 @@ +use super::FormattedText; + +impl FormattedText { + pub fn newline() -> Result<(), ()> { + print!("\n"); + return Ok(()); + } + + pub fn write(&self) -> String { + return self.text.clone(); + } +} \ No newline at end of file