fastshell 0.3.4

Lightweight Bash + Python runtime SDK for mobile AI agents
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (c) 2025 xiefujin <490021684@qq.com>
// Licensed under Apache-2.0, see LICENSE file for full license terms.

//! Additional common commands filling coverage gaps: `arch`, `factor`,
//! `numfmt`, `pr`.

use crate::shell::{CommandOutput, Shell};

impl Shell {
    /// `arch` — print the machine architecture (same as `uname -m`).
    pub fn cmd_arch(&self, _args: &[&str]) -> CommandOutput {
        CommandOutput::success(format!("{}\n", std::env::consts::ARCH))
    }

    /// `factor N...` — prime-factorise each integer.
    pub fn cmd_factor(&self, args: &[&str]) -> CommandOutput {
        let mut out = String::new();
        let mut nums: Vec<&str> = args
            .iter()
            .copied()
            .filter(|a| !a.starts_with('-'))
            .collect();
        if nums.is_empty() {
            return CommandOutput::error("factor: missing operand\n".to_string(), 1);
        }
        // Keep the input order stable.
        let mut seen = std::collections::HashSet::new();
        for a in args {
            if a.starts_with('-') {
                continue;
            }
            if !seen.insert(*a) {
                continue;
            }
            let n: u64 = match a.trim().parse() {
                Ok(v) => v,
                Err(_) => {
                    return CommandOutput::error(
                        format!("factor: '{}' is not a valid integer\n", a),
                        1,
                    )
                }
            };
            out.push_str(&format!("{}:", n));
            for f in prime_factors(n) {
                out.push_str(&format!(" {}", f));
            }
            out.push('\n');
        }
        nums.clear();
        CommandOutput::success(out)
    }

    /// `numfmt [--to=si|iec|iec-i|none] [--from=si|iec|none] [--suffix=S] N...`
    /// — a pragmatic subset of GNU `numfmt` (defaults to `--to=si`).
    pub fn cmd_numfmt(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut to = String::from("si");
        let mut from = String::from("none");
        let mut suffix = String::new();
        let mut nums: Vec<String> = Vec::new();
        let mut i = 0;
        while i < args.len() {
            let a = args[i];
            if let Some(v) = a.strip_prefix("--to=") {
                to = v.to_string();
            } else if let Some(v) = a.strip_prefix("--from=") {
                from = v.to_string();
            } else if let Some(v) = a.strip_prefix("--suffix=") {
                suffix = v.to_string();
            } else if a == "--to" || a == "--from" || a == "--suffix" {
                i += 1;
                if let Some(v) = args.get(i) {
                    match a {
                        "--to" => to = v.to_string(),
                        "--from" => from = v.to_string(),
                        _ => suffix = v.to_string(),
                    }
                }
            } else if a.starts_with('-') && a.len() > 1 && a != "-" {
                // ignore other flags (e.g. --round)
            } else {
                nums.push(a.to_string());
            }
            i += 1;
        }
        if nums.is_empty() {
            // GNU numfmt reads whitespace/newline-separated numbers from stdin
            // when no operands are given.
            if let Some(s) = stdin {
                nums.extend(
                    s.split_whitespace()
                        .filter(|t| !t.is_empty())
                        .map(|t| t.to_string()),
                );
            }
        }
        let mut out = String::new();
        for raw in &nums {
            let value = parse_human(raw, &from);
            if value.is_nan() {
                return CommandOutput::error(format!("numfmt: invalid number: '{}'\n", raw), 1);
            }
            out.push_str(&format_human(value, &to));
            out.push_str(&suffix);
            out.push('\n');
        }
        CommandOutput::success(out)
    }

