primum 0.1.0

Prioritized work items as plain markdown files, with a command line and a local web board
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! The command line. Writes go through `store`, reads through `store::load`
//! and the same `ItemView` the web page gets, so the terminal and the page
//! always agree.
use crate::model::{KINDS, STATUSES, parse_id};
use crate::projects;
use crate::server::{ItemView, views};
use crate::store::{self, Changes, Project};
use anyhow::{Context, Result, anyhow, bail};
use clap::{Args, Parser, Subcommand};
use std::io::{IsTerminal, Read, Write};
use std::path::PathBuf;

#[derive(Parser)]
#[command(
    name = "primum",
    version,
    about = "Prioritized work items as plain markdown files"
)]
pub struct Cli {
    /// The project, by name; default: the one containing this folder, or the only one
    #[arg(short, long, global = true)]
    pub project: Option<String>,
    #[command(subcommand)]
    pub cmd: Cmd,
}

#[derive(Args, Default)]
pub struct Scores {
    /// How much it matters, 1-10
    #[arg(long, value_parser = clap::value_parser!(i64).range(1..=10))]
    pub impact: Option<i64>,
    /// How soon it matters, 1-10
    #[arg(long, value_parser = clap::value_parser!(i64).range(1..=10))]
    pub urgency: Option<i64>,
    /// What goes wrong while it waits, 1-10
    #[arg(long, value_parser = clap::value_parser!(i64).range(1..=10))]
    pub risk: Option<i64>,
    /// How much work it is, 1-10
    #[arg(long, value_parser = clap::value_parser!(i64).range(1..=10))]
    pub effort: Option<i64>,
}

#[derive(Subcommand)]
pub enum Cmd {
    /// Make this folder a project (primum.toml and items/) and list it
    Init {
        /// The project's name; default: the folder's
        #[arg(long)]
        name: Option<String>,
    },
    /// The projects this machine shows, or add or remove one
    Projects {
        #[command(subcommand)]
        cmd: Option<ProjectsCmd>,
    },
    /// Add an item; prints its id
    Add {
        /// URGENT, BUG, DEBT, KILLER or TODO
        kind: String,
        title: String,
        #[command(flatten)]
        scores: Scores,
        /// Ids it waits for, like TODO:1,BUG:2
        #[arg(long)]
        needs: Option<String>,
        /// Its text, from a markdown file (- for standard input)
        #[arg(long)]
        body_file: Option<PathBuf>,
    },
    /// Items in priority order; open and in progress unless told otherwise
    List {
        #[arg(long)]
        kind: Option<String>,
        /// Statuses to show, like open,doing
        #[arg(long)]
        status: Option<String>,
        /// Every item, closed ones too
        #[arg(long)]
        all: bool,
        #[arg(long)]
        json: bool,
    },
    /// One item: scores, needs, text and documents
    Show {
        id: String,
        #[arg(long)]
        json: bool,
    },
    /// Change an item's fields
    Set {
        id: String,
        #[arg(long)]
        title: Option<String>,
        #[command(flatten)]
        scores: Scores,
        /// open, doing, done or wontfix
        #[arg(long)]
        status: Option<String>,
        /// Ids it waits for, like TODO:1,BUG:2 ("" for none)
        #[arg(long)]
        needs: Option<String>,
        /// Replace its text with a markdown file (- for standard input)
        #[arg(long)]
        body_file: Option<PathBuf>,
    },
    /// Mark an item in progress
    Start { id: String },
    /// Mark an item done
    Done { id: String },
    /// Append a dated note to an item's text
    Note {
        id: String,
        /// The note, from a markdown file (- for standard input)
        #[arg(long)]
        file: PathBuf,
    },
    /// Add or replace an item's document, items/<ID>.<TYPE>.md
    Doc {
        id: String,
        /// The document's type, like spec, plan or report
        doc_type: String,
        #[arg(long)]
        file: PathBuf,
    },
    /// Move an open item to another lane; it gets that lane's next id
    Kind { id: String, kind: String },
    /// Delete an item and its documents
    Delete {
        id: String,
        /// Do not ask
        #[arg(long)]
        yes: bool,
    },
    /// What to work on now
    Next {
        #[arg(short, default_value_t = 3)]
        n: usize,
    },
    /// Open the board in the browser
    Serve {
        #[arg(long, default_value_t = 8777)]
        port: u16,
        /// Do not open a browser
        #[arg(long)]
        no_open: bool,
    },
    /// Import a videre-board board into this (empty) project, once
    Import { board_dir: PathBuf },
}

#[derive(Subcommand)]
pub enum ProjectsCmd {
    /// List the project in PATH on this machine
    Add { path: PathBuf },
    /// Take a project off this machine's list (the project is not touched)
    Remove { name: String },
}

