bashkit 0.5.0

Awesomely fast virtual sandbox with bash and file system
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
//! Head and tail builtins - output first/last lines of input

use async_trait::async_trait;

use super::{Builtin, Context, read_text_file};
use crate::error::Result;
use crate::interpreter::ExecResult;

/// Default number of lines to output
const DEFAULT_LINES: usize = 10;

/// The head builtin - output the first N lines or bytes of input.
///
/// Usage: head [-n NUM | -c NUM] [FILE...]
///
/// Options:
///   -n NUM   Output the first NUM lines (default: 10)
///   -c NUM   Output the first NUM bytes
///   -NUM     Shorthand for -n NUM
pub struct Head;

#[async_trait]
impl Builtin for Head {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: head [OPTION]... [FILE]...\nPrint the first 10 lines of each FILE to standard output.\n\n  -n NUM\toutput the first NUM lines\n  -c NUM\toutput the first NUM bytes\n  -NUM\t\tshorthand for -n NUM\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("head (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        let (count, byte_mode, files) = parse_head_args(ctx.args, DEFAULT_LINES)?;

        let mut output = String::new();

        if files.is_empty() {
            // Read from stdin
            if let Some(stdin) = ctx.stdin {
                if byte_mode {
                    output = take_first_bytes(stdin, count);
                } else {
                    output = take_first_lines(stdin, count);
                }
            }
        } else {
            // Read from files
            let multiple_files = files.len() > 1;
            for (i, file) in files.iter().enumerate() {
                if multiple_files {
                    if i > 0 {
                        output.push('\n');
                    }
                    output.push_str(&format!("==> {} <==\n", file));
                }

                let path = if file.starts_with('/') {
                    std::path::PathBuf::from(file)
                } else {
                    ctx.cwd.join(file)
                };

                match ctx.fs.read_file(&path).await {
                    Ok(content) => {
                        if byte_mode {
                            // Byte mode: take first N bytes, preserve raw byte values
                            let bytes = &content[..content.len().min(count)];
                            let s: String = bytes.iter().map(|&b| b as char).collect();
                            output.push_str(&s);
                        } else {
                            let text: String = content.iter().map(|&b| b as char).collect();
                            output.push_str(&take_first_lines(&text, count));
                        }
                    }
                    Err(e) => {
                        return Ok(ExecResult::err(format!("head: {}: {}\n", file, e), 1));
                    }
                }
            }
        }

        Ok(ExecResult::ok(output))
    }
}

/// The tail builtin - output the last N lines of input.
///
/// Usage: tail [-n NUM] [FILE...]
///
/// Options:
///   -n NUM   Output the last NUM lines (default: 10)
///   -n +NUM  Output starting from line NUM (1-indexed)
///   -NUM     Shorthand for -n NUM
pub struct Tail;

#[async_trait]
impl Builtin for Tail {
    async fn execute(&self, ctx: Context<'_>) -> Result<ExecResult> {
        if let Some(r) = super::check_help_version(
            ctx.args,
            "Usage: tail [OPTION]... [FILE]...\nPrint the last 10 lines of each FILE to standard output.\n\n  -n NUM\toutput the last NUM lines\n  -n +NUM\toutput starting with line NUM\n  -NUM\t\tshorthand for -n NUM\n  --help\tdisplay this help and exit\n  --version\toutput version information and exit\n",
            Some("tail (bashkit) 0.1"),
        ) {
            return Ok(r);
        }
        let (num_lines, from_start, files) = parse_tail_args(ctx.args, DEFAULT_LINES)?;

        let mut output = String::new();

        if files.is_empty() {
            // Read from stdin
            if let Some(stdin) = ctx.stdin {
                output = if from_start {
                    take_from_line(stdin, num_lines)
                } else {
                    take_last_lines(stdin, num_lines)
                };
            }
        } else {
            // Read from files
            let multiple_files = files.len() > 1;
            for (i, file) in files.iter().enumerate() {
                if multiple_files {
                    if i > 0 {
                        output.push('\n');
                    }
                    output.push_str(&format!("==> {} <==\n", file));
                }

                let path = if file.starts_with('/') {
                    std::path::PathBuf::from(file)
                } else {
                    ctx.cwd.join(file)
                };

                let text = match read_text_file(&*ctx.fs, &path, "tail").await {
                    Ok(t) => t,
                    Err(e) => return Ok(e),
                };
                let selected = if from_start {
                    take_from_line(&text, num_lines)
                } else {
                    take_last_lines(&text, num_lines)
                };
                output.push_str(&selected);
            }
        }

        Ok(ExecResult::ok(output))
    }
}

