mermaid-text 0.43.0

Render Mermaid diagrams as Unicode box-drawing text — no browser, no image protocols, pure Rust
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! Renderer for [`GanttDiagram`]. Produces a Unicode bar-chart string.
//!
//! **Layout** (from left to right):
//!
//! ```text
//! Gantt: Title (2014-01-01 → 2014-03-04, 63 days)
//!
//!                     Jan 01    Jan 15    Feb 01
//! Section A
//!   Design            ████████░░░░░░░░░░░░░░░░  [01-01 → 01-30, 30d]
//!   Implementation    ░░░░░░░░████░░░░░░░░░░░░  [01-31 → 02-19, 20d]
//! Section B
//!   Testing           ░░░░░░░░░░░░░░░░░░██░░░░  [02-15 → 03-01, 15d]
//!   Deployment        ░░░░░░░░░░░░░░░░░░░░░█░░  [03-02 → 03-04, 3d]
//! ```
//!
//! **Bar characters:** `█` (U+2588 FULL BLOCK) for active cells, `░`
//! (U+2591 LIGHT SHADE) for empty cells. Both are geometric symbols, not
//! emoji. The `to_ascii` post-pass maps them to `#` and `.` respectively.
//!
//! **Scaling.** When `max_width` is `Some(N)`, the bar zone is sized to fit
//! within the available columns after the name column and the date annotation.
//! Each task bar occupies `(duration / total_span) * bar_zone` cells (minimum
//! 1 cell per task). When `max_width` is `None`, 1 cell = 1 day.
//!
//! **Axis ticks** are emitted at regular intervals derived from the total span
//! so that tick labels never overlap. The tick format obeys `axis_format`.

use unicode_width::UnicodeWidthStr;

use crate::gantt::{GanttDiagram, GanttTask};

// Bar characters — geometric block elements, not emoji.
const FULL_BLOCK: char = '\u{2588}'; //const LIGHT_SHADE: char = '\u{2591}'; //
/// Width of the task-name column (chars). Tasks whose names exceed this are
/// right-truncated with `…` so the bar chart column aligns.
const NAME_COL_MIN: usize = 18;

/// Minimum bar zone width in cells when max_width is provided.
const BAR_ZONE_MIN: usize = 20;

/// Default bar zone cell count (1 cell per day) when max_width is None and the
/// span is too large to render 1:1 without clipping. Used as the cap.
const BAR_ZONE_UNCONSTRAINED_CAP: usize = 60;

/// Render a [`GanttDiagram`] to a Unicode string.
///
/// # Arguments
///
/// * `diag`      — the parsed diagram
/// * `max_width` — optional column budget; when `Some(N)` the bar zone is
///   scaled to fit within N columns; when `None` each cell represents 1 day
///   (capped at 60 cells to keep the output manageable).
///
/// # Returns
///
/// A multi-line string ready for printing. Sections are separated by blank
/// lines; the header line (with title and date range) appears first.
pub fn render(diag: &GanttDiagram, max_width: Option<usize>) -> String {
    let (min_date, max_date) = match (diag.min_date(), diag.max_date()) {
        (Some(lo), Some(hi)) => (lo, hi),
        _ => return render_empty(diag),
    };

    let span_days = diag.span_days().max(1);

    // Compute column widths.
    let name_col = diag
        .sections
        .iter()
        .flat_map(|s| s.tasks.iter())
        .map(|t| t.name.len() + 2) // 2 spaces of indent
        .max()
        .unwrap_or(NAME_COL_MIN)
        .max(NAME_COL_MIN);

    // Date annotation column: "[MM-DD → MM-DD, NNd]" — up to ~22 chars.
    let annot_col = 24usize;

    // Bar zone: how many cells to allocate for the horizontal bars.
    let bar_zone = compute_bar_zone(max_width, name_col, annot_col, span_days);

    let mut out = String::new();

    // ---- Header line -------------------------------------------------------
    let lo_str = min_date.format("%Y-%m-%d").to_string();
    let hi_str = max_date.format("%Y-%m-%d").to_string();
    if let Some(title) = &diag.title {
        out.push_str(&format!(
            "Gantt: {title} ({lo_str} \u{2192} {hi_str}, {span_days} days)\n"
        ));
    } else {
        out.push_str(&format!(
            "Gantt ({lo_str} \u{2192} {hi_str}, {span_days} days)\n"
        ));
    }

    // ---- Axis line ---------------------------------------------------------
    out.push('\n');
    let axis_line = build_axis_line(
        &diag.axis_format,
        min_date,
        span_days,
        bar_zone,
        name_col,
        annot_col,
    );
    out.push_str(&axis_line);
    out.push('\n');

    // ---- Sections and tasks ------------------------------------------------
    for section in &diag.sections {
        out.push('\n');

        if let Some(name) = &section.name {
            out.push_str(name);
            out.push('\n');
        }

        for task in &section.tasks {
            let row = render_task_row(
                task,
                min_date,
                span_days,
                bar_zone,
                name_col,
                &diag.axis_format,
            );
            out.push_str(&row);
            out.push('\n');
        }
    }

    // Trim the trailing newline.
    if out.ends_with('\n') {
        out.pop();
    }
    out
}