/// Runs the command line; returns the exit code.
pub fn run(args: Vec<String>) -> i32 {
    let cli = match Cli::try_parse_from(args) {
        Ok(c) => c,
        Err(e) => {
            let _ = e.print();
            return e.exit_code();
        }
    };
    match dispatch(cli) {
        Ok(()) => 0,
        Err(e) => {
            eprintln!("primum: {e:#}");
            1
        }
    }
}

fn dispatch(cli: Cli) -> Result<()> {
    let cwd = std::env::current_dir().context("no working directory")?;
    let project = || projects::resolve(cli.project.as_deref(), &cwd);
    match cli.cmd {
        Cmd::Init { name } => {
            let p = projects::init(&cwd, name.as_deref())?;
            println!("primum project {} in {}", p.name, p.root.display());
        }
        Cmd::Projects { cmd: None } => {
            for e in projects::list()? {
                match e.project {
                    Ok(p) => println!("{}\t{}\tok", p.name, e.path.display()),
                    Err(why) => println!("-\t{}\t{why}", e.path.display()),
                }
            }
        }
        Cmd::Projects {
            cmd: Some(ProjectsCmd::Add { path }),
        } => {
            let p = projects::add(&path)?;
            println!("{}\t{}", p.name, p.root.display());
        }
        Cmd::Projects {
            cmd: Some(ProjectsCmd::Remove { name }),
        } => projects::remove(&name)?,
        Cmd::Add {
            kind,
            title,
            scores,
            needs,
            body_file,
        } => {
            let c = Changes {
                needs: needs.as_deref().map(parse_needs).transpose()?,
                body: body_file.as_ref().map(read_text).transpose()?,
                ..scores_changes(&scores)
            };
            println!(
                "{}",
                store::insert(&project()?, &kind.to_uppercase(), &title, c)?
            );
        }
        Cmd::List {
            kind,
            status,
            all,
            json,
        } => list(&project()?, kind, status, all, json)?,
        Cmd::Show { id, json } => show(&project()?, &norm_id(&id)?, json)?,
        Cmd::Set {
            id,
            title,
            scores,
            status,
            needs,
            body_file,
        } => {
            let id = norm_id(&id)?;
            let c = Changes {
                title,
                status,
                needs: needs.as_deref().map(parse_needs).transpose()?,
                body: body_file.as_ref().map(read_text).transpose()?,
                ..scores_changes(&scores)
            };
            let named = changed_fields(&c);
            if named.is_empty() {
                bail!("nothing to change: name a field, like --status done");
            }
            store::update(&project()?, &id, c)?;
            println!("{id}: {}", named.join(", "));
        }
        Cmd::Start { id } => set_status(&project()?, &id, "doing")?,
        Cmd::Done { id } => set_status(&project()?, &id, "done")?,
        Cmd::Note { id, file } => {
            let id = norm_id(&id)?;
            store::append_note(&project()?, &id, &read_text(&file)?)?;
            println!("{id}: note {}", chrono::Local::now().format("%Y-%m-%d"));
        }
        Cmd::Doc { id, doc_type, file } => {
            let path = store::set_doc(&project()?, &norm_id(&id)?, &doc_type, &read_text(&file)?)?;
            println!("{path}");
        }
        Cmd::Kind { id, kind } => {
            let id = norm_id(&id)?;
            let new = store::set_kind(&project()?, &id, &kind.to_uppercase())?;
            println!("{id} -> {new}");
        }
        Cmd::Delete { id, yes } => delete(&project()?, &norm_id(&id)?, yes)?,
        Cmd::Next { n } => next(&project()?, n)?,
        Cmd::Serve { port, no_open } => crate::server::serve(port, !no_open)?,
        Cmd::Import { board_dir } => {
            let p = project()?;
            let r = crate::import::run(&board_dir, &p)?;
            println!("{} items into {}", r.items, p.items_dir().display());
            for (from, to) in &r.docs {
                println!("  {from} -> {to}");
            }
            for (id, path) in &r.shared {
                println!("  {id} also shows {path}");
            }
            if !r.left.is_empty() {
                println!("left in {} (linked to no item):", board_dir.display());
                for f in &r.left {
                    println!("  {f}");
                }
            }
        }
    }
    Ok(())
}

fn scores_changes(s: &Scores) -> Changes {
    Changes {
        impact: s.impact,
        urgency: s.urgency,
        risk: s.risk,
        effort: s.effort,
        ..Default::default()
    }
}

fn changed_fields(c: &Changes) -> Vec<&'static str> {
    [
        ("title", c.title.is_some()),
        ("status", c.status.is_some()),
        ("impact", c.impact.is_some()),
        ("urgency", c.urgency.is_some()),
        ("risk", c.risk.is_some()),
        ("effort", c.effort.is_some()),
        ("needs", c.needs.is_some()),
        ("body", c.body.is_some()),
    ]
    .into_iter()
    .filter(|(_, set)| *set)
    .map(|(name, _)| name)
    .collect()
}