/// Parse arguments for head command, including -c (byte count) mode.
/// Returns (count, byte_mode, file_list)
fn parse_head_args(args: &[String], default: usize) -> Result<(usize, bool, Vec<String>)> {
    let mut count = default;
    let mut byte_mode = false;
    let mut files = Vec::new();
    let mut p = super::arg_parser::ArgParser::new(args);

    while !p.is_done() {
        if let Some(val) = p.flag_value_opt("-n") {
            count = val.parse().unwrap_or(default);
            byte_mode = false;
        } else if let Some(val) = p.flag_value_opt("-c") {
            count = val.parse().unwrap_or(default);
            byte_mode = true;
        } else if let Some(arg) = p.current().filter(|a| a.starts_with('-')) {
            if let Some(num_str) = arg.strip_prefix('-')
                && let Ok(n) = num_str.parse::<usize>()
            {
                count = n;
            }
            p.advance();
        } else if let Some(arg) = p.positional() {
            files.push(arg.to_string());
        }
    }

    Ok((count, byte_mode, files))
}

/// Take the first N bytes from text.
/// Uses char-level truncation so that Latin-1 encoded binary data
/// (e.g. from /dev/urandom where each byte maps to one char) is
/// counted correctly — each char represents one original byte.
fn take_first_bytes(text: &str, n: usize) -> String {
    text.chars().take(n).collect()
}

/// Parse arguments for tail command, including +N "from start" syntax.
/// Returns (num_lines, from_start, file_list)
fn parse_tail_args(args: &[String], default: usize) -> Result<(usize, bool, Vec<String>)> {
    let mut num_lines = default;
    let mut from_start = false;
    let mut files = Vec::new();
    let mut p = super::arg_parser::ArgParser::new(args);

    while !p.is_done() {
        if let Some(val) = p.flag_value_opt("-n") {
            if let Some(pos_str) = val.strip_prefix('+') {
                from_start = true;
                num_lines = pos_str.parse().unwrap_or(default);
            } else {
                from_start = false;
                num_lines = val.parse().unwrap_or(default);
            }
        } else if let Some(arg) = p.current().filter(|a| a.starts_with('-')) {
            if let Some(num_str) = arg.strip_prefix('-')
                && let Ok(n) = num_str.parse::<usize>()
            {
                num_lines = n;
            }
            p.advance();
        } else if let Some(arg) = p.positional() {
            files.push(arg.to_string());
        }
    }

    Ok((num_lines, from_start, files))
}

/// Take the first N lines from text
fn take_first_lines(text: &str, n: usize) -> String {
    let lines: Vec<&str> = text.lines().take(n).collect();
    if lines.is_empty() {
        String::new()
    } else {
        let mut result = lines.join("\n");
        // Preserve trailing newline if original had one
        if text.ends_with('\n') || !text.is_empty() {
            result.push('\n');
        }
        result
    }
}

/// Take lines starting from line N (1-indexed, like `tail -n +N`)
fn take_from_line(text: &str, n: usize) -> String {
    let lines: Vec<&str> = text.lines().collect();
    let start = if n == 0 { 0 } else { n - 1 };
    let selected: Vec<&str> = lines.into_iter().skip(start).collect();

    if selected.is_empty() {
        String::new()
    } else {
        let mut result = selected.join("\n");
        if text.ends_with('\n') || !text.is_empty() {
            result.push('\n');
        }
        result
    }
}

