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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
//! Renders the preview SVG for the README.
//!
//! To update the preview, execute the following command from the top level of
//! the repository:
//!
//! ```sh
//! cargo run --example readme_preview svg > codespan-reporting/assets/readme_preview.svg
//! ```
use codespan_reporting::diagnostic::{Diagnostic, Label};
use codespan_reporting::files::SimpleFile;
use codespan_reporting::term::termcolor::{Color, ColorSpec, StandardStream, WriteColor};
use codespan_reporting::term::{self, ColorArg};
use std::io::{self, Write};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "emit")]
pub enum Opts {
/// Render SVG output
Svg,
/// Render Stderr output
Stderr {
/// Configure coloring of output
#[structopt(
long = "color",
parse(try_from_str),
default_value = "auto",
possible_values = ColorArg::VARIANTS,
case_insensitive = true
)]
color: ColorArg,
},
}
fn main() -> anyhow::Result<()> {
let file = SimpleFile::new(
"FizzBuzz.fun",
unindent::unindent(
r#"
module FizzBuzz where
fizz₁ : Nat → String
fizz₁ num = case (mod num 5) (mod num 3) of
0 0 => "FizzBuzz"
0 _ => "Fizz"
_ 0 => "Buzz"
_ _ => num
fizz₂ : Nat → String
fizz₂ num =
case (mod num 5) (mod num 3) of
0 0 => "FizzBuzz"
0 _ => "Fizz"
_ 0 => "Buzz"
_ _ => num
"#,
),
);
let diagnostics = [Diagnostic::error()
.with_message("`case` clauses have incompatible types")
.with_code("E0308")
.with_labels(vec![
Label::primary((), 328..331).with_message("expected `String`, found `Nat`"),
Label::secondary((), 211..331).with_message("`case` clauses have incompatible types"),
Label::secondary((), 258..268).with_message("this is found to be of type `String`"),
Label::secondary((), 284..290).with_message("this is found to be of type `String`"),
Label::secondary((), 306..312).with_message("this is found to be of type `String`"),
Label::secondary((), 186..192).with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent(
"
expected type `String`
found type `Nat`
",
)])];
// let mut files = SimpleFiles::new();
match Opts::from_args() {
Opts::Svg => {
let mut buffer = Vec::new();
let mut writer = HtmlEscapeWriter::new(SvgWriter::new(&mut buffer));
let config = codespan_reporting::term::Config {
styles: codespan_reporting::term::Styles::with_blue(Color::Blue),
..codespan_reporting::term::Config::default()
};
for diagnostic in &diagnostics {
term::emit(&mut writer, &config, &file, &diagnostic)?;
}
let num_lines = buffer.iter().filter(|byte| **byte == b'\n').count() + 1;
let padding = 10;
let font_size = 12;
let line_spacing = 3;
let width = 882;
let height = padding + num_lines * (font_size + line_spacing) + padding;
let stdout = std::io::stdout();
let writer = &mut stdout.lock();
write!(
writer,
r#"<svg viewBox="0 0 {width} {height}" xmlns="http://www.w3.org/2000/svg">
<style>
/* https://github.com/aaron-williamson/base16-alacritty/blob/master/colors/base16-tomorrow-night-256.yml */
pre {{
background: #1d1f21;
margin: 0;
padding: {padding}px;
border-radius: 6px;
color: #ffffff;
font: {font_size}px SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
}}
pre .bold {{ font-weight: bold; }}
pre .fg.black {{ color: #1d1f21; }}
pre .fg.red {{ color: #cc6666; }}
pre .fg.green {{ color: #b5bd68; }}
pre .fg.yellow {{ color: #f0c674; }}
pre .fg.blue {{ color: #81a2be; }}
pre .fg.magenta {{ color: #b294bb; }}
pre .fg.cyan {{ color: #8abeb7; }}
pre .fg.white {{ color: #c5c8c6; }}
pre .fg.black.bright {{ color: #969896; }}
pre .fg.red.bright {{ color: #cc6666; }}
pre .fg.green.bright {{ color: #b5bd68; }}
pre .fg.yellow.bright {{ color: #f0c674; }}
pre .fg.blue.bright {{ color: #81a2be; }}
pre .fg.magenta.bright {{ color: #b294bb; }}
pre .fg.cyan.bright {{ color: #8abeb7; }}
pre .fg.white.bright {{ color: #ffffff; }}
pre .bg.black {{ background-color: #1d1f21; }}
pre .bg.red {{ background-color: #cc6666; }}
pre .bg.green {{ background-color: #b5bd68; }}
pre .bg.yellow {{ background-color: #f0c674; }}
pre .bg.blue {{ background-color: #81a2be; }}
pre .bg.magenta {{ background-color: #b294bb; }}
pre .bg.cyan {{ background-color: #8abeb7; }}
pre .bg.white {{ background-color: #c5c8c6; }}
pre .bg.black.bright {{ background-color: #969896; }}
pre .bg.red.bright {{ background-color: #cc6666; }}
pre .bg.green.bright {{ background-color: #b5bd68; }}
pre .bg.yellow.bright {{ background-color: #f0c674; }}
pre .bg.blue.bright {{ background-color: #81a2be; }}
pre .bg.magenta.bright {{ background-color: #b294bb; }}
pre .bg.cyan.bright {{ background-color: #8abeb7; }}
pre .bg.white.bright {{ background-color: #ffffff; }}
</style>
<foreignObject x="0" y="0" width="{width}" height="{height}">
<div xmlns="http://www.w3.org/1999/xhtml">
<pre>"#,
padding = padding,
font_size = font_size,
width = width,
height = height,
)?;
writer.write_all(&buffer)?;
write!(
writer,
"</pre>
</div>
</foreignObject>
</svg>
"
)?;
}
Opts::Stderr { color } => {
let writer = StandardStream::stderr(color.into());
let config = codespan_reporting::term::Config::default();
for diagnostic in &diagnostics {
term::emit(&mut writer.lock(), &config, &file, &diagnostic)?;
}
}
}
Ok(())
}
/// Rudimentary HTML escaper which performs the following conversions:
///
/// - `<` ⇒ `<`
/// - `>` ⇒ `>`
/// - `&` ⇒ `&`
pub struct HtmlEscapeWriter<W> {
upstream: W,
}
impl<W> HtmlEscapeWriter<W> {
pub fn new(upstream: W) -> HtmlEscapeWriter<W> {
HtmlEscapeWriter { upstream }
}
}
impl<W: Write> Write for HtmlEscapeWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut last_term = 0usize;
for (i, byte) in buf.iter().enumerate() {
let escape = match byte {
b'<' => &b"<"[..],
b'>' => &b">"[..],
b'&' => &b"&"[..],
_ => continue,
};
self.upstream.write_all(&buf[last_term..i])?;
last_term = i + 1;
self.upstream.write_all(escape)?;
}
self.upstream.write_all(&buf[last_term..])?;
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
self.upstream.flush()
}
}
impl<W: WriteColor> WriteColor for HtmlEscapeWriter<W> {
fn supports_color(&self) -> bool {
self.upstream.supports_color()
}
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
self.upstream.set_color(spec)
}
fn reset(&mut self) -> io::Result<()> {
self.upstream.reset()
}
}
pub struct SvgWriter<W> {
upstream: W,
color: ColorSpec,
}
impl<W> SvgWriter<W> {
pub fn new(upstream: W) -> SvgWriter<W> {
SvgWriter {
upstream,
color: ColorSpec::new(),
}
}
}
impl<W: Write> Write for SvgWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.upstream.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.upstream.flush()
}
}
impl<W: Write> WriteColor for SvgWriter<W> {
fn supports_color(&self) -> bool {
true
}
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
#![allow(unused_assignments)]
if self.color == *spec {
return Ok(());
} else {
if !self.color.is_none() {
write!(self, "</span>")?;
}
self.color = spec.clone();
}
if spec.is_none() {
write!(self, "</span>")?;
return Ok(());
} else {
write!(self, "<span class=\"")?;
}
let mut first = true;
fn write_first<W: Write>(first: bool, writer: &mut SvgWriter<W>) -> io::Result<bool> {
if !first {
write!(writer, " ")?;
}
Ok(false)
};
fn write_color<W: Write>(color: &Color, writer: &mut SvgWriter<W>) -> io::Result<()> {
match color {
Color::Black => write!(writer, "black"),
Color::Blue => write!(writer, "blue"),
Color::Green => write!(writer, "green"),
Color::Red => write!(writer, "red"),
Color::Cyan => write!(writer, "cyan"),
Color::Magenta => write!(writer, "magenta"),
Color::Yellow => write!(writer, "yellow"),
Color::White => write!(writer, "white"),
// TODO: other colors
_ => Ok(()),
}
};
if let Some(fg) = spec.fg() {
first = write_first(first, self)?;
write!(self, "fg ")?;
write_color(fg, self)?;
}
if let Some(bg) = spec.bg() {
first = write_first(first, self)?;
write!(self, "bg ")?;
write_color(bg, self)?;
}
if spec.bold() {
first = write_first(first, self)?;
write!(self, "bold")?;
}
if spec.underline() {
first = write_first(first, self)?;
write!(self, "underline")?;
}
if spec.intense() {
first = write_first(first, self)?;
write!(self, "bright")?;
}
write!(self, "\">")?;
Ok(())
}
fn reset(&mut self) -> io::Result<()> {
let color = self.color.clone();
if color != ColorSpec::new() {
write!(self, "</span>")?;
self.color = ColorSpec::new();
}
Ok(())
}
}
|