    /// `pr [-t] [-n[SEP]] [FILE...]` — paginate text (header/footer unless `-t`).
    pub fn cmd_pr(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut omit_header = false;
        let mut number = false;
        let mut number_sep = "\t".to_string();
        let mut files: Vec<&str> = Vec::new();
        for a in args {
            if *a == "-t" {
                omit_header = true;
            } else if *a == "-n" {
                number = true;
            } else if let Some(rest) = a.strip_prefix("-n") {
                number = true;
                if !rest.is_empty() {
                    number_sep = rest.to_string();
                }
            } else if a.starts_with('-') && a.len() > 1 {
                // ignore unsupported flags (columns/width/etc.)
            } else {
                files.push(a);
            }
        }
        let text = if files.is_empty() {
            match stdin {
                Some(s) => s.to_string(),
                None => return CommandOutput::error("pr: missing input\n".to_string(), 1),
            }
        } else {
            let mut all = String::new();
            for f in &files {
                match self.read_text_lossy(f) {
                    Ok(c) => all.push_str(&c),
                    Err(e) => return CommandOutput::error(format!("pr: {}: {}\n", f, e), 1),
                }
            }
            all
        };
        let lines: Vec<&str> = text.lines().collect();
        let mut out = String::new();
        let now = "2026-01-01";
        for (idx, line) in lines.iter().enumerate() {
            let page_line = idx % 66;
            if !omit_header {
                if page_line == 0 {
                    out.push_str(&format!(
                        "\n\n{} {} Page {}\n\n\n",
                        now,
                        files.first().copied().unwrap_or(""),
                        idx / 66 + 1
                    ));
                }
            }
            if number {
                out.push_str(&format!("{:>5}{}{}\n", idx + 1, number_sep, line));
            } else {
                out.push_str(line);
                out.push('\n');
            }
            if !omit_header && page_line == 60 {
                out.push_str("\n\n\n\n\n");
            }
        }
        CommandOutput::success(out)
    }

