crepuscularity-cli 0.7.4

crepus CLI — scaffolding and builds for Crepuscularity (UNSTABLE; in active development).
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
//! `crepus tui` — Terminal User Interface apps with Ratatui.
//!
//! Scaffold and build TUI applications that render `.crepus` templates in the terminal
//! using Ratatui for layout and Crossterm for input handling.

use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

use console::style;

use crate::ui;

/// Caret-style version requirement injected into scaffolded TUI apps for
/// `crepuscularity-tui`. Keep this aligned with the published `crepuscularity-tui`
/// minor; pre-1.0 caret on `0.x.y` resolves to `>=0.x.y, <0.(x+1).0`.
const SCAFFOLD_TUI_VERSION: &str = "0.4";

pub fn run(args: &[String]) {
    match args.first().map(|s| s.as_str()) {
        Some("new") => {
            let name = args.get(1).map(|s| s.as_str()).unwrap_or_else(|| {
                ui::error("Usage: crepus tui new <name>");
            });
            scaffold_tui_app(name);
        }
        Some("build") => {
            let release = args.iter().any(|a| a == "--release");
            let extra = args
                .iter()
                .skip(1)
                .filter(|a| !matches!(a.as_str(), "--release"))
                .cloned()
                .collect::<Vec<_>>();
            build_tui_app(release, &extra);
        }
        Some("run") => {
            let release = args.iter().any(|a| a == "--release");
            let extra = args
                .iter()
                .skip(1)
                .filter(|a| !matches!(a.as_str(), "--release"))
                .cloned()
                .collect::<Vec<_>>();
            run_tui_app(release, &extra);
        }
        Some("preview") => {
            let path = args.get(1).map(PathBuf::from).unwrap_or_else(|| {
                ui::error("Usage: crepus tui preview <file.crepus>");
            });
            preview_tui_template(&path);
        }
        _ => print_tui_usage(),
    }
}

fn scaffold_tui_app(name: &str) {
    let dir = Path::new(name);

    // Create directory structure
    fs::create_dir_all(dir).unwrap_or_else(|e| {
        ui::error(&format!("failed to create directory: {}", e));
    });

    let cargo_toml = format!(
        r#"[package]
name = "{name}"
version = "0.1.0"
edition = "2021"

[dependencies]
# Bump these as needed; the scaffold is pinned to the version of `crepus` you
# scaffolded with.
crepuscularity-tui = "{tui_version}"
ratatui = {{ version = "0.29", default-features = false, features = ["crossterm"] }}
crossterm = "0.28"
anyhow = "1.0"
"#,
        name = name,
        tui_version = SCAFFOLD_TUI_VERSION,
    );

    let main_rs = r###"//! Generated by `crepus tui new`. Edit `app.crepus` and run `crepus tui run`
//! (or `cargo run`). The template hot-reloads on save.

use std::time::Duration;

use crepuscularity_tui::{HotTemplate, ReloadOutcome};
use crossterm::event::{Event, KeyCode};
use ratatui::prelude::*;

fn main() -> anyhow::Result<()> {
    let mut hot = HotTemplate::watch("app.crepus").map_err(|e| anyhow::anyhow!(e))?;
    hot.template_mut()
        .set("title", "My TUI App")
        .set("message", "Hello from Crepuscularity!")
        .set("quit_hint", "Press 'q' to quit");

    let mut terminal = ratatui::init();
    let result = run(&mut terminal, &mut hot);
    ratatui::restore();
    result
}

fn run(terminal: &mut Terminal<impl Backend>, hot: &mut HotTemplate) -> anyhow::Result<()> {
    loop {
        terminal.draw(|frame| {
            let _ = hot.poll_and_draw_full(frame);
        })?;

        if crossterm::event::poll(Duration::from_millis(100))? {
            if let Event::Key(key) = crossterm::event::read()? {
                if matches!(key.code, KeyCode::Char('q') | KeyCode::Esc) {
                    return Ok(());
                }
            }
        }

        // Avoid an unused-variant warning if a future ReloadOutcome::Error is added.
        let _ = ReloadOutcome::Unchanged;
    }
}
"###;
    fs::write(dir.join("Cargo.toml"), cargo_toml).unwrap_or_else(|e| {
        ui::error(&format!("failed to write Cargo.toml: {}", e));
    });

    // Create src directory
    fs::create_dir_all(dir.join("src")).unwrap_or_else(|e| {
        ui::error(&format!("failed to create src directory: {}", e));
    });

    // Write src/main.rs
    fs::write(dir.join("src/main.rs"), main_rs).unwrap_or_else(|e| {
        ui::error(&format!("failed to write src/main.rs: {}", e));
    });

    // Create a basic .crepus template file
    let template_content = r#"div bg-black text-white flex flex-col gap-4
  div text-2xl font-bold text-green-400
    "Welcome to {title}"
  div text-lg
    "{message}"
  div text-sm text-gray-500
    "{quit_hint}"
"#;

    fs::write(dir.join("app.crepus"), template_content).unwrap_or_else(|e| {
        ui::error(&format!("failed to write app.crepus: {}", e));
    });

    ui::success(&format!(
        "Created new TUI app '{}' in directory '{}'",
        name,
        dir.display()
    ));
    eprintln!("Run 'cd {} && cargo run' to start the app", name);
}

