cliboard 0.1.0

Live math whiteboard for your terminal — render LaTeX equations in the browser with per-step AI chat
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
mod cli;
mod document;
mod export;
mod parser;
mod render;
mod server;
mod session;
mod unicode;

use clap::Parser;
use cli::{Cli, Command};
use session::{ChatMessage, ChatRole, Session};

fn main() {
    let cli = Cli::parse();

    if let Err(e) = run(cli.command) {
        eprintln!("Error: {}", e);
        std::process::exit(1);
    }
}

fn run(command: Command) -> Result<(), Box<dyn std::error::Error>> {
    match command {
        Command::New { title } => cmd_new(&title),
        Command::Step { title, latex } => cmd_step(&title, &latex),
        Command::Eq { latex } => cmd_eq(&latex),
        Command::Note { text } => cmd_note(&text),
        Command::Text { text } => cmd_text(&text),
        Command::Result { title, latex } => cmd_result(&title, &latex),
        Command::Divider => cmd_divider(),
        Command::Render { latex, output } => cmd_render(&latex, output.as_deref()),
        Command::Stop => cmd_stop(),
        Command::Export { output } => cmd_export(&output),
        Command::Status => cmd_status(),
        Command::Selection { json, latex } => cmd_selection(json, latex),
        Command::Chat { all, step, json } => cmd_chat(all, step, json),
        Command::Reply { step_id, text } => cmd_reply(step_id, text),
        Command::Listen { json } => cmd_listen(json),
        Command::Update { check } => cmd_update(check),
    }
}

fn require_session() -> Result<Session, Box<dyn std::error::Error>> {
    Session::find_current()
        .ok_or_else(|| "No active session. Run `cliboard new \"title\"` first.".into())
}

fn count_steps(content: &str) -> usize {
    content
        .lines()
        .filter(|line| line.starts_with("## "))
        .count()
}

fn cmd_new(title: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = Session::create(title)?;

    let port = server::find_available_port(8377)?;
    let url = format!("http://localhost:{}", port);

    // Always open in the system default browser
    let _ = open::that(&url);

    println!("Board live at {}", url);

    // Block the main thread so the server stays alive until Ctrl+C
    server::start_server(&session, port)?;

    Ok(())
}

fn cmd_step(title: &str, latex: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = format!("\n## {}\n\n$${}$$\n", title, latex);
    session.append(&content)?;
    let board = session.read_board()?;
    let n = count_steps(&board);
    println!("Step {} added: \"{}\"", n, title);
    Ok(())
}

fn cmd_eq(latex: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = format!("\n$${}$$\n", latex);
    session.append(&content)?;
    println!("Equation added");
    Ok(())
}

fn cmd_note(text: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = format!("\n> {}\n", text);
    session.append(&content)?;
    println!("Note added");
    Ok(())
}

fn cmd_text(text: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = format!("\n{}\n", text);
    session.append(&content)?;
    println!("Text added");
    Ok(())
}

fn cmd_result(title: &str, latex: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = format!("\n## {} {{.result}}\n\n$${}$$\n", title, latex);
    session.append(&content)?;
    println!("Result added: \"{}\"", title);
    Ok(())
}

fn cmd_divider() -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    session.append("\n---\n")?;
    println!("Divider added");
    Ok(())
}