fn set_status(p: &Project, id: &str, status: &str) -> Result<()> {
    let id = norm_id(id)?;
    let c = Changes {
        status: Some(status.into()),
        ..Default::default()
    };
    store::update(p, &id, c)?;
    println!("{id}: {status}");
    Ok(())
}

/// `todo:3` is `TODO:3`; anything that is not an id is an error.
fn norm_id(id: &str) -> Result<String> {
    let up = id.trim().to_uppercase();
    parse_id(&up).ok_or_else(|| anyhow!("{id} is not an item id, like TODO:3"))?;
    Ok(up)
}

/// `TODO:1, bug:2` is `[TODO:1, BUG:2]`; an empty string is no needs.
pub fn parse_needs(s: &str) -> Result<Vec<String>> {
    s.split(',')
        .map(str::trim)
        .filter(|x| !x.is_empty())
        .map(norm_id)
        .collect()
}

/// A file's text; `-` is standard input.
fn read_text(path: &PathBuf) -> Result<String> {
    if path.as_os_str() == "-" {
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s)?;
        return Ok(s);
    }
    std::fs::read_to_string(path).map_err(|_| anyhow!("no such file: {}", path.display()))
}

fn list(
    p: &Project,
    kind: Option<String>,
    status: Option<String>,
    all: bool,
    json: bool,
) -> Result<()> {
    let statuses: Vec<String> = match (&status, all) {
        (Some(s), _) => s.split(',').map(|x| x.trim().to_lowercase()).collect(),
        (None, true) => STATUSES.iter().map(|s| s.to_string()).collect(),
        (None, false) => vec!["open".into(), "doing".into()],
    };
    if let Some(bad) = statuses.iter().find(|s| !STATUSES.contains(&s.as_str())) {
        bail!("unknown status {bad}");
    }
    let kind = kind.map(|k| k.to_uppercase());
    if let Some(k) = &kind
        && !KINDS.contains(&k.as_str())
    {
        bail!("unknown kind {k}");
    }
    let board = store::load(p)?;
    let rows: Vec<ItemView> = views(&board)
        .into_iter()
        .filter(|v| kind.as_ref().is_none_or(|k| &v.kind == k))
        .filter(|v| statuses.contains(&v.status))
        .collect();
    if json {
        println!("{}", serde_json::to_string_pretty(&rows)?);
        return Ok(());
    }
    let style = Style::detect();
    println!("{}", header(&style));
    for v in &rows {
        println!("{}", row_line(v, &style));
    }
    Ok(())
}

fn show(p: &Project, id: &str, json: bool) -> Result<()> {
    let board = store::load(p)?;
    let it = board.get(id).ok_or_else(|| anyhow!("no item {id}"))?;
    let v = ItemView::new(it, &board);
    if json {
        println!("{}", serde_json::to_string_pretty(&v)?);
        return Ok(());
    }
    let s = Style::detect();
    println!("{} {}", s.badge(&v.kind, &v.id), s.paint(&v.title, "1"));
    println!(
        "impact {}  urgency {}  risk {}  effort {}  score {:.1}  {}",
        v.impact, v.urgency, v.risk, v.effort, v.score, v.status
    );
    if !v.needs.is_empty() {
        let blocked = if v.blocked {
            format!(" (blocked by {})", v.blockers.join(", "))
        } else {
            String::new()
        };
        println!("needs {}{blocked}", v.needs.join(", "));
    }
    if let Some(f) = &v.finished {
        println!("finished {f}");
    }
    println!(
        "{}",
        s.paint(&format!("{}/{}", p.root.display(), v.file), "2")
    );
    if !v.body.trim().is_empty() {
        println!();
        print!("{}", render_markdown(&v.body, &s));
    }
    if !v.docs.is_empty() {
        println!();
        println!("{}", s.paint("documents", "1"));
        for d in &v.docs {
            println!("  {:<8} {}", d.kind, d.path);
        }
    }
    Ok(())
}

fn delete(p: &Project, id: &str, yes: bool) -> Result<()> {
    if !yes {
        let board = store::load(p)?;
        board.get(id).ok_or_else(|| anyhow!("no item {id}"))?;
        let own = board
            .docs
            .get(id)
            .map(|d| {
                let prefix = format!("items/{}.", crate::item_file::stem(id));
                d.iter().filter(|x| x.path.starts_with(&prefix)).count()
            })
            .unwrap_or(0);
        if !std::io::stdin().is_terminal() {
            bail!("deleting {id} needs --yes when not run from a terminal");
        }
        print!("Delete {id} and {own} documents? [y/N] ");
        std::io::stdout().flush()?;
        let mut answer = String::new();
        std::io::stdin().read_line(&mut answer)?;
        if !matches!(answer.trim(), "y" | "Y" | "yes") {
            bail!("not deleted");
        }
    }
    for f in store::delete(p, id)? {
        println!("{f}");
    }
    Ok(())
}

