Skip to main content

rustex_lib/engine/
output.rs

1use ansi_term::Color::{Black, Blue, Green, Red, White, Yellow};
2use std::{any::Any, fmt::Display};
3use tex_engine::engine::utils::outputs::Outputs;
4
5pub trait OutputCont: Any {
6    fn message(&self, text: String);
7    fn errmessage(&self, text: String);
8    fn file_open(&self, text: String);
9    fn file_close(&self, text: String);
10    fn write_18(&self, text: String);
11    fn write_17(&self, text: String);
12    fn write_16(&self, text: String);
13    fn write_neg1(&self, text: String);
14    fn write_other(&self, text: String);
15    fn as_any(self: Box<Self>) -> Box<dyn Any>;
16}
17
18pub enum RusTeXOutput {
19    Log(tex_engine::engine::utils::outputs::LogOutputs),
20    Print(bool),
21    None,
22    Cont(Box<dyn OutputCont>),
23}
24impl RusTeXOutput {
25    pub fn errmessage<D: Display>(&self, text: D) {
26        match self {
27            Self::Log(_) => log::info!(target:"errmessage::?","{}",text),
28            Self::Print(_) => {
29                println!("\n\n{}", Red.paint(text.to_string()));
30            }
31            Self::None => {}
32            Self::Cont(b) => b.errmessage(text.to_string()),
33        }
34    }
35}
36impl Outputs for RusTeXOutput {
37    fn new() -> Self {
38        Self::None
39    }
40
41    fn message<D: Display>(&self, text: D) {
42        match self {
43            Self::Log(l) => l.message(text),
44            Self::Print(_) => print!("{}", Yellow.paint(text.to_string())),
45            Self::None => {}
46            Self::Cont(b) => b.message(text.to_string()),
47        }
48    }
49
50    fn file_open<D: Display>(&self, text: D) {
51        match self {
52            Self::Log(l) => l.file_open(text),
53            Self::Print(_) => print!("\n({}", text),
54            Self::None => {}
55            Self::Cont(b) => b.file_open(text.to_string()),
56        }
57    }
58
59    fn file_close<D: Display>(&self, text: D) {
60        match self {
61            Self::Log(l) => l.file_close(""),
62            Self::Print(_) => print!(")"),
63            Self::None => {}
64            Self::Cont(b) => b.file_close(text.to_string()),
65        }
66    }
67
68    fn write_18<D: Display>(&self, text: D) {
69        match self {
70            Self::Log(l) => l.write_18(text),
71            Self::Print(_) => (),
72            Self::None => (),
73            Self::Cont(b) => b.write_18(text.to_string()),
74        }
75    }
76
77    fn write_17<D: Display>(&self, text: D) {
78        match self {
79            Self::Log(l) => l.write_17(text),
80            Self::Print(_) => print!("{}", text),
81            Self::None => {}
82            Self::Cont(b) => b.write_17(text.to_string()),
83        }
84    }
85
86    fn write_16<D: Display>(&self, text: D) {
87        match self {
88            Self::Log(l) => l.write_16(text),
89            Self::Print(_) => print!("\n{}", White.bold().paint(text.to_string())),
90            Self::None => {}
91            Self::Cont(b) => b.write_16(text.to_string()),
92        }
93    }
94
95    fn write_neg1<D: Display>(&self, text: D) {
96        match self {
97            Self::Log(l) => l.write_neg1(text),
98            Self::Print(true) => print!("\n{}", Black.on(Blue).paint(text.to_string())),
99            Self::None | Self::Print(_) => {}
100            Self::Cont(b) => b.write_neg1(text.to_string()),
101        }
102    }
103
104    fn write_other<D: Display>(&self, text: D) {
105        match self {
106            Self::Log(l) => l.write_other(text),
107            Self::Print(_) => print!("\n{}", Black.on(Green).paint(text.to_string())),
108            Self::None => {}
109            Self::Cont(b) => b.write_other(text.to_string()),
110        }
111    }
112}