straymark-cli 3.14.1

CLI for StrayMark — the cognitive discipline your AI-assisted projects need
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
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph, Widget, Wrap};

use crate::charter::{CharterFrontmatter, CharterStatus, EffortEstimate};
use crate::tui::app::{ActivePanel, App, MetaSelection};
use crate::tui::document::{ConfidenceLevel, DocFrontMatter, DocStatus, DocumentMetadata, RiskLevel};
use crate::tui::i18n_strings::t;
use crate::tui::theme;
use crate::utils::{pad_right_visual, truncate_visual};

/// Visual columns reserved for the field label + colon. Sized to fit the
/// widest English label ("Confidence:" = 11 cols) plus a small gutter.
/// Spanish ("Confianza:") and zh-CN labels (≤ 6 cols) all fit comfortably.
const LABEL_WIDTH: usize = 13;

/// Render a metadata field label padded to LABEL_WIDTH visual columns so
/// values stay aligned across all locales.
fn label_block(label: &str, style: ratatui::style::Style) -> ratatui::text::Span<'static> {
    ratatui::text::Span::styled(format!(" {}", pad_right_visual(label, LABEL_WIDTH)), style)
}

pub struct MetadataPanel<'a> {
    app: &'a App,
}

impl<'a> MetadataPanel<'a> {
    pub fn new(app: &'a App) -> Self {
        Self { app }
    }
}

impl Widget for MetadataPanel<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let is_active = self.app.active_panel == ActivePanel::Metadata;
        let lang = self.app.language.as_str();
        let border_style = if is_active {
            Style::default().fg(theme::BORDER_ACTIVE)
        } else {
            Style::default().fg(theme::SUBTLE)
        };

        let block = Block::default()
            .title(format!(" {} ", t("Metadata", lang)))
            .title_style(if is_active {
                Style::default().fg(theme::ACCENT).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme::SUBTLE)
            })
            .borders(Borders::ALL)
            .border_type(theme::BORDER_TYPE)
            .border_style(border_style)
            .style(Style::default().bg(theme::SURFACE));

        let inner = block.inner(area);
        block.render(area, buf);

        let doc = match &self.app.current_doc {
            Some(d) => d,
            None => {
                let line = Line::from(Span::styled(
                    format!(" {}", t("No document selected", lang)),
                    Style::default().fg(theme::TEXT_DIM),
                ));
                Paragraph::new(vec![line]).render(inner, buf);
                return;
            }
        };

        let lines: Vec<Line<'static>> = match &doc.frontmatter {
            None => {
                vec![
                    Line::from(vec![
                        Span::styled(
                            format!(" {}  ", t("File:", lang)),
                            Style::default().fg(theme::TEXT_DIM),
                        ),
                        Span::styled(doc.filename.clone(), Style::default().fg(theme::TEXT)),
                    ]),
                    Line::from(Span::styled(
                        format!(" {}", t("No frontmatter", lang)),
                        Style::default().fg(theme::TEXT_DIM),
                    )),
                ]
            }
            Some(DocumentMetadata::Doc(fm)) => doc_lines(fm, lang, self.app, inner.width),
            Some(DocumentMetadata::Charter(fm)) => charter_lines(fm, lang, self.app, inner.width),
        };

        // Empty/no-frontmatter cases bypass scroll arithmetic — just paint.
        if doc.frontmatter.is_none() {
            Paragraph::new(lines)
                .wrap(Wrap { trim: false })
                .render(inner, buf);
            return;
        }

        // Calculate scroll: find the line with ▸ marker to keep it visible
        let total_lines = lines.len() as u16;
        let scroll = if total_lines > inner.height {
            if let Some(selected_pos) = lines.iter().position(|line| {
                line.spans
                    .first()
                    .map(|s| s.content.contains(''))
                    .unwrap_or(false)
            }) {
                let sel = selected_pos as u16;
                if sel >= inner.height {
                    sel.saturating_sub(inner.height.saturating_sub(3))
                        .min(total_lines.saturating_sub(inner.height))
                } else {
                    0
                }
            } else {
                0
            }
        } else {
            0
        };

        Paragraph::new(lines)
            .scroll((scroll, 0))
            .render(inner, buf);
    }
}