// ---------------------------------------------------------------------------
// Bar zone sizing
// ---------------------------------------------------------------------------

/// Compute how many bar cells to allocate.
///
/// When `max_width` is given, the bar zone fills the remaining space after the
/// name column and annotation column. When `None`, each cell represents 1 day
/// capped at `BAR_ZONE_UNCONSTRAINED_CAP`.
fn compute_bar_zone(
    max_width: Option<usize>,
    name_col: usize,
    annot_col: usize,
    span_days: i64,
) -> usize {
    match max_width {
        Some(budget) => {
            // Layout: <name_col> <bar_zone> <2-space gap> <annot_col>
            let overhead = name_col + 2 + annot_col;
            budget.saturating_sub(overhead).max(BAR_ZONE_MIN)
        }
        None => (span_days as usize).min(BAR_ZONE_UNCONSTRAINED_CAP),
    }
}

// ---------------------------------------------------------------------------
// Task row
// ---------------------------------------------------------------------------

/// Render one task row: name column + bar + annotation.
fn render_task_row(
    task: &GanttTask,
    min_date: chrono::NaiveDate,
    span_days: i64,
    bar_zone: usize,
    name_col: usize,
    axis_format: &str,
) -> String {
    // Name column — indent two spaces, pad/truncate to name_col width.
    let display_name = format!("  {}", task.name);
    let col_out = pad_or_truncate(&display_name, name_col);

    // Bar
    let bar = build_bar(task, min_date, span_days, bar_zone);

    // Annotation: "[start → end, Nd]" using axis_format date style.
    let start_str = format_date_axis(&task.start, axis_format);
    let end_str = format_date_axis(&task.end, axis_format);
    let dur = task.duration_days();
    let annot = format!("  [{start_str} \u{2192} {end_str}, {dur}d]");

    format!("{col_out}{bar}{annot}")
}

// ---------------------------------------------------------------------------
// Bar building
// ---------------------------------------------------------------------------

/// Build a bar string of `bar_zone` cells where cells within [start, end] are
/// `FULL_BLOCK` and all others are `LIGHT_SHADE`.
fn build_bar(
    task: &GanttTask,
    min_date: chrono::NaiveDate,
    span_days: i64,
    bar_zone: usize,
) -> String {
    let task_start_offset = (task.start - min_date).num_days();
    let task_end_offset = (task.end - min_date).num_days();

    let mut bar = String::with_capacity(bar_zone * 3); // each char is up to 3 UTF-8 bytes
    for cell in 0..bar_zone {
        // Map cell index to a day offset within the span. Use floating-point
        // to avoid cumulative integer rounding drift.
        let day_lo = (cell as f64 * span_days as f64) / bar_zone as f64;
        let day_hi = ((cell + 1) as f64 * span_days as f64) / bar_zone as f64 - 1.0;

        // Cell is "active" if the task overlaps with this day range.
        let active = (task_end_offset as f64) >= day_lo && (task_start_offset as f64) <= day_hi;
        bar.push(if active { FULL_BLOCK } else { LIGHT_SHADE });
    }
    bar
}

// ---------------------------------------------------------------------------
// Axis line
// ---------------------------------------------------------------------------

