tokf 0.2.33

Config-driven CLI tool that compresses command output before it reaches an LLM context
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
use super::compound::*;

#[test]
fn single_segment_when_no_operator() {
    // Pipes are NOT chain operators — the whole string is one segment.
    let parts = split_compound("git diff HEAD | head -5");
    assert_eq!(
        parts,
        vec![("git diff HEAD | head -5".to_string(), String::new())]
    );
}

#[test]
fn splits_and_then_semicolon() {
    let parts = split_compound("git add foo && git diff; git status");
    assert_eq!(parts.len(), 3);
    assert_eq!(parts[0], ("git add foo".to_string(), " && ".to_string()));
    assert_eq!(parts[1], ("git diff".to_string(), "; ".to_string()));
    assert_eq!(parts[2], ("git status".to_string(), String::new()));
}

#[test]
fn splits_or_operator() {
    let parts = split_compound("make test || cargo test");
    assert_eq!(parts.len(), 2);
    assert_eq!(parts[0].0, "make test");
    assert_eq!(parts[1].0, "cargo test");
}

#[test]
fn splits_newline() {
    let parts = split_compound("git add .\ngit status");
    assert_eq!(parts.len(), 2);
    assert_eq!(parts[0].0, "git add .");
    assert_eq!(parts[1].0, "git status");
}

#[test]
fn has_bare_pipe_single_pipe() {
    assert!(has_bare_pipe("git diff HEAD | head -5"));
}

#[test]
fn has_bare_pipe_multi_pipe_chain() {
    assert!(has_bare_pipe("cmd | grep foo | wc -l"));
}

#[test]
fn has_bare_pipe_logical_or_only() {
    assert!(!has_bare_pipe("make test || cargo test"));
}

#[test]
fn has_bare_pipe_no_pipe() {
    assert!(!has_bare_pipe("cargo build --release"));
}

#[test]
fn has_bare_pipe_mixed_or_and_pipe() {
    assert!(has_bare_pipe("a || b | c"));
}

// --- quote-awareness ---

#[test]
fn has_bare_pipe_pipe_in_single_quotes_ignored() {
    assert!(!has_bare_pipe("grep -E 'foo|bar' file.txt"));
}