fn cmd_render(latex: &str, output: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
    let rendered = render::render_equation(latex)
        .map_err(|e| format!("KaTeX error: {}", e))?;

    let html = format!(
        r#"<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.22/dist/katex.min.css">
<style>body {{ display:flex; justify-content:center; align-items:center; min-height:100vh; margin:0; background:#1a1a2e; color:#e0e0e0; }}</style>
</head><body>{}</body></html>"#,
        rendered
    );

    match output {
        Some(path) => {
            std::fs::write(path, &html)?;
            println!("Rendered to {}", path);
        }
        None => {
            let tmp = tempfile::Builder::new()
                .suffix(".html")
                .tempfile()?;
            let tmp_path = tmp.into_temp_path();
            std::fs::write(&tmp_path, &html)?;
            let _ = open::that(tmp_path.to_str().unwrap_or(""));
            // Give the browser time to open the file before it's cleaned up
            std::thread::sleep(std::time::Duration::from_secs(2));
            println!("Opened in browser");
        }
    }

    Ok(())
}

fn cmd_stop() -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;

    if let Some(pid) = session.read_pid() {
        let _ = std::process::Command::new("kill")
            .arg(pid.to_string())
            .status();
        session.remove_pid();
        println!("Server stopped (PID {})", pid);
    } else {
        println!("No server PID found");
    }

    Ok(())
}

fn cmd_export(output: &str) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let content = session.read_board()?;
    let doc = parser::parse(&content);
    export::export_html(&doc, output)?;
    println!("Exported to {}", output);
    Ok(())
}

fn cmd_status() -> Result<(), Box<dyn std::error::Error>> {
    match Session::find_current() {
        Some(session) => {
            let port = session.read_port();
            let pid = session.read_pid();
            let content = session.read_board().unwrap_or_default();
            let steps = count_steps(&content);

            let alive = pid
                .map(is_pid_alive)
                .unwrap_or(false);

            if alive {
                let port_str = port
                    .map(|p| format!(":{}", p))
                    .unwrap_or_else(|| "unknown port".to_string());
                println!("Running on {}, {} steps", port_str, steps);
            } else {
                println!("Not running ({} steps in board)", steps);
            }
        }
        None => {
            println!("No active session");
        }
    }
    Ok(())
}

fn cmd_selection(json: bool, latex: bool) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;

    match session.read_selection() {
        Some(selection) => {
            if json {
                let json_str = serde_json::to_string_pretty(&selection)?;
                println!("{}", json_str);
            } else if latex {
                println!("{}", selection.latex);
            } else {
                println!("Step {}: {}", selection.step_id, selection.title);
                println!("LaTeX: {}", selection.latex);
                println!("Unicode: {}", selection.unicode);
            }
        }
        None => {
            println!("No selection yet. Click an equation on the board first.");
        }
    }

    Ok(())
}

fn cmd_chat(all: bool, step: Option<usize>, json: bool) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let store = session.read_messages()?;

    let messages = if let Some(step_id) = step {
        store
            .messages
            .into_iter()
            .filter(|m| m.step_id == step_id)
            .collect()
    } else if all {
        store.messages
    } else {
        // Show pending (unanswered) questions
        session.pending_messages()?
    };

    if json {
        println!("{}", serde_json::to_string_pretty(&messages)?);
        return Ok(());
    }

    if messages.is_empty() {
        println!("No messages.");
        return Ok(());
    }

    for msg in &messages {
        let role_prefix = match msg.role {
            ChatRole::User => "Q",
            ChatRole::Assistant => "A",
        };
        println!("[Step {}] {}: {}", msg.step_id, role_prefix, msg.text);
        if let Some(ctx) = &msg.context {
            if let (Some(selected), Some(latex)) = (&ctx.selected, &ctx.latex) {
                let title_part = ctx
                    .step_title
                    .as_deref()
                    .filter(|t| !t.is_empty())
                    .map(|t| format!(" ({})", t))
                    .unwrap_or_default();
                println!(
                    "  -> selected: \"{}\" in {}{}",
                    selected, latex, title_part
                );
            }
        }
    }
    Ok(())
}

fn cmd_reply(step_id: usize, text: String) -> Result<(), Box<dyn std::error::Error>> {
    let session = require_session()?;
    let rendered = render::render_reply_content(&text, step_id);
    let timestamp_ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)?
        .as_millis();
    let msg = ChatMessage {
        id: format!("{:x}", timestamp_ms),
        step_id,
        role: ChatRole::Assistant,
        text,
        rendered,
        timestamp: chrono::Local::now().to_rfc3339(),
        context: None,
    };
    session.append_message(msg)?;
    println!("Reply sent to step {}.", step_id);
    Ok(())
}

