linesmith 0.1.1

A Rust status line for Claude Code and other AI coding CLIs
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
use std::io::Cursor;
use std::process::Command;
use std::str::FromStr;

const CLAUDE_MINIMAL: &str = include_str!("fixtures/claude_minimal.json");
const CLAUDE_WORKTREE: &str = include_str!("fixtures/claude_worktree.json");

/// Run `git <args>` inside `cwd` with an isolated config (global /
/// system configs bypassed, hooks disabled, signing off, default
/// branch pinned). Panics with the child's stderr + exit code on
/// failure so CI logs carry enough context to diagnose without
/// re-running locally.
fn run_git(cwd: &std::path::Path, args: &[&str]) {
    let out = Command::new("git")
        .env("GIT_CONFIG_GLOBAL", "/dev/null")
        .env("GIT_CONFIG_SYSTEM", "/dev/null")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .args(["-c", "commit.gpgsign=false"])
        .args(["-c", "core.hooksPath=/dev/null"])
        .args(["-c", "init.defaultBranch=main"])
        .args(["-c", "user.email=t@t", "-c", "user.name=t"])
        .arg("-C")
        .arg(cwd)
        .args(args)
        .output()
        .expect("spawn git");
    assert!(
        out.status.success(),
        "git {args:?} in {cwd:?} exited {:?}\nstderr: {}",
        out.status.code(),
        String::from_utf8_lossy(&out.stderr)
    );
}

#[test]
fn renders_model_and_workspace_when_outside_worktree() {
    let mut out = Vec::new();
    linesmith::run(Cursor::new(CLAUDE_MINIMAL), &mut out).expect("run ok");
    assert_eq!(
        String::from_utf8(out).expect("utf8"),
        "Claude Sonnet 4.6 linesmith\n"
    );
}

#[test]
fn renders_full_payload_with_cost_effort_and_workspace() {
    // Rate-limit segments are opt-in, so a first-run user doesn't
    // trigger a Keychain prompt from the default line.
    let mut out = Vec::new();
    linesmith::run(Cursor::new(CLAUDE_WORKTREE), &mut out).expect("run ok");
    let rendered = String::from_utf8(out).expect("utf8");

    for substring in [
        "Claude Sonnet 4.6",
        "42% · 200k",
        "$1.23",
        "high",
        "linesmith",
    ] {
        assert!(
            rendered.contains(substring),
            "expected {substring:?} in {rendered:?}"
        );
    }
    // Guard: workspace sources worktree-name from gix discovery now,
    // not stdin. `run()` leaves cwd unset, so the hybrid form must
    // NOT appear here — even though the fixture carries a
    // `git_worktree` field.
    assert!(
        !rendered.contains("linesmith/"),
        "workspace must not emit hybrid form without a real linked-worktree cwd: {rendered:?}"
    );
    for absent in ["5h", "7d", "rate_limit"] {
        assert!(
            !rendered.contains(absent),
            "{absent:?} should not appear without explicit opt-in ({rendered:?})",
        );
    }
    assert!(rendered.ends_with('\n'));
}

#[test]
fn malformed_json_exits_zero_with_marker_line() {
    let mut out = Vec::new();
    linesmith::run(Cursor::new(b"{not json"), &mut out).expect("run should not error");
    assert_eq!(String::from_utf8(out).expect("utf8"), "?\n");
}

#[test]
fn narrow_terminal_drops_cost_and_effort_first() {
    // Budget chosen so the two highest drop-priorities (cost, effort)
    // drop before context_window or workspace get touched.
    let mut out = Vec::new();
    linesmith::run_with_width(Cursor::new(CLAUDE_WORKTREE), &mut out, 40).expect("run ok");
    let rendered = String::from_utf8(out).expect("utf8");
    assert!(!rendered.contains("$1.23"), "cost should drop first");
    assert!(!rendered.contains("high"), "effort should drop second");
    assert!(rendered.contains("42% · 200k"));
    assert!(rendered.contains("linesmith"));
    assert!(
        !rendered.contains("linesmith/"),
        "no worktree cwd here: {rendered:?}"
    );
}

