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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! This module writes the output of `graph` as text, in the form that the
//! Text form section of design-graph describes. It draws each tree with the
//! branch characters `├──` and `└──`, as the `tree` command does.
use std::io::{self, Write};
use serde_json::Value;
use sokf_core::{EdgeKind, Entry, Graph, GraphResult, NodeKey, NodeKind, Row, graph::node_json};
use super::{Template, template::path_or_target};
/// `text` writes each starting node of `result` with its tree below it, and
/// then the line of counts.
// sokf-implements: CLI-7_AC-77
pub fn text<W: Write>(
w: &mut W,
graph: &Graph,
result: &GraphResult,
template: &Template,
) -> io::Result<()> {
for row in &result.rows {
let line = match graph.kind(&row.node) {
Some(NodeKind::Root) => root_line(graph, &row.node),
// A starting node that is a directory or an asset prints as its path.
Some(NodeKind::Directory | NodeKind::Asset) => row.node.as_str().to_string(),
_ => node_line(graph, &row.node, template),
};
writeln!(w, "{line}{}", star(row.elsewhere))?;
entries(w, graph, &row.entries, "", template)?;
}
// A blank line sets the counts apart from the tree.
if !result.rows.is_empty() {
writeln!(w)?;
}
writeln!(w, "{}", result.summary.line())
}
// sokf-implements: CLI-7_AC-26
fn entries<W: Write>(
w: &mut W,
graph: &Graph,
entries: &[Entry],
prefix: &str,
template: &Template,
) -> io::Result<()> {
let count = entries.len();
for (i, entry) in entries.iter().enumerate() {
let last = i + 1 == count;
let branch = if last { "└── " } else { "├── " };
let below = format!("{prefix}{}", if last { " " } else { "│ " });
match entry {
Entry::Layout(row) => {
writeln!(
w,
"{prefix}{branch}{}{}",
layout_line(graph, row, template),
star(row.elsewhere)
)?;
self::entries(w, graph, &row.entries, &below, template)?;
}
Entry::Edge {
edge,
target,
expanded,
elsewhere,
folded,
via,
} => {
let line = edge_line(graph, *edge, target, *folded, *via, template);
writeln!(w, "{prefix}{branch}{line}{}", star(*elsewhere))?;
self::entries(w, graph, expanded, &below, template)?;
}
// sokf-implements: CLI-7_AC-69
Entry::More { rel, count } => {
let what = match rel {
Some(rel) => format!("{count} more {rel}"),
None => format!("{count} more"),
};
writeln!(w, "{prefix}{branch}… {what} (--all)")?;
}
}
}
Ok(())
}
/// `edge_line` returns the line of an edge, `<rel> → <target>`. A region
/// is a part of an included file, or the key that a code marker cites. On
/// the declared edge the region follows the target after `#`, and on its
/// inverse it follows the `rel`. A folded edge, which stands for the
/// includes of two or more regions of one file, or for the markers of one
/// file under a document, shows its count in place of a region. A marker
/// edge that isn't folded ends with `(line <n>)`, the line of the marker
/// in its file. When the edge comes from a directory that holds the file of
/// the row, the node that the tree prints the line under, the line ends
/// with `(via <directory>)`. On a key's `defined-in` line, `(line <n>)`
/// gives the number of the line that defines the key.
// sokf-implements: CLI-7_AC-90
// sokf-implements: CLI-7_AC-93
fn edge_line(
graph: &Graph,
edge: usize,
target: &NodeKey,
folded: usize,
via: bool,
template: &Template,
) -> String {
let e = &graph.edges[edge];
let region = e
.region
.as_deref()
.filter(|_| matches!(e.kind, EdgeKind::Includes | EdgeKind::Marker) && folded == 0);
// An include's region follows its target, and an `included-by`'s its
// `rel`. A marker's region follows its target, and its inverse's `rel`.
let after_rel = match e.kind {
EdgeKind::Includes => e.rel != "includes",
_ => e.synthesised,
};
let rel = match region {
Some(region) if after_rel => format!("{} #{region}", e.rel),
_ => e.rel.clone(),
};
let mut line = format!("{rel} → {}", node_line(graph, target, template));
if !after_rel && let Some(region) = region {
line.push('#');
line.push_str(region);
}
// sokf-implements: CLI-7_AC-70
// sokf-implements: CLI-7_AC-91
if folded > 0 {
line.push_str(&format!(" ({})", folded_count(e.kind, folded)));
} else if e.kind == EdgeKind::Marker
&& let Some(n) = e.line
{
line.push_str(&format!(" (line {n})"));
}
if via {
line.push_str(&format!(" (via {})", e.from.as_str()));
}
if e.kind == EdgeKind::Keys
&& e.rel == "defined-in"
&& let Some(n) = graph
.node(&e.from)
.and_then(|n| n.fields.get("line"))
.and_then(Value::as_u64)
{
line.push_str(&format!(" (line {n})"));
}
line
}
/// `folded_count` returns the count of a folded edge, such as `3 regions`
/// or `1 marker`.
pub(crate) fn folded_count(kind: EdgeKind, n: usize) -> String {
match (kind, n) {
(EdgeKind::Marker, 1) => "1 marker".to_string(),
(EdgeKind::Marker, n) => format!("{n} markers"),
(_, n) => format!("{n} regions"),
}
}
/// `root_line` returns the path of a root, followed by ` - ` and the
/// description of its manifest when the manifest has one.
// sokf-implements: CLI-7_AC-66
fn root_line(graph: &Graph, key: &NodeKey) -> String {
match graph
.node(key)
.and_then(|n| n.fields.get("description"))
.and_then(Value::as_str)
{
Some(description) => format!("{} - {description}", key.as_str()),
None => key.as_str().to_string(),
}
}
/// `layout_line` returns the line of a node in a layout: a root as
/// `root_line` writes it, a directory as its name followed by `/`, and any
/// other node as `node_line` writes it.
fn layout_line(graph: &Graph, row: &Row, template: &Template) -> String {
match graph.kind(&row.node) {
Some(NodeKind::Root) => root_line(graph, &row.node),
Some(NodeKind::Directory) => format!(
"{}/",
row.node.path().map(|p| p.file_name()).unwrap_or_default()
),
_ => node_line(graph, &row.node, template),
}
}
/// `node_line` returns the line of a starting node or a target node: a
/// document through the template, an asset as the id of its sidecar, a key
/// as it is written, and any other node as its path or target. The line of
/// a broken node ends with `[broken: <reason>]`.
// sokf-implements: CLI-7_AC-18
// sokf-implements: CLI-7_AC-25
// sokf-implements: CLI-7_AC-85
fn node_line(graph: &Graph, key: &NodeKey, template: &Template) -> String {
let Some(node) = graph.node(key) else {
return key.as_str().to_string();
};
let json = node_json(node);
let field = |name: &str| json.get(name).and_then(Value::as_str).map(str::to_string);
let mut line = match node.kind {
NodeKind::Document => template.render(&json),
NodeKind::Asset => field("sidecar").unwrap_or_else(|| path_or_target(&json)),
NodeKind::Key => field("key").unwrap_or_else(|| path_or_target(&json)),
_ => path_or_target(&json),
};
if json.get("broken") == Some(&Value::Bool(true)) {
let reason = json
.get("reason")
.and_then(Value::as_str)
.unwrap_or_default();
line.push_str(&format!(" [broken: {reason}]"));
}
line
}
fn star(elsewhere: bool) -> &'static str {
if elsewhere { " (*)" } else { "" }
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use serde_json::json;
use sokf_core::Node;
use super::*;
use crate::render::fixtures::{edge, key, node};
fn graph() -> Graph {
let nodes: BTreeMap<NodeKey, Node> = [
node("/k", NodeKind::Root, json!({"name": "k"})),
node("/k/core", NodeKind::Directory, json!({})),
node(
"/k/core/a.md",
NodeKind::Document,
json!({"id": "a", "title": "A"}),
),
node(
"/k/b.md",
NodeKind::Document,
json!({"broken": true, "reason": "no id"}),
),
node("/README.md", NodeKind::Additional, json!({})),
node("/src/x.rs", NodeKind::File, json!({})),
node("gone", NodeKind::Missing, json!({})),
]
.into_iter()
.collect();
Graph::new(
nodes,
vec![
edge(
"/k/core/a.md",
EdgeKind::Includes,
"/src/x.rs",
"includes",
Some("cli"),
false,
),
edge(
"/k/core/a.md",
EdgeKind::Links,
"gone",
"references",
None,
false,
),
edge(
"/src/x.rs",
EdgeKind::Includes,
"/k/core/a.md",
"included-by",
Some("cli"),
false,
),
],
)
}
fn row(s: &str, elsewhere: bool, entries: Vec<Entry>) -> Row {
Row {
node: key(s),
elsewhere,
entries,
}
}
fn edge_entry(edge: usize, target: &str, expanded: Vec<Entry>, elsewhere: bool) -> Entry {
Entry::Edge {
edge,
target: key(target),
expanded,
elsewhere,
folded: 0,
via: false,
}
}
// sokf-tests: CLI-7_AC-26
#[test]
fn prints_the_prefixes_lines_regions_stars_and_broken_markers() {
let g = graph();
let result = GraphResult {
start: vec![],
rows: vec![
row(
"/k",
false,
vec![
Entry::Layout(row("/README.md", false, vec![])),
Entry::Layout(row(
"/k/b.md",
false,
vec![edge_entry(2, "/k/core/a.md", vec![], true)],
)),
Entry::Layout(row(
"/k/core",
false,
vec![Entry::Layout(row(
"/k/core/a.md",
false,
vec![
edge_entry(
0,
"/src/x.rs",
vec![edge_entry(2, "/k/core/a.md", vec![], true)],
false,
),
edge_entry(1, "gone", vec![], false),
],
))],
)),
],
),
row("/k/core/a.md", true, vec![]),
row("/src/x.rs", false, vec![]),
],
nodes: vec![],
edges: vec![],
folded: Default::default(),
summary: Default::default(),
projection: None,
notes: Vec::new(),
};
let mut buf = Vec::new();
text(
&mut buf,
&g,
&result,
&Template::parse("{id} {title}").unwrap(),
)
.unwrap();
assert_eq!(
String::from_utf8(buf).unwrap(),
"/k\n\
├── /README.md\n\
├── /k/b.md [broken: no id]\n\
│ └── included-by #cli → a A (*)\n\
└── core/\n\
\x20 └── a A\n\
\x20 ├── includes → /src/x.rs#cli\n\
\x20 │ └── included-by #cli → a A (*)\n\
\x20 └── references → gone\n\
a A (*)\n\
/src/x.rs\n\
\n\
0 / 0 docs\n"
);
// An unknown key prints as itself.
assert_eq!(
node_line(&g, &key("/nope"), &Template::parse("{id}").unwrap()),
"/nope"
);
}
}