    /// `mapfile [-t] [-n N] [ARRAY]` / `readarray` — read stdin lines into an
    /// array (default `MAPFILE`).
    pub fn cmd_mapfile(&mut self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut strip_nl = false;
        let mut name = "MAPFILE".to_string();
        let mut max: Option<usize> = None;
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-t" | "--trim" => strip_nl = true,
                "-n" => {
                    i += 1;
                    max = args.get(i).and_then(|s| s.parse().ok());
                }
                a if a.starts_with('-') => {}
                a => name = a.to_string(),
            }
            i += 1;
        }
        let input = stdin.unwrap_or("");
        let mut items: Vec<String> = if strip_nl {
            input.lines().map(|l| l.to_string()).collect()
        } else {
            input.split_inclusive('\n').map(|l| l.to_string()).collect()
        };
        if let Some(n) = max {
            items.truncate(n);
        }
        self.arrays.insert(name, items);
        CommandOutput::success(String::new())
    }

    /// `fmt [-w WIDTH] [FILE...]` — reflow paragraphs to WIDTH columns (75).
    pub fn cmd_fmt(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut width = 75usize;
        let mut files: Vec<&str> = Vec::new();
        let mut i = 0;
        while i < args.len() {
            match args[i] {
                "-w" | "--width" => {
                    i += 1;
                    if let Some(w) = args.get(i) {
                        width = w.parse().unwrap_or(75).max(1);
                    }
                }
                a if a.starts_with("-w") && a.len() > 2 => {
                    width = a[2..].parse().unwrap_or(75).max(1);
                }
                a if a.starts_with('-') && a.len() > 1 => {}
                a => files.push(a),
            }
            i += 1;
        }
        let text = if files.is_empty() {
            match stdin {
                Some(s) => s.to_string(),
                None => return CommandOutput::error("fmt: missing input\n".to_string(), 1),
            }
        } else {
            let mut all = String::new();
            for f in &files {
                match self.read_text_lossy(f) {
                    Ok(c) => all.push_str(&c),
                    Err(e) => return CommandOutput::error(format!("fmt: {}: {}\n", f, e), 1),
                }
            }
            all
        };
        let mut out = String::new();
        for para in text.split("\n\n") {
            let words: Vec<&str> = para.split_whitespace().collect();
            let mut line = String::new();
            for w in words {
                if line.is_empty() {
                    line.push_str(w);
                } else if line.len() + 1 + w.len() <= width {
                    line.push(' ');
                    line.push_str(w);
                } else {
                    out.push_str(&line);
                    out.push('\n');
                    line = w.to_string();
                }
            }
            out.push_str(&line);
            out.push_str("\n\n");
        }
        CommandOutput::success(out)
    }

    /// `stdbuf [-i/-o/-e MODE] CMD [ARGS...]` — buffering flags are ignored
    /// (in-process); the command runs normally.
    pub fn cmd_stdbuf(&mut self, args: &[&str]) -> CommandOutput {
        let mut i = 0;
        while i < args.len() {
            let a = args[i];
            if a == "-i" || a == "-o" || a == "-e" {
                i += 2;
                continue;
            }
            if a.starts_with('-') && a.len() > 1 {
                i += 1;
                continue;
            }
            break;
        }
        match args.get(i) {
            None => CommandOutput::error("stdbuf: missing command\n".to_string(), 125),
            Some(cmd) => self.execute(cmd, &args[i + 1..], None),
        }
    }

    /// `zstd [-d] [-c] [-k] [-#] [FILE]` — zstd compress/decompress.
    pub fn cmd_zstd(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut decompress = false;
        let mut to_stdout = false;
        let mut keep = false;
        let mut level: i32 = 3;
        let mut files: Vec<&str> = Vec::new();
        for a in args {
            match *a {
                "-d" | "--decompress" => decompress = true,
                "-c" | "--stdout" => to_stdout = true,
                "-k" | "--keep" => keep = true,
                _ if a.starts_with("--") => {}
                _ if a.starts_with('-') && a.len() > 1 => {
                    for ch in a[1..].chars() {
                        match ch {
                            'd' => decompress = true,
                            'c' => to_stdout = true,
                            'k' => keep = true,
                            '1'..='9' => level = ch.to_digit(10).unwrap() as i32,
                            _ => {}
                        }
                    }
                }
                _ => files.push(a),
            }
        }
        let (input, in_name): (Vec<u8>, Option<String>) = if files.is_empty() {
            match self
                .take_binary_in()
                .or_else(|| stdin.map(|s| s.as_bytes().to_vec()))
            {
                Some(b) => (b, None),
                None => return CommandOutput::error("zstd: missing input\n".to_string(), 1),
            }
        } else {
            match self.vfs.read(files[0], &self.cwd) {
                Ok(b) => (b, Some(files[0].to_string())),
                Err(e) => return CommandOutput::error(format!("zstd: {}: {}\n", files[0], e), 1),
            }
        };
        let result = if decompress {
            match zstd::stream::decode_all(&input[..]) {
                Ok(d) => d,
                Err(e) => return CommandOutput::error(format!("zstd: {}\n", e), 1),
            }
        } else {
            match zstd::stream::encode_all(&input[..], level) {
                Ok(c) => c,
                Err(e) => return CommandOutput::error(format!("zstd: {}\n", e), 1),
            }
        };
        if to_stdout || in_name.is_none() {
            self.set_binary_out(result.clone());
            CommandOutput::success(String::from_utf8_lossy(&result).to_string())
        } else {
            let name = in_name.unwrap();
            let out_name = if decompress {
                name.strip_suffix(".zst")
                    .or_else(|| name.strip_suffix(".zstd"))
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| format!("{}.out", name))
            } else {
                format!("{}.zst", name)
            };
            match self.vfs.write_bytes(&out_name, &self.cwd, &result) {
                Ok(_) => {
                    if !keep {
                        let _ = self.vfs.remove_file(&name, &self.cwd);
                    }
                    CommandOutput::success(format!("{}: {} bytes\n", out_name, result.len()))
                }
                Err(e) => CommandOutput::error(format!("zstd: {}: {}\n", out_name, e), 1),
            }
        }
    }

    /// `unzstd` / `zstdcat` — decompress.
    pub fn cmd_unzstd(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let mut a: Vec<&str> = vec!["-d"];
        a.extend_from_slice(args);
        self.cmd_zstd(&a, stdin)
    }
    /// `unlink FILE` — remove a single file.
    pub fn cmd_unlink(&self, args: &[&str]) -> CommandOutput {
        let file = match args.iter().find(|a| !a.starts_with('-')) {
            Some(f) => *f,
            None => return CommandOutput::error("unlink: missing operand\n".to_string(), 1),
        };
        match self.vfs.remove_file(file, &self.cwd) {
            Ok(_) => CommandOutput::success(String::new()),
            Err(e) => CommandOutput::error(format!("unlink: {}: {}\n", file, e), 1),
        }
    }

    /// `link SRC DST` — hard link (falls back to a copy when linking fails).
    pub fn cmd_link(&self, args: &[&str]) -> CommandOutput {
        let files: Vec<&str> = args
            .iter()
            .copied()
            .filter(|a| !a.starts_with('-'))
            .collect();
        if files.len() < 2 {
            return CommandOutput::error("link: missing operand\n".to_string(), 1);
        }
        let (src, dst) = (files[0], files[1]);
        let (sp, dp) = match (
            self.vfs.resolve(src, &self.cwd),
            self.vfs.resolve(dst, &self.cwd),
        ) {
            (Ok(s), Ok(d)) => (s, d),
            _ => return CommandOutput::error(format!("link: {}: invalid path\n", src), 1),
        };
        if std::fs::hard_link(&sp, &dp).is_ok() {
            return CommandOutput::success(String::new());
        }
        match self.vfs.read(src, &self.cwd) {
            Ok(data) => match self.vfs.write_bytes(dst, &self.cwd, &data) {
                Ok(_) => CommandOutput::success(String::new()),
                Err(e) => CommandOutput::error(format!("link: {}: {}\n", dst, e), 1),
            },
            Err(e) => CommandOutput::error(format!("link: {}: {}\n", src, e), 1),
        }
    }

    /// `whereis NAME` — print the resolved path for NAME (builtin → "builtin").
    pub fn cmd_whereis(&self, args: &[&str]) -> CommandOutput {
        let mut out = String::new();
        for a in args.iter().filter(|a| !a.starts_with('-')) {
            out.push_str(&format!("{}: {}\n", a, a));
        }
        CommandOutput::success(out)
    }

    /// `envsubst [SHELL-FORMAT]` — substitute `$VAR`/`${VAR}` from the shell env.
    pub fn cmd_envsubst(&self, args: &[&str], stdin: Option<&str>) -> CommandOutput {
        let input = match stdin {
            Some(s) => s,
            None => return CommandOutput::error("envsubst: missing input\n".to_string(), 1),
        };
        let _ = args; // SHELL-FORMAT filtering is not modelled; substitute all.
        let chars: Vec<char> = input.chars().collect();
        let mut out = String::new();
        let mut i = 0;
        while i < chars.len() {
            if chars[i] == '$' && i + 1 < chars.len() {
                if chars[i + 1] == '{' {
                    if let Some(close) = chars[i + 2..].iter().position(|&c| c == '}') {
                        let name: String = chars[i + 2..i + 2 + close].iter().collect();
                        out.push_str(&self.vars.get(&name).cloned().unwrap_or_default());
                        i = i + 2 + close + 1;
                        continue;
                    }
                } else if chars[i + 1].is_ascii_alphabetic() || chars[i + 1] == '_' {
                    let start = i + 1;
                    let mut j = start;
                    while j < chars.len() && (chars[j].is_ascii_alphanumeric() || chars[j] == '_') {
                        j += 1;
                    }
                    let name: String = chars[start..j].iter().collect();
                    out.push_str(&self.vars.get(&name).cloned().unwrap_or_default());
                    i = j;
                    continue;
                }
            }
            out.push(chars[i]);
            i += 1;
        }
        CommandOutput::success(out)
    }
}