/// Take the last N lines from text
fn take_last_lines(text: &str, n: usize) -> String {
    let lines: Vec<&str> = text.lines().collect();
    let start = lines.len().saturating_sub(n);
    let selected: Vec<&str> = lines[start..].to_vec();

    if selected.is_empty() {
        String::new()
    } else {
        let mut result = selected.join("\n");
        // Preserve trailing newline if original had one
        if text.ends_with('\n') || !text.is_empty() {
            result.push('\n');
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use std::path::PathBuf;
    use std::sync::Arc;

    use crate::fs::InMemoryFs;

    async fn run_head(args: &[&str], stdin: Option<&str>) -> ExecResult {
        let fs = Arc::new(InMemoryFs::new());
        let mut variables = HashMap::new();
        let env = HashMap::new();
        let mut cwd = PathBuf::from("/");

        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context {
            args: &args,
            env: &env,
            variables: &mut variables,
            cwd: &mut cwd,
            fs,
            stdin,
            #[cfg(feature = "http_client")]
            http_client: None,
            #[cfg(feature = "git")]
            git_client: None,
            #[cfg(feature = "ssh")]
            ssh_client: None,
            shell: None,
        };

        Head.execute(ctx).await.unwrap()
    }

    async fn run_tail(args: &[&str], stdin: Option<&str>) -> ExecResult {
        let fs = Arc::new(InMemoryFs::new());
        let mut variables = HashMap::new();
        let env = HashMap::new();
        let mut cwd = PathBuf::from("/");

        let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
        let ctx = Context {
            args: &args,
            env: &env,
            variables: &mut variables,
            cwd: &mut cwd,
            fs,
            stdin,
            #[cfg(feature = "http_client")]
            http_client: None,
            #[cfg(feature = "git")]
            git_client: None,
            #[cfg(feature = "ssh")]
            ssh_client: None,
            shell: None,
        };

        Tail.execute(ctx).await.unwrap()
    }

    #[tokio::test]
    async fn test_head_default() {
        let input = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n";
        let result = run_head(&[], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n");
    }

    #[tokio::test]
    async fn test_head_n_flag() {
        let input = "a\nb\nc\nd\ne\n";
        let result = run_head(&["-n", "3"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\nc\n");
    }

    #[tokio::test]
    async fn test_head_shorthand() {
        let input = "a\nb\nc\nd\ne\n";
        let result = run_head(&["-2"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\n");
    }

    #[tokio::test]
    async fn test_tail_default() {
        let input = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n";
        let result = run_tail(&[], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n");
    }

    #[tokio::test]
    async fn test_tail_n_flag() {
        let input = "a\nb\nc\nd\ne\n";
        let result = run_tail(&["-n", "3"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "c\nd\ne\n");
    }

    #[tokio::test]
    async fn test_tail_shorthand() {
        let input = "a\nb\nc\nd\ne\n";
        let result = run_tail(&["-2"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "d\ne\n");
    }

    #[tokio::test]
    async fn test_head_empty_input() {
        let result = run_head(&[], Some("")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "");
    }

    #[tokio::test]
    async fn test_tail_empty_input() {
        let result = run_tail(&[], Some("")).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "");
    }

    #[tokio::test]
    async fn test_head_fewer_lines_than_requested() {
        let input = "a\nb\n";
        let result = run_head(&["-n", "10"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\n");
    }

    #[tokio::test]
    async fn test_tail_fewer_lines_than_requested() {
        let input = "a\nb\n";
        let result = run_tail(&["-n", "10"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\n");
    }

    #[tokio::test]
    async fn test_tail_plus_n_from_start() {
        let input = "header\nline1\nline2\nline3\n";
        let result = run_tail(&["-n", "+2"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "line1\nline2\nline3\n");
    }

    #[tokio::test]
    async fn test_tail_plus_1_all_lines() {
        let input = "a\nb\nc\n";
        let result = run_tail(&["-n", "+1"], Some(input)).await;
        assert_eq!(result.exit_code, 0);
        assert_eq!(result.stdout, "a\nb\nc\n");
    }
}