fn build_tui_app(release: bool, extra: &[String]) {
    ensure_cargo_project_for_tui();
    let mut cmd = Command::new("cargo");
    cmd.arg("build");
    if release {
        cmd.arg("--release");
    }
    cmd.args(extra);
    delegate_to_cargo(cmd, "build");
}

fn run_tui_app(release: bool, extra: &[String]) {
    ensure_cargo_project_for_tui();
    let mut cmd = Command::new("cargo");
    cmd.arg("run");
    if release {
        cmd.arg("--release");
    }
    if !extra.is_empty() {
        cmd.arg("--").args(extra);
    }
    delegate_to_cargo(cmd, "run");
}

fn ensure_cargo_project_for_tui() {
    if !Path::new("Cargo.toml").exists() {
        ui::error(
            "no Cargo.toml in the current directory. Run `crepus tui build|run` from the \
             root of a TUI app scaffolded with `crepus tui new <name>`.",
        );
    }
}

fn delegate_to_cargo(mut cmd: Command, verb: &str) {
    match cmd.status() {
        Ok(status) if status.success() => {}
        Ok(status) => {
            std::process::exit(status.code().unwrap_or(1));
        }
        Err(e) => {
            ui::error(&format!(
                "failed to invoke `cargo {verb}`: {e}. Is the Rust toolchain installed and on PATH?"
            ));
        }
    }
}

fn print_tui_usage() {
    eprintln!(
        "{}",
        style("crepus tui — Terminal User Interface applications")
            .cyan()
            .bold()
    );
    eprintln!();
    eprintln!("{}", style("COMMANDS").dim());
    eprintln!(
        "  {}  {}",
        style("new <name>      ").green(),
        style("scaffold a new TUI app").dim()
    );
    eprintln!(
        "  {}  {}",
        style("build [--release]").green(),
        style("build the TUI app").dim()
    );
    eprintln!(
        "  {}  {}",
        style("run              ").green(),
        style("run the TUI app").dim()
    );
    eprintln!(
        "  {}  {}",
        style("preview <file>  ").green(),
        style("live-preview a .crepus template in the terminal").dim()
    );
    eprintln!();
    eprintln!("{}", style("EXAMPLES").dim());
    eprintln!("  crepus tui new my-tui-app");
    eprintln!("  cd my-tui-app && crepus tui build");
    eprintln!("  crepus tui run");
    eprintln!("  crepus tui preview app.crepus     # hot-reload preview, q/Esc to quit");
}

/// Live-preview a `.crepus` template in the current terminal.
///
/// Sets up crossterm raw mode via `ratatui::init`, watches the file for
/// changes via [`crepuscularity_tui::HotTemplate`], and re-renders on each
/// save. `q` or `Esc` exits cleanly; `r` forces a reload. A `context.toml`
/// next to the template is loaded as initial variables.
fn preview_tui_template(path: &Path) {
    use crepuscularity_tui::{HotTemplate, ReloadOutcome};
    use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind};
    use std::time::{Duration, Instant};

    if !path.exists() {
        ui::error(&format!("file not found: {}", path.display()));
    }

    let mut hot = match HotTemplate::watch(path) {
        Ok(h) => h,
        Err(e) => ui::error(&format!("could not load template: {e}")),
    };

    if let Some(dir) = path.parent() {
        let ctx_path = dir.join("context.toml");
        if ctx_path.exists() {
            load_tui_context_toml(&ctx_path, hot.template_mut().context_mut());
        }
    }

    eprintln!(
        "{} previewing {} (q/Esc to quit, r to force reload)",
        style("crepus tui").dim(),
        style(path.display().to_string()).cyan().bold()
    );

    let mut terminal = ratatui::init();
    let mut last_reload: Option<(Instant, ReloadOutcome)> = None;

    let result: Result<(), String> = loop {
        let outcome_holder = std::cell::Cell::new(ReloadOutcome::Unchanged);
        if let Err(e) = terminal.draw(|frame| match hot.poll_and_draw_full(frame) {
            Ok(o) => outcome_holder.set(o),
            Err(_) => outcome_holder.set(ReloadOutcome::Unchanged),
        }) {
            break Err(format!("terminal draw: {e}"));
        }
        match outcome_holder.into_inner() {
            ReloadOutcome::Unchanged => {}
            other => last_reload = Some((Instant::now(), other)),
        }

        if matches!(event::poll(Duration::from_millis(100)), Ok(true)) {
            match event::read() {
                Ok(Event::Key(key)) if key.kind == KeyEventKind::Press => match key.code {
                    KeyCode::Char('q') | KeyCode::Esc => break Ok(()),
                    KeyCode::Char('r') => {
                        if let Err(e) = hot.template_mut().reload() {
                            last_reload = Some((Instant::now(), ReloadOutcome::Error(e)));
                        } else {
                            last_reload = Some((Instant::now(), ReloadOutcome::Reloaded));
                        }
                    }
                    _ => {}
                },
                Ok(_) => {}
                Err(e) => break Err(format!("event read: {e}")),
            }
        }

        if let Some((t, _)) = &last_reload {
            if t.elapsed() > Duration::from_secs(2) {
                last_reload = None;
            }
        }
    };
    ratatui::restore();

    if let Some((_, ReloadOutcome::Error(msg))) = last_reload {
        eprintln!("{} {}", style("").yellow(), msg);
    }
    if let Err(e) = result {
        ui::error(&e);
    }
}