fn prime_factors(mut n: u64) -> Vec<u64> {
    let mut out = Vec::new();
    if n < 2 {
        return out;
    }
    while n % 2 == 0 {
        out.push(2);
        n /= 2;
    }
    let mut f = 3u64;
    while f.saturating_mul(f) <= n {
        while n % f == 0 {
            out.push(f);
            n /= f;
        }
        f += 2;
    }
    if n > 1 {
        out.push(n);
    }
    out
}

fn parse_human(s: &str, from: &str) -> f64 {
    let s = s.trim();
    if from == "none" {
        // Allow a trailing SI/IEC suffix in the input too.
        let (num, mult) = split_suffix(s);
        return num * mult;
    }
    split_suffix(s).0 * split_suffix(s).1
}

fn split_suffix(s: &str) -> (f64, f64) {
    let bytes = s.as_bytes();
    let mut end = bytes.len();
    while end > 0 {
        let c = bytes[end - 1] as char;
        if c.is_ascii_digit() || c == '.' {
            break;
        }
        end -= 1;
    }
    let num: f64 = s[..end].parse().unwrap_or(f64::NAN);
    let suf = &s[end..];
    let mult = match suf {
        "" => 1.0,
        "K" | "k" => 1e3,
        "M" => 1e6,
        "G" => 1e9,
        "T" => 1e12,
        "P" => 1e15,
        "Ki" => 1024.0,
        "Mi" => 1024f64.powi(2),
        "Gi" => 1024f64.powi(3),
        "Ti" => 1024f64.powi(4),
        _ => f64::NAN,
    };
    (num, mult)
}

fn format_human(v: f64, to: &str) -> String {
    match to {
        "si" => {
            let units = ["", "K", "M", "G", "T", "P"];
            let mut x = v;
            let mut u = 0;
            while x.abs() >= 1000.0 && u + 1 < units.len() {
                x /= 1000.0;
                u += 1;
            }
            if u == 0 {
                format!("{}", v)
            } else {
                format!("{:.1}{}", x, units[u])
            }
        }
        "iec" | "iec-i" => {
            let units = ["", "Ki", "Mi", "Gi", "Ti", "Pi"];
            let mut x = v;
            let mut u = 0;
            while x.abs() >= 1024.0 && u + 1 < units.len() {
                x /= 1024.0;
                u += 1;
            }
            if u == 0 {
                format!("{}", v)
            } else {
                format!("{:.1}{}", x, units[u])
            }
        }
        _ => format!("{}", v),
    }
}