sel-rs 0.2.2

Select slices from text files by line numbers, ranges, positions, or regex
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
//! Integration tests for multi-file handling.

use std::io::Write;
use std::process::Command;
use tempfile::NamedTempFile;

/// Helper to run sel with arguments and return stdout.
fn run_sel(args: &[&str]) -> String {
    let output = Command::new("cargo")
        .args(["run", "--quiet", "--"])
        .args(args)
        .output()
        .expect("Failed to execute sel");

    String::from_utf8_lossy(&output.stdout).to_string()
}

/// Helper to run sel with arguments and return both stdout and stderr.
fn run_sel_with_result(args: &[&str]) -> (String, String, i32) {
    let output = Command::new("cargo")
        .args(["run", "--quiet", "--"])
        .args(args)
        .output()
        .expect("Failed to execute sel");

    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    let code = output.status.code().unwrap_or(-1);

    (stdout, stderr, code)
}

/// Helper to create a test file with lines.
fn create_test_file(lines: &[&str]) -> NamedTempFile {
    let mut file = NamedTempFile::new().unwrap();
    for line in lines {
        writeln!(file, "{}", line).unwrap();
    }
    file.flush().unwrap();
    file
}

/// Helper to get the filename of a temp file.
fn temp_file_name(file: &NamedTempFile) -> String {
    file.path()
        .file_name()
        .unwrap()
        .to_string_lossy()
        .to_string()
}