/// Minimal TOML loader (key=value, strings/bools/ints/floats) shared with
/// `crepus preview`.  Lives here to avoid the `desktop` cfg-gate so non-GPUI
/// builds still get TUI preview support.
fn load_tui_context_toml(path: &Path, ctx: &mut crepuscularity_tui::TemplateContext) {
    use crepuscularity_tui::TemplateValue;
    let Ok(content) = std::fs::read_to_string(path) else {
        return;
    };
    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        if let Some(eq) = line.find('=') {
            let key = line[..eq].trim();
            let val = line[eq + 1..].trim();
            if val == "true" {
                ctx.set(key, TemplateValue::Bool(true));
            } else if val == "false" {
                ctx.set(key, TemplateValue::Bool(false));
            } else if let Ok(n) = val.parse::<i64>() {
                ctx.set(key, TemplateValue::Int(n));
            } else if let Ok(f) = val.parse::<f64>() {
                ctx.set(key, TemplateValue::Float(f));
            } else {
                ctx.set(key, strip_one_outer_quote_pair(val).to_string());
            }
        }
    }
}

/// Strip exactly one matching pair of `"`/`'` quotes from the outside of `s`.
///
/// Unlike `str::trim_matches('"').trim_matches('\'')` this preserves repeated
/// quotes inside the value, e.g. `"\"\"hi\"\""` → `\"hi\"` rather than `hi`.
fn strip_one_outer_quote_pair(s: &str) -> &str {
    let bytes = s.as_bytes();
    if bytes.len() >= 2 {
        let first = bytes[0];
        let last = bytes[bytes.len() - 1];
        if (first == b'"' && last == b'"') || (first == b'\'' && last == b'\'') {
            // Slice on byte indices that we just verified are `"` / `'` — both
            // ASCII, so we are always on a UTF-8 char boundary.
            return &s[1..s.len() - 1];
        }
    }
    s
}

#[cfg(test)]
mod tests {
    use super::strip_one_outer_quote_pair;

    #[test]
    fn strips_single_outer_double_quotes() {
        assert_eq!(strip_one_outer_quote_pair("\"hello\""), "hello");
    }

    #[test]
    fn strips_single_outer_single_quotes() {
        assert_eq!(strip_one_outer_quote_pair("'hi'"), "hi");
    }

    #[test]
    fn preserves_inner_quotes_when_repeated() {
        assert_eq!(
            strip_one_outer_quote_pair("\"\"keep\"\""),
            "\"keep\"",
            "only the outermost pair should be stripped"
        );
    }

    #[test]
    fn preserves_mismatched_quotes() {
        assert_eq!(strip_one_outer_quote_pair("\"hi'"), "\"hi'");
        assert_eq!(strip_one_outer_quote_pair("'hi\""), "'hi\"");
    }

    #[test]
    fn preserves_unquoted_value() {
        assert_eq!(strip_one_outer_quote_pair("plain"), "plain");
    }

    #[test]
    fn handles_short_inputs() {
        assert_eq!(strip_one_outer_quote_pair(""), "");
        assert_eq!(strip_one_outer_quote_pair("\""), "\"");
        assert_eq!(strip_one_outer_quote_pair("\"\""), "");
    }
}