cli_util/
lib.rs

1use std::{env, fs};
2use std::io::{stdin, Error, Write, Read};
3
4pub mod ls;
5pub mod cd;
6pub mod find;
7pub mod grep;
8
9
10/// Parsing command line
11pub fn parse_command_line_args(command_line: &str) -> Vec<String> {
12    // A vector to collect arguments
13    let mut args = Vec::new();
14    let mut current_arg = String::new();
15    let mut in_quotes = false;
16
17    for c in command_line.chars() {
18        if c == '"' {
19            in_quotes = !in_quotes;
20            if !current_arg.is_empty() {
21                args.push(current_arg.clone());
22                current_arg.clear();
23            }
24        } else if c.is_whitespace() && !in_quotes {
25            if !current_arg.is_empty() {
26                args.push(current_arg.clone());
27                current_arg.clear();
28            }
29        } else {
30            current_arg.push(c);
31        }
32    }
33
34    // En son argümanı ekleyin
35    if !current_arg.is_empty() {
36        args.push(current_arg);
37    }
38    args
39}
40
41
42/// Get current directory
43pub fn working_directory() -> Result<String, Error> {
44    let path = env::current_dir();
45    let cwd = path?;
46    Ok(cwd.display().to_string())
47}
48
49/// Concatenate and print (display) the content of files.
50pub fn cat(file_path: &str, handle: &mut Box<dyn Write>) -> Result<(), Box<dyn std::error::Error>> {
51    let mut buffer = Vec::new();
52
53    if file_path.is_empty() {
54        let mut stdin = stdin();
55        match stdin.read_to_end(&mut buffer) {
56            Ok(0) => {} // EOF (Ctrl+D) algılandı, döngüden çık
57            Ok(_) => {
58                // Veriyi işle
59            }
60            Err(e) => {
61                eprintln!("Error reading from stdin: {}", e);
62            }
63        }
64    } else {
65        buffer = fs::read(file_path)?;
66    };
67
68    handle.write_all(&buffer)?;
69
70    Ok(())
71}