/// Build the date axis header line.
///
/// Tick interval is chosen so that ticks are spaced at least 8 cells apart
/// (to avoid label overlap). Labels use the diagram's `axis_format`.
fn build_axis_line(
    axis_format: &str,
    min_date: chrono::NaiveDate,
    span_days: i64,
    bar_zone: usize,
    name_col: usize,
    _annot_col: usize,
) -> String {
    // The axis starts at the same column as the bar zone.
    let prefix = " ".repeat(name_col);

    // Determine tick interval in days so tick labels don't overlap.
    // Each label is at most 8 chars wide; require at least 8 cells between ticks.
    let min_tick_cells = 8usize;
    let cells_per_day = bar_zone as f64 / span_days as f64;
    let min_days_between_ticks = (min_tick_cells as f64 / cells_per_day).ceil() as i64;

    // Round to a "nice" interval: 1, 2, 5, 7, 10, 14, 21, 30, 60, 90, 180, 365.
    let tick_interval_days = nice_interval(min_days_between_ticks.max(1));

    // Build the axis row as an array of chars (one per bar cell), then
    // overwrite tick-label positions.
    let mut row: Vec<char> = vec![' '; bar_zone];

    let mut tick_day: i64 = 0;
    while tick_day < span_days {
        let cell = ((tick_day as f64 * bar_zone as f64) / span_days as f64) as usize;
        let tick_date = min_date + chrono::Duration::days(tick_day);
        let label = format_date_axis(&tick_date, axis_format);

        // Write the label characters into `row` starting at `cell`.
        for (j, ch) in label.chars().enumerate() {
            if cell + j < bar_zone {
                row[cell + j] = ch;
            }
        }

        tick_day += tick_interval_days;
    }

    let axis_str: String = row.into_iter().collect();
    format!("{prefix}{axis_str}")
}

/// Round `n` up to the nearest "nice" interval.
fn nice_interval(n: i64) -> i64 {
    const NICE: &[i64] = &[1, 2, 5, 7, 10, 14, 21, 30, 60, 90, 180, 365];
    NICE.iter().copied().find(|&v| v >= n).unwrap_or(365)
}

// ---------------------------------------------------------------------------
// Formatting helpers
// ---------------------------------------------------------------------------

/// Format a date using the Mermaid axis format pattern.
///
/// Supported patterns: `%b %d`, `%Y-%m-%d`, `%m/%d`, `%d`, `%m-%d`
/// (the default). Unrecognised patterns fall back to `%m-%d`.
fn format_date_axis(date: &chrono::NaiveDate, axis_format: &str) -> String {
    match axis_format {
        "%b %d" => {
            // "%b" is the abbreviated month name (e.g. "Jan"); chrono
            // formats it correctly via NaiveDate::format.
            date.format("%b %d").to_string()
        }
        "%Y-%m-%d" => date.format("%Y-%m-%d").to_string(),
        "%m/%d" => date.format("%m/%d").to_string(),
        "%d" => date.format("%d").to_string(),
        // Default (and explicit "%m-%d")
        _ => date.format("%m-%d").to_string(),
    }
}

/// Pad `s` to `width` display cells, or truncate with `…` if it exceeds it.
fn pad_or_truncate(s: &str, width: usize) -> String {
    let w = UnicodeWidthStr::width(s as &str);
    if w >= width {
        // Truncate: gather chars until we'd exceed width-1, then append '…'.
        let mut out = String::new();
        let mut used = 0;
        for ch in s.chars() {
            let cw = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1);
            if used + cw + 1 > width {
                break;
            }
            out.push(ch);
            used += cw;
        }
        out.push('\u{2026}'); //        // Pad any remaining space.
        let final_w = UnicodeWidthStr::width(out.as_str());
        for _ in final_w..width {
            out.push(' ');
        }
        out
    } else {
        let mut out = s.to_string();
        for _ in w..width {
            out.push(' ');
        }
        out
    }
}

// ---------------------------------------------------------------------------
// Edge case: diagram with no tasks
// ---------------------------------------------------------------------------

