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
use std::fmt;
/// Several lines of output that travel together.
///
/// Every multi-line component in tinsel returns a `Block` ([`Table`](crate::Table),
/// [`Tree`](crate::Tree), [`Card`](crate::Card), [`callout`](crate::callout), …), so they all
/// work the same way: print it, `println!` it, or take its lines and arrange them yourself.
///
/// ```
/// use tinsel::{Block, pairs};
///
/// let block: Block = pairs(&[("region", "eu-west-2"), ("memory", "512 MB")]);
/// block.print(); // print it
/// println!("{block}"); // or format it
/// assert_eq!(block.len(), 2); // or look inside
/// ```
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Block {
lines: Vec<String>,
}
impl Block {
/// An empty block.
pub fn new() -> Self {
Self::default()
}
/// Add a line to the end.
pub fn push(&mut self, line: impl Into<String>) {
self.lines.push(line.into());
}
/// Add every line of another block to the end.
pub fn extend(&mut self, other: Block) {
self.lines.extend(other.lines);
}
/// The lines, still styled.
pub fn lines(&self) -> &[String] {
&self.lines
}
/// The lines, owned.
pub fn into_lines(self) -> Vec<String> {
self.lines
}
/// How many lines there are.
pub fn len(&self) -> usize {
self.lines.len()
}
/// Whether there are no lines at all.
pub fn is_empty(&self) -> bool {
self.lines.is_empty()
}
/// The text without any colour or styling, lines joined with `\n`. Handy in tests.
pub fn plain(&self) -> String {
crate::text::strip_ansi(&self.to_string())
}
/// Print every line to stdout. With [`Output::Json`](crate::Output::Json) the block is
/// one `{"event":"block","lines":[…]}` event instead.
pub fn print(&self) {
if crate::output::json() {
return self.emit();
}
for line in &self.lines {
println!("{line}");
}
}
/// Print every line to stderr (or, as JSON, to stdout like everything else).
pub fn eprint(&self) {
if crate::output::json() {
return self.emit();
}
for line in &self.lines {
eprintln!("{line}");
}
}
}
impl Block {
/// Print the block through a pager (`$PAGER`, or `less`) if it's taller than the
/// terminal, so it can be scrolled and searched; otherwise just print it. Colours are
/// kept (`less -R`).
///
/// ```no_run
/// tinsel::markdown(include_str!("../CHANGELOG.md")).page();
/// ```
pub fn page(&self) {
use std::io::{IsTerminal, Write};
if crate::output::json() || !std::io::stdout().is_terminal() || self.len() < crate::term::height() {
return self.print();
}
let pager = std::env::var("PAGER").ok().filter(|p| !p.trim().is_empty());
let default = if cfg!(windows) { "more" } else { "less" };
let command = pager.unwrap_or_else(|| default.to_string());
let mut words = command.split_whitespace();
let program = words.next().unwrap_or(default);
let mut cmd = std::process::Command::new(program);
cmd.args(words).stdin(std::process::Stdio::piped());
if program.ends_with("less") && std::env::var_os("LESS").is_none() {
// colours, quit if it fits after all, and leave the text on screen afterwards
cmd.env("LESS", "-RFX");
}
let Ok(mut child) = cmd.spawn() else {
return self.print();
};
if let Some(mut stdin) = child.stdin.take() {
// the reader quitting early closes the pipe; that's fine
let _ = stdin.write_all(format!("{self}\n").as_bytes());
}
let _ = child.wait();
}
fn emit(&self) {
let lines: Vec<String> = self.lines.iter().map(|l| crate::text::strip_ansi(l)).collect();
crate::output::emit("block", &[("lines", crate::output::Val::List(&lines))]);
}
}
impl fmt::Display for Block {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.lines.join("\n"))
}
}
impl From<Vec<String>> for Block {
fn from(lines: Vec<String>) -> Self {
Block { lines }
}
}
impl FromIterator<String> for Block {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
Block {
lines: iter.into_iter().collect(),
}
}
}
impl IntoIterator for Block {
type Item = String;
type IntoIter = std::vec::IntoIter<String>;
fn into_iter(self) -> Self::IntoIter {
self.lines.into_iter()
}
}