/// Finish what is started before starting more: the items in progress
/// first, then the top open, unblocked ones, as the page's "next up".
fn next(p: &Project, n: usize) -> Result<()> {
    let board = store::load(p)?;
    let rows = views(&board);
    let doing: Vec<&str> = rows
        .iter()
        .filter(|v| v.status == "doing")
        .map(|v| v.id.as_str())
        .collect();
    if !doing.is_empty() {
        println!("in progress: {}", doing.join(", "));
    }
    let style = Style::detect();
    let open: Vec<&ItemView> = rows
        .iter()
        .filter(|v| v.status == "open" && !v.blocked)
        .take(n)
        .collect();
    if open.is_empty() {
        println!("nothing unblocked and open.");
    }
    for v in open {
        println!("{}", row_line(v, &style));
    }
    Ok(())
}

// ---------------------------------------------------------------- terminal

/// ANSI styling, off when stdout is not a terminal or NO_COLOR is set.
pub struct Style {
    color: bool,
    width: usize,
}

impl Style {
    fn detect() -> Style {
        let color = std::io::stdout().is_terminal()
            && std::env::var_os("NO_COLOR").is_none_or(|v| v.is_empty());
        let width = std::env::var("COLUMNS")
            .ok()
            .and_then(|c| c.parse().ok())
            .unwrap_or(120);
        Style { color, width }
    }

    fn paint(&self, text: &str, codes: &str) -> String {
        if self.color {
            format!("\x1b[{codes}m{text}\x1b[0m")
        } else {
            text.to_string()
        }
    }

    /// The id on its lane's color, as on the page.
    fn badge(&self, kind: &str, id: &str) -> String {
        let (r, g, b) = kind_rgb(kind);
        self.paint(&format!(" {id:<9}"), &format!("1;97;48;2;{r};{g};{b}"))
    }
}

fn kind_rgb(kind: &str) -> (u8, u8, u8) {
    match kind {
        "URGENT" => (0xB6, 0x3F, 0x3F),
        "BUG" => (0x9A, 0x6A, 0x16),
        "DEBT" => (0x4F, 0x6F, 0x99),
        "KILLER" => (0x3F, 0x7E, 0x69),
        _ => (0x75, 0x68, 0x9A),
    }
}

fn title_width(s: &Style) -> usize {
    s.width.saturating_sub(60).max(20)
}

fn truncate(s: &str, n: usize) -> String {
    if s.chars().count() <= n {
        return s.to_string();
    }
    let mut t: String = s.chars().take(n.saturating_sub(1)).collect();
    t.push('…');
    t
}

fn pad(s: &str, n: usize) -> String {
    let len = s.chars().count();
    format!("{s}{}", " ".repeat(n.saturating_sub(len)))
}

fn header(s: &Style) -> String {
    let line = format!(
        "{} {} imp urg rsk eff score {} status",
        pad("id", 10),
        pad("title", title_width(s)),
        pad("needs", 16)
    );
    s.paint(&line, "2")
}

fn row_line(v: &ItemView, s: &Style) -> String {
    let id = if s.color {
        s.badge(&v.kind, &v.id)
    } else {
        pad(&v.id, 10)
    };
    let needs = if v.blocked {
        v.blockers.join(",")
    } else {
        v.needs.join(",")
    };
    let rest = format!(
        " {} {:>3} {:>3} {:>3} {:>3} {:>5.1} {} {}",
        pad(&truncate(&v.title, title_width(s)), title_width(s)),
        v.impact,
        v.urgency,
        v.risk,
        v.effort,
        v.score,
        pad(&truncate(&needs, 16), 16),
        v.status
    );
    let rest = if v.status == "doing" {
        s.paint(&rest, "1")
    } else if v.blocked || v.status == "done" || v.status == "wontfix" {
        s.paint(&rest, "2")
    } else {
        rest
    };
    format!("{id}{rest}")
}

/// Markdown for the terminal: headings bold, fenced code dim, nothing
/// reflowed.
fn render_markdown(md: &str, s: &Style) -> String {
    let mut out = String::new();
    let mut fenced = false;
    for line in md.lines() {
        let styled = if line.trim_start().starts_with("```") {
            fenced = !fenced;
            s.paint(line, "2")
        } else if fenced {
            s.paint(line, "2")
        } else if line.starts_with('#') {
            s.paint(line, "1")
        } else {
            line.to_string()
        };
        out += &styled;
        out.push('\n');
    }
    out
}