#[test]
fn extreme_narrow_keeps_only_lowest_priority_segments() {
    // Budget tight enough that only workspace (lowest drop-priority)
    // survives, even though context_window would fit alone.
    let mut out = Vec::new();
    linesmith::run_with_width(Cursor::new(CLAUDE_WORKTREE), &mut out, 10).expect("run ok");
    assert_eq!(String::from_utf8(out).expect("utf8"), "linesmith\n");
}

#[test]
fn xdg_plugin_renders_via_full_driver_path() {
    // Pins the `cli_main → load_plugins → build_segments → RhaiSegment::render`
    // chain end-to-end with a real .rhai file under XDG.
    use std::fs;
    use tempfile::TempDir;

    let xdg = TempDir::new().expect("tempdir");
    let segments_dir = xdg.path().join("linesmith").join("segments");
    fs::create_dir_all(&segments_dir).expect("mkdir");

    fs::write(
        segments_dir.join("echo.rhai"),
        r#"
        const ID = "echo";
        fn render(ctx) {
            #{ runs: [#{ text: ctx.config.text }] }
        }
        "#,
    )
    .expect("write plugin");

    let config_dir = xdg.path().join("linesmith");
    fs::write(
        config_dir.join("config.toml"),
        r#"
            [line]
            segments = ["echo"]
            [segments.echo]
            text = "hi-from-plugin"
        "#,
    )
    .expect("write config");

    let mut env = linesmith::CliEnv::for_tests();
    env.xdg_config_home = Some(xdg.path().to_string_lossy().into_owned());

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = linesmith::cli_main(
        std::iter::empty::<&str>(),
        Cursor::new(CLAUDE_MINIMAL),
        &mut stdout,
        &mut stderr,
        &env,
    );
    assert_eq!(code, 0, "stderr: {}", String::from_utf8_lossy(&stderr));
    assert_eq!(String::from_utf8(stdout).expect("utf8"), "hi-from-plugin\n");
}

#[test]
fn git_branch_renders_unborn_head_via_full_driver_path() {
    // End-to-end through cli_main: a freshly init'd repo fixture has
    // no commits, so HEAD is unborn and the segment renders the
    // symbolic-ref target (whatever init.defaultBranch resolves to).
    use tempfile::TempDir;

    let repo_dir = TempDir::new().expect("tempdir");
    gix::init(repo_dir.path()).expect("gix::init");

    let mut env = linesmith::CliEnv::for_tests();
    env.cwd = Some(repo_dir.path().to_path_buf());

    // Scope the segment list to just `git_branch` so the assertion
    // doesn't couple to the rest of the default line.
    let xdg = TempDir::new().expect("tempdir");
    let config_dir = xdg.path().join("linesmith");
    std::fs::create_dir_all(&config_dir).expect("mkdir");
    std::fs::write(
        config_dir.join("config.toml"),
        r#"
            [line]
            segments = ["git_branch"]
        "#,
    )
    .expect("write config");
    env.xdg_config_home = Some(xdg.path().to_string_lossy().into_owned());

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = linesmith::cli_main(
        std::iter::empty::<&str>(),
        Cursor::new(CLAUDE_MINIMAL),
        &mut stdout,
        &mut stderr,
        &env,
    );
    assert_eq!(code, 0, "stderr: {}", String::from_utf8_lossy(&stderr));
    let rendered = String::from_utf8(stdout).expect("utf8");
    // gix's default branch is `main` unless init.defaultBranch is
    // configured; accept either.
    assert!(
        rendered.starts_with("main") || rendered.starts_with("master"),
        "expected branch name at start, got {rendered:?}"
    );
}

