cli_util 0.2.35

Command-line utilitiy for unix based systems
Documentation
use std::{env, fs};
use std::io::{stdin, Error, Write, Read};

pub mod ls;
pub mod cd;
pub mod find;
pub mod grep;


/// Parsing command line
pub fn parse_command_line_args(command_line: &str) -> Vec<String> {
    // A vector to collect arguments
    let mut args = Vec::new();
    let mut current_arg = String::new();
    let mut in_quotes = false;

    for c in command_line.chars() {
        if c == '"' {
            in_quotes = !in_quotes;
            if !current_arg.is_empty() {
                args.push(current_arg.clone());
                current_arg.clear();
            }
        } else if c.is_whitespace() && !in_quotes {
            if !current_arg.is_empty() {
                args.push(current_arg.clone());
                current_arg.clear();
            }
        } else {
            current_arg.push(c);
        }
    }

    // En son argümanı ekleyin
    if !current_arg.is_empty() {
        args.push(current_arg);
    }
    args
}


/// Get current directory
pub fn working_directory() -> Result<String, Error> {
    let path = env::current_dir();
    let cwd = path?;
    Ok(cwd.display().to_string())
}

/// Concatenate and print (display) the content of files.
pub fn cat(file_path: &str, handle: &mut Box<dyn Write>) -> Result<(), Box<dyn std::error::Error>> {
    let mut buffer = Vec::new();

    if file_path.is_empty() {
        let mut stdin = stdin();
        match stdin.read_to_end(&mut buffer) {
            Ok(0) => {} // EOF (Ctrl+D) algılandı, döngüden çık
            Ok(_) => {
                // Veriyi işle
            }
            Err(e) => {
                eprintln!("Error reading from stdin: {}", e);
            }
        }
    } else {
        buffer = fs::read(file_path)?;
    };

    handle.write_all(&buffer)?;

    Ok(())
}