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
use super::{context::*, theme::*};
use std::io::*;
const TO_STRING_BUFFER_CAPACITY: usize = 1024;
//
// Depict
//
/// Depict the object in a manner suitable for terminals.
///
/// May include colors and styles.
pub trait Depict {
/// Write a depiction suitable for terminals.
///
/// Required behavior for implementations:
///
/// 1. Depictions *must not* end in a newline.
/// 2. If *not* starting with a newline and *not* empty, *must* call [DepictionContext::separate] first.
/// 3. All lines *after* the first (but *not* the first) *must* start with the [DepictionContext] indentation.
fn depict<WriteT>(&self, writer: &mut WriteT, context: &DepictionContext) -> Result<()>
where
WriteT: Write;
/// Write the depiction with a final newline.
fn write_depiction<WriteT>(&self, writer: &mut WriteT, context: &DepictionContext) -> Result<()>
where
WriteT: Write,
{
self.depict(writer, context)?;
writeln!(writer)
}
/// Write the depiction with a final newline.
///
/// Uses default [Theme].
fn write_default_depiction<WriteT>(&self, writer: &mut WriteT) -> Result<()>
where
WriteT: Write,
{
self.write_depiction(writer, &DepictionContext::new(&Theme::default()))
}
/// Write the depiction with a final newline.
///
/// Uses plain [Theme].
fn write_plain_depiction<WriteT>(&self, writer: &mut WriteT) -> Result<()>
where
WriteT: Write,
{
self.write_depiction(writer, &DepictionContext::new(&Theme::plain()))
}
/// Print the depiction to [anstream::stdout] with a final newline.
///
/// Panics on write [Error].
fn print_depiction(&self, context: &DepictionContext) {
self.write_depiction(&mut anstream::stdout(), context).expect("writing to stdout");
}
/// Print the depiction to [anstream::stdout] with a final newline.
///
/// Uses default [Theme].
///
/// Panics on write [Error].
fn print_default_depiction(&self) {
self.print_depiction(&DepictionContext::new(&Theme::default()));
}
/// Print the depiction to [anstream::stdout] with a final newline.
///
/// Uses plain [Theme].
///
/// Panics on write [Error].
fn print_plain_depiction(&self) {
self.print_depiction(&DepictionContext::new(&Theme::plain()));
}
/// Print the depiction to [anstream::stderr] with a final newline.
///
/// Panics on write [Error].
fn eprint_depiction(&self, context: &DepictionContext) {
self.write_depiction(&mut anstream::stderr(), context).expect("writing to stderr");
}
/// Print the depiction to [anstream::stderr] with a final newline.
///
/// Uses default [Theme].
///
/// Panics on write [Error].
fn eprint_default_depiction(&self) {
self.eprint_depiction(&DepictionContext::new(&Theme::default()));
}
/// Print the depiction to [anstream::stderr] with a final newline.
///
/// Uses plain [Theme].
///
/// Panics on write [Error].
fn eprint_plain_depiction(&self) {
self.eprint_depiction(&DepictionContext::new(&Theme::plain()));
}
/// Capture [depict](Depict::depict) into a [String].
fn to_depiction(&self, context: &DepictionContext) -> Result<String> {
let mut writer = Vec::with_capacity(TO_STRING_BUFFER_CAPACITY);
self.depict(&mut writer, context)?;
String::from_utf8(writer.into()).map_err(Error::other)
}
/// Capture [depict](Depict::depict) into a [String].
///
/// Uses default [Theme].
fn to_default_depiction(&self) -> Result<String> {
self.to_depiction(&DepictionContext::new(&Theme::default()))
}
/// Capture [depict](Depict::depict) into a [String].
///
/// Uses plain [Theme].
fn to_plain_depiction(&self) -> Result<String> {
self.to_depiction(&DepictionContext::new(&Theme::plain()))
}
}