/// Set up a primary checkout + one linked worktree via `git worktree
/// add`, returning `(primary_tempdir, worktree_parent_tempdir,
/// worktree_path)`. Tempdirs are held by the caller so the worktree
/// stays live for the duration of the test.
fn linked_worktree_fixture(
    name: &str,
) -> (tempfile::TempDir, tempfile::TempDir, std::path::PathBuf) {
    use tempfile::TempDir;

    let primary = TempDir::new().expect("primary tempdir");
    let wt_parent = TempDir::new().expect("worktree tempdir");
    run_git(primary.path(), &["init", "--quiet"]);
    run_git(
        primary.path(),
        &["commit", "--allow-empty", "-m", "seed", "--quiet"],
    );
    let worktree_dir = wt_parent.path().join(name);
    run_git(
        primary.path(),
        &[
            "worktree",
            "add",
            "--quiet",
            "-b",
            name,
            worktree_dir.to_str().expect("utf8 path"),
        ],
    );
    (primary, wt_parent, worktree_dir)
}

fn cli_env_with_config(
    worktree_dir: std::path::PathBuf,
    config_toml: &str,
) -> (linesmith::CliEnv, tempfile::TempDir) {
    use tempfile::TempDir;

    let xdg = TempDir::new().expect("xdg tempdir");
    let config_dir = xdg.path().join("linesmith");
    std::fs::create_dir_all(&config_dir).expect("mkdir");
    std::fs::write(config_dir.join("config.toml"), config_toml).expect("write config");

    let mut env = linesmith::CliEnv::for_tests();
    env.cwd = Some(worktree_dir);
    env.xdg_config_home = Some(xdg.path().to_string_lossy().into_owned());
    (env, xdg)
}

#[test]
fn renders_worktree_hybrid_with_real_linked_worktree() {
    let (_primary, _wt_parent, worktree_dir) = linked_worktree_fixture("feat-segments");
    let (env, _xdg) = cli_env_with_config(
        worktree_dir,
        r#"
            [line]
            segments = ["workspace"]
        "#,
    );

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = linesmith::cli_main(
        std::iter::empty::<&str>(),
        Cursor::new(CLAUDE_MINIMAL),
        &mut stdout,
        &mut stderr,
        &env,
    );
    assert_eq!(code, 0, "stderr: {}", String::from_utf8_lossy(&stderr));
    assert_eq!(
        String::from_utf8(stdout).expect("utf8"),
        "linesmith/feat-segments\n"
    );
}

#[test]
fn workspace_and_git_branch_coexist_on_linked_worktree() {
    // Spec §Coordination: both segments render distinct payloads on a
    // linked worktree. `workspace` shows the worktree name, `git_branch`
    // shows the branch. No suppression.
    let (_primary, _wt_parent, worktree_dir) = linked_worktree_fixture("feat-segments");
    let (env, _xdg) = cli_env_with_config(
        worktree_dir,
        r#"
            [line]
            segments = ["workspace", "git_branch"]
        "#,
    );

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = linesmith::cli_main(
        std::iter::empty::<&str>(),
        Cursor::new(CLAUDE_MINIMAL),
        &mut stdout,
        &mut stderr,
        &env,
    );
    assert_eq!(code, 0, "stderr: {}", String::from_utf8_lossy(&stderr));
    let rendered = String::from_utf8(stdout).expect("utf8");
    assert!(
        rendered.contains("linesmith/feat-segments"),
        "workspace hybrid missing: {rendered:?}"
    );
    assert!(
        rendered.contains("feat-segments") && rendered.matches("feat-segments").count() >= 2,
        "git_branch should render the branch name alongside workspace: {rendered:?}"
    );
}

#[test]
fn git_branch_hides_outside_repo() {
    use tempfile::TempDir;

    let cwd = TempDir::new().expect("tempdir");

    let mut env = linesmith::CliEnv::for_tests();
    env.cwd = Some(cwd.path().to_path_buf());

    let xdg = TempDir::new().expect("tempdir");
    let config_dir = xdg.path().join("linesmith");
    std::fs::create_dir_all(&config_dir).expect("mkdir");
    std::fs::write(
        config_dir.join("config.toml"),
        r#"
            [line]
            segments = ["git_branch", "model"]
        "#,
    )
    .expect("write config");
    env.xdg_config_home = Some(xdg.path().to_string_lossy().into_owned());

    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let code = linesmith::cli_main(
        std::iter::empty::<&str>(),
        Cursor::new(CLAUDE_MINIMAL),
        &mut stdout,
        &mut stderr,
        &env,
    );
    assert_eq!(code, 0);
    let rendered = String::from_utf8(stdout).expect("utf8");
    // Only the model segment rendered; git_branch hid silently.
    assert_eq!(rendered, "Claude Sonnet 4.6\n");
}