fn doc_lines(
    fm: &DocFrontMatter,
    lang: &str,
    app: &App,
    inner_width: u16,
) -> Vec<Line<'static>> {
    let l = Style::default().fg(theme::TEXT_DIM);
    let v = Style::default().fg(theme::TEXT);
    let mut lines: Vec<Line<'static>> = vec![Line::from("")];

    // Status
    if let Some(ref status) = fm.status {
        let (indicator, color) = status_style(status);
        lines.push(Line::from(vec![
            label_block(t("Status:", lang), l),
            Span::styled(
                format!("{indicator} {status}"),
                Style::default().fg(color).add_modifier(Modifier::BOLD),
            ),
        ]));
    }

    // Created
    if let Some(ref created) = fm.created {
        lines.push(Line::from(vec![
            label_block(t("Created:", lang), l),
            Span::styled(created.clone(), v),
        ]));
    }

    // Agent
    if let Some(ref agent) = fm.agent {
        lines.push(Line::from(vec![
            label_block(t("Agent:", lang), l),
            Span::styled(agent.clone(), v),
        ]));
    }

    // Confidence
    if let Some(ref confidence) = fm.confidence {
        let (filled, total, color, label) = confidence_bar(confidence);
        lines.push(Line::from(vec![
            label_block(t("Confidence:", lang), l),
            Span::styled(
                format!("{}{}", "".repeat(filled), "".repeat(total - filled)),
                Style::default().fg(color),
            ),
            Span::styled(format!("  {label}"), Style::default().fg(color)),
        ]));
    }

    // Risk
    if let Some(ref risk) = fm.risk_level {
        let (filled, total, color, label) = risk_bar(risk);
        lines.push(Line::from(vec![
            label_block(t("Risk:", lang), l),
            Span::styled(
                format!("{}{}", "".repeat(filled), "".repeat(total - filled)),
                Style::default().fg(color),
            ),
            Span::styled(format!("  {label}"), Style::default().fg(color)),
        ]));
    }

    // Review required
    if let Some(true) = fm.review_required {
        lines.push(Line::from(vec![
            label_block(t("Review:", lang), l),
            Span::styled(
                t("⚠ REQUIRED", lang).to_string(),
                Style::default()
                    .fg(theme::YELLOW)
                    .add_modifier(Modifier::BOLD),
            ),
        ]));
    }

    // Tags
    if !fm.tags.is_empty() {
        let tag_hint = match app.meta_selection {
            Some(MetaSelection::Tag(_)) => t(" (Enter: search)", lang),
            _ => "",
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {}", t("Tags:", lang)), l),
            Span::styled(tag_hint.to_string(), Style::default().fg(theme::TEXT_DIM)),
        ]));
        let tag_style = Style::default()
            .fg(theme::TEXT_DIM)
            .bg(Color::Rgb(45, 45, 60));
        let tag_selected_style = Style::default()
            .fg(Color::Rgb(220, 224, 242))
            .bg(Color::Rgb(45, 50, 80))
            .add_modifier(Modifier::BOLD);
        for (i, tag) in fm.tags.iter().enumerate() {
            let is_sel = app.meta_selection == Some(MetaSelection::Tag(i));
            let marker = if is_sel { "" } else { "   " };
            let st = if is_sel { tag_selected_style } else { tag_style };
            lines.push(Line::from(vec![
                Span::styled(marker, l),
                Span::styled(format!(" {tag} "), st),
            ]));
        }
    }

    // Separator before related
    if !fm.related.is_empty() {
        push_related_block(
            &mut lines,
            &fm.related,
            lang,
            app,
            inner_width,
        );
    }

    lines
}

fn charter_lines(
    fm: &CharterFrontmatter,
    lang: &str,
    app: &App,
    inner_width: u16,
) -> Vec<Line<'static>> {
    let l = Style::default().fg(theme::TEXT_DIM);
    let v = Style::default().fg(theme::TEXT);
    let mut lines: Vec<Line<'static>> = vec![Line::from("")];

    // Charter ID
    if !fm.charter_id.is_empty() {
        lines.push(Line::from(vec![
            label_block(t("Charter ID:", lang), l),
            Span::styled(fm.charter_id.clone(), v),
        ]));
    }

    // Status (charter vocabulary)
    let (indicator, status_color) = charter_status_style(&fm.status);
    lines.push(Line::from(vec![
        label_block(t("Status:", lang), l),
        Span::styled(
            format!("{indicator} {}", fm.status.as_str().to_uppercase()),
            Style::default()
                .fg(status_color)
                .add_modifier(Modifier::BOLD),
        ),
    ]));

    // Effort estimate
    let (effort_color, effort_label) = effort_style(&fm.effort_estimate);
    lines.push(Line::from(vec![
        label_block(t("Effort:", lang), l),
        Span::styled(
            effort_label.to_string(),
            Style::default().fg(effort_color).add_modifier(Modifier::BOLD),
        ),
    ]));

    // Trigger (one-line, truncated to remaining width)
    if !fm.trigger.is_empty() {
        let max_width = (inner_width as usize)
            .saturating_sub(LABEL_WIDTH + 2);
        let trimmed = fm.trigger.replace('\n', " ");
        lines.push(Line::from(vec![
            label_block(t("Trigger:", lang), l),
            Span::styled(truncate_visual(&trimmed, max_width), v),
        ]));
    }

    // Origin (single-line summary). The materialized "Related" block below
    // makes each origin link navigable; this top line just gives an at-a-glance
    // hint of where the charter came from.
    let origin = crate::charter::display_origin(fm);
    let origin_style = if origin == "" {
        Style::default().fg(theme::TEXT_DIM)
    } else {
        v
    };
    let origin_max = (inner_width as usize).saturating_sub(LABEL_WIDTH + 2);
    lines.push(Line::from(vec![
        label_block(t("Origin:", lang), l),
        Span::styled(truncate_visual(&origin, origin_max), origin_style),
    ]));

    // Related = originating_ailogs + originating_spec, surfaced through the
    // same MetaSelection::Related mechanism docs use, so Enter still follows
    // the link.
    let mut related: Vec<String> = Vec::new();
    if let Some(ailogs) = &fm.originating_ailogs {
        related.extend(ailogs.iter().cloned());
    }
    if let Some(spec) = &fm.originating_spec {
        related.push(spec.clone());
    }
    if !related.is_empty() {
        push_related_block(&mut lines, &related, lang, app, inner_width);
    }

    lines
}

