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
use super::*;
use ::kserd::fmt::FormattingConfig;
/// > **These methods are available when the REPL is in the [`Print`] state.**
impl<D> Repl<Print, D> {
/// Prints the result if successful as `[out#]` or the failure message if any.
/// Uses the default formatter for the `Kserd` data.
pub fn print(self) -> (Repl<Read, D>, Option<(usize, Kserd<'static>)>) {
self.print_with_formatting(FormattingConfig::default())
}
/// Prints the result if successful as `[out#]` or the failure message if any.
/// Uses the given formatting configuration for the `Kserd` data.
/// The return is (<repl in read state>, <maybe <stmt index, data>>)
pub fn print_with_formatting(
self,
config: FormattingConfig,
) -> (Repl<Read, D>, Option<(usize, Kserd<'static>)>) {
let Repl {
state,
data,
more,
data_mrker,
} = self;
let repl_data = data;
let Print { mut output, data } = state;
let mut kserd = None;
match data {
EvalOutput::Data(k) => {
let num = repl_data.current_src().stmts.len().saturating_sub(1);
let out_stmt = format!("[out{}]", num);
let line = format!(
"{} {}: {}",
repl_data.cmdtree.path().color(repl_data.prompt_colour),
out_stmt.color(repl_data.out_colour),
k.as_str_with_config(config)
);
output.write_line(&line);
kserd = Some((num, k));
}
EvalOutput::Print(print) => {
if print.len() > 0 {
// only write if there is something to write.
output.write_line(&print);
}
}
}
let mut r = Repl {
state: Read {
output: output.into_read(),
},
data: repl_data,
data_mrker,
more,
};
prepare_read(&mut r);
(r, kserd)
}
}
fn prepare_read<D>(repl: &mut Repl<Read, D>) {
repl.draw_prompt();
let editing_src = repl.data.editing.and_then(|ei| {
let src = repl.data.current_src();
match ei.editing {
Editing::Crate => src.crates.get(ei.index).map(|x| &x.src_line).cloned(),
Editing::Item => src.items.get(ei.index).map(|x| x.0.clone()),
Editing::Stmt => src.stmts.get(ei.index).map(|x| x.src_line()),
}
});
repl.data.editing_src = editing_src;
}