#[test]
fn config_reorders_and_filters_segments() {
    // Config picks only model + workspace, in that custom order.
    let cfg = linesmith::config::Config::from_str(
        r#"
            [line]
            segments = ["workspace", "model"]
        "#,
    )
    .expect("parse");
    let segments = linesmith::build_segments(Some(&cfg), None, |_| {});
    let mut out = Vec::new();
    linesmith::run_with_segments_and_width(Cursor::new(CLAUDE_WORKTREE), &mut out, &segments, 200)
        .expect("run ok");
    let rendered = String::from_utf8(out).expect("utf8");
    assert_eq!(rendered, "linesmith Claude Sonnet 4.6\n");
}

#[test]
fn config_style_override_emits_sgr_bytes_end_to_end() {
    // TOML → SegmentOverride → parse_style → with_user_style → render_with_warn
    // pipeline: the model segment's rendered text should be wrapped in a
    // TrueColor-red + bold SGR prefix followed by a reset.
    let cfg = linesmith::config::Config::from_str(
        r#"
            [line]
            segments = ["model"]
            [segments.model]
            style = "fg:rgb(255, 0, 0) bold"
        "#,
    )
    .expect("parse");
    let segments = linesmith::build_segments(Some(&cfg), None, |_| {});
    let status_ctx =
        linesmith::input::parse(include_bytes!("fixtures/claude_minimal.json")).expect("parse");
    let ctx = linesmith::data_context::DataContext::new(status_ctx);
    let line = linesmith::layout::render_with_warn(
        &segments,
        &ctx,
        200,
        &mut |_| {},
        linesmith::theme::default_theme(),
        linesmith::theme::Capability::TrueColor,
    );
    assert!(
        line.contains("\x1b[1;38;2;255;0;0m"),
        "expected bold + truecolor-red SGR prefix, got {line:?}"
    );
    assert!(line.contains("Claude Sonnet 4.6"));
    assert!(line.contains("\x1b[0m"), "expected SGR reset");
}

#[test]
fn config_style_override_invalid_warns_and_render_still_succeeds() {
    let cfg = linesmith::config::Config::from_str(
        r#"
            [line]
            segments = ["model"]
            [segments.model]
            style = "role:mauve"
        "#,
    )
    .expect("parse");
    let mut warnings = Vec::new();
    let segments = linesmith::build_segments(Some(&cfg), None, |m| warnings.push(m.to_string()));
    assert_eq!(warnings.len(), 1);
    assert!(warnings[0].contains("segments.model.style"));
    assert!(warnings[0].contains("mauve"));
    let mut out = Vec::new();
    linesmith::run_with_segments_and_width(Cursor::new(CLAUDE_MINIMAL), &mut out, &segments, 200)
        .expect("run ok");
    // Render still succeeds; the bad override is skipped.
    assert!(String::from_utf8(out)
        .expect("utf8")
        .contains("Claude Sonnet 4.6"));
}

#[test]
fn config_priority_override_flips_drop_order_under_pressure() {
    // With default priorities, a narrow terminal drops cost (192)
    // before model (64). Override model's priority to 250 and it drops
    // first instead.
    let cfg = linesmith::config::Config::from_str(
        r#"
            [line]
            segments = ["model", "cost"]
            [segments.model]
            priority = 250
        "#,
    )
    .expect("parse");
    let segments = linesmith::build_segments(Some(&cfg), None, |_| {});
    let mut out = Vec::new();
    // Budget tight enough to force one drop but fit the other.
    linesmith::run_with_segments_and_width(Cursor::new(CLAUDE_WORKTREE), &mut out, &segments, 10)
        .expect("run ok");
    let rendered = String::from_utf8(out).expect("utf8");
    // Model dropped; cost survived.
    assert!(!rendered.contains("Claude"));
    assert!(rendered.contains("$1.23"));
}