summaryrefslogtreecommitdiff
path: root/src/string.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/string.rs')
-rw-r--r--src/string.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/string.rs b/src/string.rs
new file mode 100644
index 0000000..7b34fc8
--- /dev/null
+++ b/src/string.rs
@@ -0,0 +1,72 @@
+use crate::*;
+
+use std::io::Write;
+use std::sync::LazyLock;
+
+
+static STDOUT_WRITER: LazyLock<BufferWriter> = LazyLock::new(||
+ BufferWriter::stdout(ColorChoice::Auto)
+);
+
+static STDERR_WRITER: LazyLock<BufferWriter> = LazyLock::new(||
+ BufferWriter::stderr(ColorChoice::Auto)
+);
+
+
+pub struct InkedString {
+ pub fragments: Vec<InkedFragment>,
+}
+
+impl InkedString {
+ pub fn new() -> Self {
+ Self {
+ fragments: Vec::new(),
+ }
+ }
+
+ pub fn push(&mut self, fragment: impl Into<InkedFragment>) {
+ self.fragments.push(fragment.into());
+ }
+
+ pub fn append(&mut self, mut string: InkedString) {
+ self.fragments.append(&mut string.fragments);
+ }
+
+ pub fn print(mut self) {
+ self.push(ink!(""));
+ self.write_string(&STDOUT_WRITER)
+ }
+
+ pub fn println(mut self) {
+ self.push(ink!("\n"));
+ self.write_string(&STDOUT_WRITER)
+ }
+
+ pub fn eprint(mut self) {
+ self.push(ink!(""));
+ self.write_string(&STDERR_WRITER)
+ }
+
+ pub fn eprintln(mut self) {
+ self.push(ink!("\n"));
+ self.write_string(&STDERR_WRITER)
+ }
+
+ fn write_string(self, writer: &LazyLock<BufferWriter>) {
+ let mut buffer = writer.buffer();
+ for fragment in &self.fragments {
+ buffer.set_color(&fragment.colour).unwrap();
+ write!(buffer, "{}", fragment.string).unwrap();
+ }
+ writer.print(&buffer).unwrap()
+ }
+}
+
+
+impl From<String> for InkedString {
+ fn from(string: std::string::String) -> Self {
+ Self {
+ fragments: vec![ string.into() ],
+ }
+ }
+}