blob: 7b34fc8a263e9f421495a16d94191918f9da0f72 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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() ],
}
}
}
|