fn push_related_block(
    lines: &mut Vec<Line<'static>>,
    related: &[String],
    lang: &str,
    app: &App,
    inner_width: u16,
) {
    let l = Style::default().fg(theme::TEXT_DIM);
    let sep_width = inner_width.saturating_sub(2) as usize;
    lines.push(Line::from(Span::styled(
        format!(" {}", "".repeat(sep_width)),
        Style::default().fg(theme::SUBTLE),
    )));

    let hint = match app.meta_selection {
        Some(MetaSelection::Related(_)) => t(" (Enter: follow)", lang),
        _ => "",
    };
    lines.push(Line::from(vec![
        Span::styled(format!(" {}", t("Related:", lang)), l),
        Span::styled(hint.to_string(), Style::default().fg(theme::TEXT_DIM)),
    ]));

    let max_link_width = inner_width.saturating_sub(4) as usize;
    for (i, rel) in related.iter().enumerate() {
        let is_selected = app.meta_selection == Some(MetaSelection::Related(i));
        let marker = if is_selected { "" } else { "   " };
        let style = if is_selected {
            Style::default()
                .fg(Color::Rgb(220, 224, 242))
                .bg(Color::Rgb(45, 50, 80))
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED)
        } else {
            Style::default()
                .fg(theme::TEXT)
                .add_modifier(Modifier::UNDERLINED)
        };
        let display = truncate_visual(rel, max_link_width);
        lines.push(Line::from(vec![
            Span::styled(marker, l),
            Span::styled(display, style),
        ]));
    }
}

fn status_style(status: &DocStatus) -> (&'static str, Color) {
    match status {
        DocStatus::Draft => ("", theme::YELLOW),
        DocStatus::Accepted => ("", theme::GREEN),
        DocStatus::Deprecated => ("", theme::RED),
        DocStatus::Superseded => ("", theme::TEXT_DIM),
        DocStatus::Unknown => ("?", theme::TEXT_DIM),
    }
}

fn charter_status_style(status: &CharterStatus) -> (&'static str, Color) {
    match status {
        CharterStatus::Declared => ("", theme::YELLOW),
        CharterStatus::InProgress => ("", theme::ACCENT),
        CharterStatus::Closed => ("", theme::GREEN),
    }
}

fn effort_style(effort: &EffortEstimate) -> (Color, &'static str) {
    match effort {
        EffortEstimate::Xs => (theme::SUBTLE, "XS"),
        EffortEstimate::S => (theme::GREEN, "S"),
        EffortEstimate::M => (theme::YELLOW, "M"),
        EffortEstimate::L => (theme::RED, "L"),
    }
}

fn confidence_bar(level: &ConfidenceLevel) -> (usize, usize, Color, &'static str) {
    match level {
        ConfidenceLevel::High => (8, 10, theme::GREEN, "high"),
        ConfidenceLevel::Medium => (5, 10, theme::YELLOW, "medium"),
        ConfidenceLevel::Low => (2, 10, theme::RED, "low"),
        ConfidenceLevel::Unknown => (0, 10, theme::TEXT_DIM, "unknown"),
    }
}

fn risk_bar(level: &RiskLevel) -> (usize, usize, Color, &'static str) {
    match level {
        RiskLevel::Low => (2, 10, theme::GREEN, "low"),
        RiskLevel::Medium => (5, 10, theme::YELLOW, "medium"),
        RiskLevel::High => (7, 10, theme::RED, "high"),
        RiskLevel::Critical => (10, 10, theme::RED, "critical"),
        RiskLevel::Unknown => (0, 10, theme::TEXT_DIM, "unknown"),
    }
}