#[test]
fn test_two_files_simple_selector() {
    let file1 = create_test_file(&["line1 from file1", "line2 from file1"]);
    let file2 = create_test_file(&["line1 from file2", "line2 from file2"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Both files should show line 1
    assert!(output.contains("line1 from file1"));
    assert!(output.contains("line1 from file2"));
}

#[test]
fn test_three_files_range() {
    let file1 = create_test_file(&["a1", "a2", "a3"]);
    let file2 = create_test_file(&["b1", "b2", "b3"]);
    let file3 = create_test_file(&["c1", "c2", "c3"]);

    let output = run_sel(&[
        "2-3",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
        file3.path().to_str().unwrap(),
    ]);

    assert!(output.contains("a2"));
    assert!(output.contains("b2"));
    assert!(output.contains("c2"));
    assert!(!output.contains("a1"));
    assert!(!output.contains("b1"));
}

#[test]
fn test_filename_prefix_multi_file() {
    let file1 = create_test_file(&["content1"]);
    let file2 = create_test_file(&["content2"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // With multiple files, filename should be shown
    let name1 = temp_file_name(&file1);
    let name2 = temp_file_name(&file2);

    assert!(output.contains(&name1) || output.contains(file1.path().to_str().unwrap()));
    assert!(output.contains(&name2) || output.contains(file2.path().to_str().unwrap()));
}

#[test]
fn test_no_filename_prefix_single_file() {
    let file1 = create_test_file(&["content1"]);
    let output = run_sel(&["1", file1.path().to_str().unwrap()]);

    // Single file should not show filename by default
    let name = temp_file_name(&file1);
    // Output should include the line prefix, not a filename prefix.
    assert!(output.contains("1: content1"));
    // Unless filename is part of content
    if !output.contains(&name) {
        // Good - no filename prefix
    }
}

#[test]
fn test_force_filename_with_h_flag() {
    let file1 = create_test_file(&["content"]);
    let name = temp_file_name(&file1);

    let output = run_sel(&["-H", "1", file1.path().to_str().unwrap()]);

    // With -H, filename should be shown even for single file
    assert!(output.contains(&name) || output.contains(file1.path().to_str().unwrap()));
}

#[test]
fn test_multi_file_with_regex() {
    let file1 = create_test_file(&["ERROR: file1 error", "INFO: file1 info"]);
    let file2 = create_test_file(&["ERROR: file2 error", "DEBUG: file2 debug"]);
    let file3 = create_test_file(&["INFO: file3 info"]);

    let output = run_sel(&[
        "-e",
        "ERROR",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
        file3.path().to_str().unwrap(),
    ]);

    assert!(output.contains("ERROR: file1 error"));
    assert!(output.contains("ERROR: file2 error"));
    assert!(!output.contains("INFO:"));
    assert!(!output.contains("DEBUG:"));
}

#[test]
fn test_multi_file_regex_with_filename() {
    let file1 = create_test_file(&["TARGET"]);
    let file2 = create_test_file(&["TARGET"]);

    let output = run_sel(&[
        "-e",
        "TARGET",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    let _name1 = temp_file_name(&file1);
    let _name2 = temp_file_name(&file2);

    // Filenames should appear with regex on multiple files
    assert!(output.contains("TARGET"));
}

#[test]
fn test_multi_file_complex_selector() {
    let file1 = create_test_file(&["l1", "l2", "l3", "l4", "l5"]);
    let file2 = create_test_file(&["l1", "l2", "l3", "l4", "l5"]);

    let output = run_sel(&[
        "1,3,5",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Both files should output lines 1, 3, 5
    let lines: Vec<&str> = output.lines().collect();
    let matching_lines: Vec<_> = lines
        .iter()
        .filter(|l| l.contains("l1") || l.contains("l3") || l.contains("l5"))
        .collect();

    assert!(matching_lines.len() >= 6); // 3 lines per file * 2 files
}

#[test]
fn test_multi_file_all_lines() {
    let file1 = create_test_file(&["a", "b"]);
    let file2 = create_test_file(&["c", "d"]);

    let output = run_sel(&[
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Without selector, all lines from all files
    assert!(output.contains("a"));
    assert!(output.contains("b"));
    assert!(output.contains("c"));
    assert!(output.contains("d"));
}

#[test]
fn test_multi_file_empty_first_file() {
    let file1 = create_test_file(&[]);
    let file2 = create_test_file(&["content"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Should still process second file
    assert!(output.contains("content"));
}

#[test]
fn test_multi_file_empty_second_file() {
    let file1 = create_test_file(&["content"]);
    let file2 = create_test_file(&[]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Should still process first file
    assert!(output.contains("content"));
}

#[test]
fn test_multi_file_different_sizes() {
    let file1 = create_test_file(&["only one line"]);
    let file2 = create_test_file(&["line1", "line2", "line3", "line4", "line5"]);

    let output = run_sel(&[
        "3",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // File1 only has 1 line, file2 has line 3
    assert!(output.contains("line3"));
}

#[test]
fn test_multi_file_with_range_extending_beyond() {
    let file1 = create_test_file(&["a", "b"]);
    let file2 = create_test_file(&["x", "y", "z"]);

    let output = run_sel(&[
        "2-5",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Should handle gracefully - file1 only has 2 lines
    assert!(output.contains("b"));
    assert!(output.contains("y"));
    assert!(output.contains("z"));
}

#[test]
fn test_multi_file_with_context() {
    let file1 = create_test_file(&["before", "TARGET1", "after"]);
    let file2 = create_test_file(&["before", "TARGET2", "after"]);

    let output = run_sel(&[
        "-c",
        "1",
        "2",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    assert!(output.contains("TARGET1") || output.contains("TARGET2"));
}

#[test]
fn test_multi_file_with_no_line_numbers() {
    let file1 = create_test_file(&["content1"]);
    let file2 = create_test_file(&["content2"]);

    let output = run_sel(&[
        "-l",
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // No line numbers, but filename should still appear for multiple files
    assert!(output.contains("content1"));
    assert!(output.contains("content2"));
}

#[test]
fn test_multi_file_positional_selector() {
    let file1 = create_test_file(&["position test in file1"]);
    let file2 = create_test_file(&["position test in file2"]);

    let output = run_sel(&[
        "1:1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    assert!(output.contains("position test in file1"));
    assert!(output.contains("position test in file2"));
}

#[test]
fn test_multi_file_ordering_preserved() {
    let file1 = create_test_file(&["from_file1"]);
    let file2 = create_test_file(&["from_file2"]);
    let file3 = create_test_file(&["from_file3"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
        file3.path().to_str().unwrap(),
    ]);

    // Files should be processed in order given
    let file1_pos = output.find("from_file1");
    let file2_pos = output.find("from_file2");
    let file3_pos = output.find("from_file3");

    if let (Some(f1), Some(f2), Some(f3)) = (file1_pos, file2_pos, file3_pos) {
        assert!(f1 < f2);
        assert!(f2 < f3);
    }
}

#[test]
fn test_multi_file_duplicate_content() {
    let file1 = create_test_file(&["IDENTICAL LINE"]);
    let file2 = create_test_file(&["IDENTICAL LINE"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    // Both occurrences should appear
    let count = output.matches("IDENTICAL LINE").count();
    assert_eq!(count, 2);
}

#[test]
fn test_multi_file_with_special_filenames() {
    let file1 = create_test_file(&["content with spaces"]);
    let file2 = create_test_file(&["content with tabs\t"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    assert!(output.contains("content with spaces"));
}

#[test]
fn test_many_files() {
    let files: Vec<_> = (0..10)
        .map(|i| {
            let content = format!("content{}", i);
            create_test_file(&[content.as_str()])
        })
        .collect();
    let args: Vec<_> = ["1"]
        .iter()
        .copied()
        .chain(files.iter().map(|f| f.path().to_str().unwrap()))
        .collect();

    let output = run_sel(&args);

    // All files should be processed
    for i in 0..10 {
        assert!(output.contains(&format!("content{}", i)));
    }
}

#[test]
fn test_multi_file_nonexistent_file() {
    let file1 = create_test_file(&["exists"]);
    let (_stdout, stderr, code) =
        run_sel_with_result(&["1", file1.path().to_str().unwrap(), "/nonexistent/file.txt"]);

    // Should error on nonexistent file
    assert!(code != 0 || stderr.contains("Error") || stderr.contains("No such file"));
}

#[test]
fn test_multi_file_mixed_valid_invalid() {
    let file1 = create_test_file(&["file1 content"]);
    let file2 = create_test_file(&["file2 content"]);
    let (_stdout, stderr, code) = run_sel_with_result(&[
        "1",
        file1.path().to_str().unwrap(),
        "/nonexistent.txt",
        file2.path().to_str().unwrap(),
    ]);

    // Behavior on mixed files - should stop at error
    if code != 0 {
        assert!(stderr.contains("Error"));
    }
}

#[test]
fn test_multi_file_unicode_content() {
    let file1 = create_test_file(&["Hello 世界"]);
    let file2 = create_test_file(&["Привет мир"]);

    let output = run_sel(&[
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    assert!(output.contains("世界"));
    assert!(output.contains("Привет"));
}

#[test]
fn test_multi_file_with_color_flag() {
    let file1 = create_test_file(&["content1"]);
    let file2 = create_test_file(&["content2"]);

    // Test with color=never to avoid ANSI codes in output
    let output = run_sel(&[
        "--color=never",
        "1",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
    ]);

    assert!(output.contains("content1"));
    assert!(output.contains("content2"));
}

#[test]
fn test_multi_file_regex_all_files() {
    let file1 = create_test_file(&["match this", "not this"]);
    let file2 = create_test_file(&["match that", "also not"]);
    let file3 = create_test_file(&["match other", "nope"]);

    let output = run_sel(&[
        "-e",
        "match",
        file1.path().to_str().unwrap(),
        file2.path().to_str().unwrap(),
        file3.path().to_str().unwrap(),
    ]);

    // All files should have matches
    assert!(output.contains("match this"));
    assert!(output.contains("match that"));
    assert!(output.contains("match other"));
    assert!(!output.contains("not this"));
    assert!(!output.contains("also not"));
    assert!(!output.contains("nope"));
}