This repository has been archived by the owner on Mar 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
write.rs
177 lines (161 loc) · 5.62 KB
/
write.rs
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
// Copyright 2020 Sebastian Wiesner <sebastian@swsnr.de>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use std::io::{Result, Write};
use ansi_term::{Colour, Style};
use pulldown_cmark::{CodeBlockKind, HeadingLevel};
use syntect::highlighting::{HighlightState, Highlighter, Theme};
use syntect::parsing::{ParseState, ScopeStack};
use crate::references::*;
use crate::render::data::LinkReferenceDefinition;
use crate::render::state::*;
use crate::terminal::capabilities::{MarkCapability, StyleCapability, TerminalCapabilities};
use crate::terminal::TerminalSize;
use crate::{Environment, Settings};
#[inline]
pub fn write_indent<W: Write>(writer: &mut W, level: u16) -> Result<()> {
write!(writer, "{}", " ".repeat(level as usize))
}
#[inline]
pub fn write_styled<W: Write, S: AsRef<str>>(
writer: &mut W,
capabilities: &TerminalCapabilities,
style: &Style,
text: S,
) -> Result<()> {
match capabilities.style {
None => write!(writer, "{}", text.as_ref()),
Some(StyleCapability::Ansi(ansi)) => ansi.write_styled(writer, style, text),
}
}
#[inline]
pub fn write_mark<W: Write>(writer: &mut W, capabilities: &TerminalCapabilities) -> Result<()> {
if let Some(mark) = capabilities.marks {
match mark {
MarkCapability::ITerm2(marks) => marks.set_mark(writer),
}
} else {
Ok(())
}
}
#[inline]
pub fn write_rule<W: Write>(
writer: &mut W,
capabilities: &TerminalCapabilities,
length: usize,
) -> std::io::Result<()> {
let rule = "\u{2550}".repeat(length);
let style = Style::new().fg(Colour::Green);
write_styled(writer, capabilities, &style, rule)
}
#[inline]
pub fn write_border<W: Write>(
writer: &mut W,
capabilities: &TerminalCapabilities,
terminal_size: &TerminalSize,
) -> std::io::Result<()> {
let separator = "\u{2500}".repeat(terminal_size.columns.min(20));
let style = Style::new().fg(Colour::Green);
write_styled(writer, capabilities, &style, separator)?;
writeln!(writer)
}
pub fn write_link_refs<W: Write>(
writer: &mut W,
environment: &Environment,
capabilities: &TerminalCapabilities,
links: Vec<LinkReferenceDefinition>,
) -> Result<()> {
if !links.is_empty() {
writeln!(writer)?;
for link in links {
let style = Style::new().fg(link.colour);
write_styled(writer, capabilities, &style, &format!("[{}]: ", link.index))?;
// If we can resolve the link try to write it as inline link to make the URL
// clickable. This mostly helps images inside inline links which we had to write as
// reference links because we can't nest inline links.
if let Some(url) = environment.resolve_reference(&link.target) {
use crate::terminal::capabilities::LinkCapability::*;
match &capabilities.links {
Some(Osc8(links)) => {
links.set_link_url(writer, url, &environment.hostname)?;
write_styled(writer, capabilities, &style, link.target)?;
links.clear_link(writer)?;
}
None => write_styled(writer, capabilities, &style, link.target)?,
};
} else {
write_styled(writer, capabilities, &style, link.target)?;
}
if !link.title.is_empty() {
write_styled(writer, capabilities, &style, format!(" {}", link.title))?;
}
writeln!(writer)?;
}
};
Ok(())
}
pub fn write_start_code_block<W: Write>(
writer: &mut W,
settings: &Settings,
indent: u16,
style: Style,
block_kind: CodeBlockKind<'_>,
theme: &Theme,
) -> Result<StackedState> {
write_indent(writer, indent)?;
write_border(
writer,
&settings.terminal_capabilities,
&settings.terminal_size,
)?;
// And start the indent for the contents of the block
write_indent(writer, indent)?;
match (&settings.terminal_capabilities.style, block_kind) {
(Some(StyleCapability::Ansi(ansi)), CodeBlockKind::Fenced(name)) if !name.is_empty() => {
match settings.syntax_set.find_syntax_by_token(&name) {
None => Ok(LiteralBlockAttrs {
indent,
style: style.fg(Colour::Yellow),
}
.into()),
Some(syntax) => {
let parse_state = ParseState::new(syntax);
let highlight_state =
HighlightState::new(&Highlighter::new(theme), ScopeStack::new());
Ok(HighlightBlockAttrs {
ansi: *ansi,
indent,
highlight_state,
parse_state,
}
.into())
}
}
}
(_, _) => Ok(LiteralBlockAttrs {
indent,
style: style.fg(Colour::Yellow),
}
.into()),
}
}
pub fn write_start_heading<W: Write>(
writer: &mut W,
capabilities: &TerminalCapabilities,
style: Style,
level: HeadingLevel,
) -> Result<StackedState> {
let style = style.fg(Colour::Blue).bold();
write_styled(
writer,
capabilities,
&style,
"\u{2504}".repeat(level as usize),
)?;
// Headlines never wrap, so indent doesn't matter
Ok(StackedState::Inline(
InlineState::InlineText,
InlineAttrs { style, indent: 0 },
))
}