fn cmd_listen(json: bool) -> Result<(), Box<dyn std::error::Error>> {
    use notify::{EventKind, RecursiveMode, Watcher};
    use std::collections::HashSet;

    let session = require_session()?;
    let messages_path = session.messages_path();

    // Track which message IDs we've already printed
    let mut seen: HashSet<String> = {
        let store = session.read_messages()?;
        store.messages.iter().map(|m| m.id.clone()).collect()
    };

    eprintln!("Listening for chat questions... (Ctrl+C to stop)");

    let (tx, rx) = std::sync::mpsc::channel();
    let mut watcher = notify::recommended_watcher(tx)?;

    let watch_dir = messages_path
        .parent()
        .unwrap_or(std::path::Path::new("."));
    watcher.watch(watch_dir, RecursiveMode::NonRecursive)?;

    for event_result in rx {
        match event_result {
            Ok(event) => {
                let dominated = matches!(
                    event.kind,
                    EventKind::Modify(_) | EventKind::Create(_)
                );
                let affects = event.paths.iter().any(|p| p == &messages_path);

                if dominated && affects {
                    if let Ok(store) = session.read_messages() {
                        for msg in &store.messages {
                            if msg.role == ChatRole::User && !seen.contains(&msg.id) {
                                seen.insert(msg.id.clone());
                                if json {
                                    println!("{}", serde_json::to_string(msg)?);
                                } else {
                                    print!("[Step {}] {}", msg.step_id, msg.text);
                                    if let Some(ctx) = &msg.context {
                                        if let Some(sel) = &ctx.selected {
                                            print!("  (re: \"{}\")", sel);
                                        }
                                    }
                                    println!();
                                }
                                // Flush immediately so piped consumers see it
                                use std::io::Write;
                                let _ = std::io::stdout().flush();
                            }
                        }
                    }
                }
            }
            Err(e) => eprintln!("Watch error: {}", e),
        }
    }

    Ok(())
}

const GITHUB_REPO: &str = "maxwellsdm1867/cliboard";

fn cmd_update(check_only: bool) -> Result<(), Box<dyn std::error::Error>> {
    let current = env!("CARGO_PKG_VERSION");
    println!("cliboard v{}", current);
    println!("Checking for updates...");

    // Fetch latest release from GitHub API
    let output = std::process::Command::new("curl")
        .args([
            "-fsSL",
            "-H",
            "Accept: application/vnd.github+json",
            &format!(
                "https://api.github.com/repos/{}/releases/latest",
                GITHUB_REPO
            ),
        ])
        .output()?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("404") || output.status.code() == Some(22) {
            println!("No releases published yet.");
            println!(
                "Build from source: cargo install --path . (or cargo build --release)"
            );
            return Ok(());
        }
        return Err("Failed to check for updates. Check your internet connection.".into());
    }

    let body = String::from_utf8(output.stdout)?;
    let release: serde_json::Value = serde_json::from_str(&body)?;

    let latest = release["tag_name"]
        .as_str()
        .ok_or("Could not parse latest version")?
        .trim_start_matches('v');

    if latest == current {
        println!("Already on the latest version.");
        return Ok(());
    }

    println!("New version available: v{} -> v{}", current, latest);

    if check_only {
        println!(
            "Run `cliboard update` to install, or visit:\n  https://github.com/{}/releases/tag/v{}",
            GITHUB_REPO, latest
        );
        return Ok(());
    }

    // Determine platform target triple
    let target = target_triple();

    // Find the right asset in the release
    let asset_name = format!("cliboard-{}.tar.gz", target);
    let assets = release["assets"]
        .as_array()
        .ok_or("No assets in release")?;

    // cargo-dist may use slightly different naming, try both patterns
    let download_url = assets
        .iter()
        .find(|a| {
            let name = a["name"].as_str().unwrap_or("");
            name == asset_name || name.contains(target)
        })
        .and_then(|a| a["browser_download_url"].as_str())
        .ok_or_else(|| {
            format!(
                "No prebuilt binary for your platform ({}).\n\
                 Install from source: cargo install --path .",
                target
            )
        })?;

    println!("Downloading...");

    let tmp_dir = tempfile::tempdir()?;
    let tmp_archive = tmp_dir.path().join("cliboard.tar.gz");

    let status = std::process::Command::new("curl")
        .args(["-fsSL", "-o"])
        .arg(&tmp_archive)
        .arg(download_url)
        .status()?;

    if !status.success() {
        return Err("Download failed.".into());
    }

    // Extract
    let status = std::process::Command::new("tar")
        .arg("xzf")
        .arg(&tmp_archive)
        .arg("-C")
        .arg(tmp_dir.path())
        .status()?;

    if !status.success() {
        return Err("Failed to extract archive.".into());
    }

    // Find the cliboard binary in extracted files (cargo-dist may nest it)
    let new_bin = find_binary_in_dir(tmp_dir.path())
        .ok_or("Could not find cliboard binary in downloaded archive.")?;

    // Replace current binary
    let current_exe = std::env::current_exe()?;
    let backup = current_exe.with_extension("old");

    // Backup current binary
    std::fs::rename(&current_exe, &backup)?;

    match std::fs::copy(&new_bin, &current_exe) {
        Ok(_) => {
            // Set executable permissions on Unix
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                std::fs::set_permissions(
                    &current_exe,
                    std::fs::Permissions::from_mode(0o755),
                )?;
            }
            let _ = std::fs::remove_file(&backup);
            println!("Updated to v{}!", latest);
        }
        Err(e) => {
            // Restore from backup
            let _ = std::fs::rename(&backup, &current_exe);
            return Err(format!("Install failed: {}. Restored previous version.", e).into());
        }
    }

    Ok(())
}