#[test]
fn has_bare_pipe_pipe_in_double_quotes_ignored() {
    assert!(!has_bare_pipe(r#"echo "a | b""#));
}

#[test]
fn has_bare_pipe_escaped_quote_does_not_end_double_quote() {
    // The \" inside the string does NOT close the double-quote context,
    // so the | remains inside quotes and is not a bare pipe.
    assert!(!has_bare_pipe(r#"echo "foo \" | bar""#));
}

#[test]
fn has_bare_pipe_pipe_after_closing_quote_is_bare() {
    // The pipe is outside the quotes — it IS a bare pipe.
    assert!(has_bare_pipe(r#"echo "hello" | grep o"#));
}

// --- edge cases ---

#[test]
fn has_bare_pipe_empty_string() {
    assert!(!has_bare_pipe(""));
}

#[test]
fn has_bare_pipe_only_pipe() {
    assert!(has_bare_pipe("|"));
}

#[test]
fn has_bare_pipe_bash_stderr_pipe() {
    // |& is Bash's pipe-stderr shorthand; the leading | is still a bare pipe.
    assert!(has_bare_pipe("cargo test |& tee log.txt"));
}

// --- strip_simple_pipe ---

fn stripped(base: &str, suffix: &str) -> StrippedPipe {
    StrippedPipe {
        base: base.to_string(),
        suffix: suffix.to_string(),
    }
}

#[test]
fn strip_tail_n() {
    assert_eq!(
        strip_simple_pipe("cargo test | tail -n 5"),
        Some(stripped("cargo test", "tail -n 5"))
    );
}

#[test]
fn strip_tail_numeric() {
    assert_eq!(
        strip_simple_pipe("cargo test | tail -5"),
        Some(stripped("cargo test", "tail -5"))
    );
}

#[test]
fn strip_tail_bare() {
    assert_eq!(
        strip_simple_pipe("cargo test | tail"),
        Some(stripped("cargo test", "tail"))
    );
}

#[test]
fn strip_head_n() {
    assert_eq!(
        strip_simple_pipe("cargo test | head -n 10"),
        Some(stripped("cargo test", "head -n 10"))
    );
}

#[test]
fn strip_head_bare() {
    assert_eq!(
        strip_simple_pipe("cargo test | head"),
        Some(stripped("cargo test", "head"))
    );
}

#[test]
fn strip_tail_lines_long() {
    assert_eq!(
        strip_simple_pipe("cargo test | tail --lines=5"),
        Some(stripped("cargo test", "tail --lines=5"))
    );
}

#[test]
fn strip_grep_pattern() {
    assert_eq!(
        strip_simple_pipe("cargo test | grep FAIL"),
        Some(stripped("cargo test", "grep FAIL"))
    );
}

#[test]
fn strip_grep_case_insensitive() {
    assert_eq!(
        strip_simple_pipe("cargo test | grep -i error"),
        Some(stripped("cargo test", "grep -i error"))
    );
}

#[test]
fn strip_grep_extended() {
    assert_eq!(
        strip_simple_pipe("cargo test | grep -E 'fail|error'"),
        Some(stripped("cargo test", "grep -E 'fail|error'"))
    );
}

#[test]
fn strip_grep_invert() {
    assert_eq!(
        strip_simple_pipe("cargo test | grep -v noise"),
        Some(stripped("cargo test", "grep -v noise"))
    );
}

#[test]
fn no_strip_tail_follow() {
    assert_eq!(strip_simple_pipe("cargo test | tail -f"), None);
}

#[test]
fn no_strip_tail_bytes() {
    assert_eq!(strip_simple_pipe("cargo test | tail -c 100"), None);
}

#[test]
fn no_strip_head_bytes() {
    assert_eq!(strip_simple_pipe("cargo test | head -c 50"), None);
}

#[test]
fn no_strip_grep_count() {
    assert_eq!(strip_simple_pipe("cargo test | grep -c FAIL"), None);
}

#[test]
fn no_strip_grep_files() {
    assert_eq!(strip_simple_pipe("cargo test | grep -l FAIL"), None);
}

#[test]
fn no_strip_wc() {
    assert_eq!(strip_simple_pipe("cargo test | wc -l"), None);
}

#[test]
fn no_strip_sort() {
    assert_eq!(strip_simple_pipe("cargo test | sort"), None);
}

#[test]
fn no_strip_multi_pipe() {
    assert_eq!(strip_simple_pipe("cmd | grep foo | tail -5"), None);
}

#[test]
fn strip_quoted_pipe_in_base() {
    // The pipe inside the quotes is not a bare pipe; the real pipe is to tail.
    assert_eq!(
        strip_simple_pipe("grep 'a|b' | tail -5"),
        Some(stripped("grep 'a|b'", "tail -5"))
    );
}

#[test]
fn no_strip_multi_pipe_with_tail() {
    assert_eq!(strip_simple_pipe("cargo test | tail -n 5 | grep x"), None);
}

#[test]
fn no_strip_grep_no_pattern() {
    // grep with only flags but no pattern argument is not strippable.
    assert_eq!(strip_simple_pipe("cargo test | grep -i"), None);
}

#[test]
fn strip_grep_combined_flags() {
    assert_eq!(
        strip_simple_pipe("cargo test | grep -iv error"),
        Some(stripped("cargo test", "grep -iv error"))
    );
}

#[test]
fn strip_head_lines_long_with_space() {
    // --lines with a space separator (not =)
    assert_eq!(
        strip_simple_pipe("cargo test | head --lines 10"),
        Some(stripped("cargo test", "head --lines 10"))
    );
}

#[test]
fn no_strip_empty_suffix() {
    // Trailing pipe with nothing after it.
    assert_eq!(strip_simple_pipe("cargo test |"), None);
}

#[test]
fn no_strip_grep_uppercase_l() {
    // -L is "files without match" — changes output format.
    assert_eq!(strip_simple_pipe("cargo test | grep -L FAIL"), None);
}

// --- strip_env_prefix ---

#[test]
fn env_prefix_single_var() {
    assert_eq!(
        strip_env_prefix("FOO=bar git status"),
        Some(("FOO=bar ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_multiple_vars() {
    assert_eq!(
        strip_env_prefix("A=1 B=2 cargo test"),
        Some(("A=1 B=2 ".to_string(), "cargo test".to_string()))
    );
}

#[test]
fn env_prefix_empty_value() {
    assert_eq!(
        strip_env_prefix("FOO= git status"),
        Some(("FOO= ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_single_quoted_value() {
    assert_eq!(
        strip_env_prefix("FOO='bar baz' git status"),
        Some(("FOO='bar baz' ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_double_quoted_value() {
    assert_eq!(
        strip_env_prefix(r#"FOO="bar baz" git status"#),
        Some((r#"FOO="bar baz" "#.to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_none_for_plain_command() {
    assert_eq!(strip_env_prefix("git status"), None);
}

#[test]
fn env_prefix_none_for_command_with_flags() {
    assert_eq!(strip_env_prefix("cargo test --lib"), None);
}

#[test]
fn env_prefix_underscore_key() {
    assert_eq!(
        strip_env_prefix("_MY_VAR=1 make"),
        Some(("_MY_VAR=1 ".to_string(), "make".to_string()))
    );
}

#[test]
fn env_prefix_real_world_rust() {
    assert_eq!(
        strip_env_prefix("RUST_LOG=debug CARGO_TERM_COLOR=always cargo test"),
        Some((
            "RUST_LOG=debug CARGO_TERM_COLOR=always ".to_string(),
            "cargo test".to_string()
        ))
    );
}

#[test]
fn env_prefix_equals_in_value() {
    // Values containing '=' are valid and common (e.g. key-value pairs passed as env).
    assert_eq!(
        strip_env_prefix("FOO=a=b git status"),
        Some(("FOO=a=b ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_long_path_value() {
    // PATH-style values with colons are entirely unquoted non-whitespace chars.
    assert_eq!(
        strip_env_prefix("PATH=/usr/local/bin:/usr/bin:/bin git status"),
        Some((
            "PATH=/usr/local/bin:/usr/bin:/bin ".to_string(),
            "git status".to_string()
        ))
    );
}

#[test]
fn env_prefix_backslash_escaped_space() {
    // FOO=bar\ baz is a single env var whose value contains an escaped space.
    assert_eq!(
        strip_env_prefix("FOO=bar\\ baz git status"),
        Some(("FOO=bar\\ baz ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_single_quote_idiom() {
    // The '\'' idiom embeds a literal single quote: 'hello'\''world' = hello'world.
    // After fixing the regex to handle backslash-escape fragments, this should
    // parse the whole assignment as one token.
    assert_eq!(
        strip_env_prefix("FOO='hello'\\''world' cargo test"),
        Some((
            "FOO='hello'\\''world' ".to_string(),
            "cargo test".to_string()
        ))
    );
}

#[test]
fn env_prefix_backslash_in_double_quoted_value() {
    // Escaped backslash inside double-quoted value.
    assert_eq!(
        strip_env_prefix(r#"FOO="bar\"baz" git status"#),
        Some((r#"FOO="bar\"baz" "#.to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_dollar_var_in_value() {
    // Shell variable expansions ($HOME, ${HOME}) are just non-whitespace chars.
    assert_eq!(
        strip_env_prefix("PREFIX=$HOME/bin git status"),
        Some(("PREFIX=$HOME/bin ".to_string(), "git status".to_string()))
    );
}

#[test]
fn env_prefix_numeric_value() {
    assert_eq!(
        strip_env_prefix("DEBUG=123456 cargo test"),
        Some(("DEBUG=123456 ".to_string(), "cargo test".to_string()))
    );
}

#[test]
fn env_prefix_numeric_key_not_matched() {
    // POSIX: variable names must start with a letter or underscore.
    assert_eq!(strip_env_prefix("1FOO=bar git status"), None);
}