fn render_empty(diag: &GanttDiagram) -> String {
    if let Some(title) = &diag.title {
        format!("Gantt: {title} (no tasks)")
    } else {
        "Gantt (no tasks)".to_string()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::gantt::parse;

    fn date(y: i32, m: u32, d: u32) -> chrono::NaiveDate {
        chrono::NaiveDate::from_ymd_opt(y, m, d).unwrap()
    }

    // ---- (1) title appears in output ---------------------------------------

    #[test]
    fn title_appears_in_output() {
        let src =
            "gantt\n  title My Project\n  dateFormat YYYY-MM-DD\n  section S\n  T :2024-01-01, 5d";
        let diag = parse(src).unwrap();
        let out = render(&diag, None);
        assert!(
            out.contains("My Project"),
            "title not found in output:\n{out}"
        );
    }

    // ---- (2) date labels appear in output ----------------------------------

    #[test]
    fn date_labels_appear_in_output() {
        let src = "gantt\n\
            dateFormat YYYY-MM-DD\n\
            axisFormat %Y-%m-%d\n\
            section S\n\
              Task :2024-03-01, 30d";
        let diag = parse(src).unwrap();
        let out = render(&diag, Some(100));
        // The axis line must contain some date label starting with "2024-".
        assert!(
            out.contains("2024-"),
            "date label not found in output:\n{out}"
        );
    }

    // ---- (3) bar widths roughly proportional to task durations -------------

    #[test]
    fn bar_widths_proportional_to_durations() {
        // Task A: 10 days; Task B: 30 days. B should have a longer bar.
        let src = "gantt\n\
            dateFormat YYYY-MM-DD\n\
            section S\n\
              Short :2024-01-01, 10d\n\
              Long  :2024-01-11, 30d";
        let diag = parse(src).unwrap();
        let out = render(&diag, Some(120));

        // Count FULL_BLOCK chars per task line.
        let mut lines = out
            .lines()
            .filter(|l| l.contains("Short") || l.contains("Long"));
        let short_line = lines.next().unwrap_or("");
        let long_line = lines.next().unwrap_or("");
        let short_blocks = short_line.chars().filter(|&c| c == FULL_BLOCK).count();
        let long_blocks = long_line.chars().filter(|&c| c == FULL_BLOCK).count();
        assert!(
            long_blocks > short_blocks,
            "long task bar ({long_blocks}) not wider than short ({short_blocks})"
        );
    }

    // ---- (4) ASCII fallback substitutes block characters -------------------

    #[test]
    fn ascii_fallback_substitutes_block_chars() {
        let src = "gantt\n  dateFormat YYYY-MM-DD\n  section S\n  T :2024-01-01, 5d";
        let diag = parse(src).unwrap();
        let unicode_out = render(&diag, None);
        // Confirm block chars are present in unicode output.
        assert!(unicode_out.contains(FULL_BLOCK) || unicode_out.contains(LIGHT_SHADE));
        // Apply the same to_ascii transform the library uses.
        let ascii_out = crate::to_ascii(&unicode_out);
        assert!(
            ascii_out.is_ascii(),
            "non-ASCII chars remain after to_ascii:\n{ascii_out}"
        );
        // '#' replaces FULL_BLOCK; '.' replaces LIGHT_SHADE.
        assert!(ascii_out.contains('#') || ascii_out.contains('.'));
    }

    // ---- (5) render empty diagram ------------------------------------------

    #[test]
    fn render_empty_diagram_returns_placeholder() {
        let diag = GanttDiagram {
            title: Some("Empty".to_string()),
            ..Default::default()
        };
        let out = render(&diag, None);
        assert!(out.contains("Empty"));
        assert!(out.contains("no tasks"));
    }

    // ---- (6) bar positions don't bleed outside task window -----------------

    #[test]
    fn bar_starts_and_ends_at_correct_positions() {
        // Single task spans the entire diagram — all cells should be FULL_BLOCK.
        // We use a large bar_zone equal to the span_days so there is a 1:1 mapping
        // and every cell falls cleanly inside the task window.
        let src = "gantt\n  dateFormat YYYY-MM-DD\n  section S\n  Only :2024-01-01, 20d";
        let diag = parse(src).unwrap();
        let task = &diag.sections[0].tasks[0];
        let span = diag.span_days(); // 20
        let bar_zone = span as usize; // 1 cell per day
        let bar = build_bar(task, date(2024, 1, 1), span, bar_zone);
        assert_eq!(
            bar.chars().filter(|&c| c == FULL_BLOCK).count(),
            bar_zone,
            "all cells should be active when task spans full diagram (1 cell per day)"
        );
    }
}