/// Find the cliboard binary in a directory tree (handles cargo-dist nesting).
fn find_binary_in_dir(dir: &std::path::Path) -> Option<std::path::PathBuf> {
    let bin_name = if cfg!(windows) {
        "cliboard.exe"
    } else {
        "cliboard"
    };

    // Check top level
    let direct = dir.join(bin_name);
    if direct.exists() {
        return Some(direct);
    }

    // Check one level deep (cargo-dist puts it in a subdirectory)
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let nested = entry.path().join(bin_name);
            if nested.exists() {
                return Some(nested);
            }
        }
    }

    None
}

/// Get the target triple for the current platform.
fn target_triple() -> &'static str {
    #[cfg(all(target_arch = "aarch64", target_os = "macos"))]
    { "aarch64-apple-darwin" }
    #[cfg(all(target_arch = "x86_64", target_os = "macos"))]
    { "x86_64-apple-darwin" }
    #[cfg(all(target_arch = "x86_64", target_os = "linux"))]
    { "x86_64-unknown-linux-gnu" }
    #[cfg(all(target_arch = "aarch64", target_os = "linux"))]
    { "aarch64-unknown-linux-gnu" }
    #[cfg(all(target_arch = "x86_64", target_os = "windows"))]
    { "x86_64-pc-windows-msvc" }
    #[cfg(all(target_arch = "aarch64", target_os = "windows"))]
    { "aarch64-pc-windows-msvc" }
    #[cfg(not(any(
        all(target_arch = "aarch64", target_os = "macos"),
        all(target_arch = "x86_64", target_os = "macos"),
        all(target_arch = "x86_64", target_os = "linux"),
        all(target_arch = "aarch64", target_os = "linux"),
        all(target_arch = "x86_64", target_os = "windows"),
        all(target_arch = "aarch64", target_os = "windows"),
    )))]
    { "unknown" }
}

/// Check if a process with the given PID is alive.
fn is_pid_alive(pid: u32) -> bool {
    // kill -0 checks if process exists without sending a signal
    std::process::Command::new("kill")
        .args(["-0", &pid.to_string()])
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}