Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sunrisewestern committed Jan 3, 2025
0 parents commit 8e522db
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Compiled binary files
/target

# Dependency lock file
Cargo.lock

# IDE files
.idea
*.iml
*.ipr
*.iws
.vscode/

# OS generated files
.DS_Store
Thumbs.db

# Rust-specific files
*.rs.bk
*.rlib
*.rmeta
*.d
*.so
*.dll
*.exe
*.pdb

# Backup files
*~
*.bak
*.swp

# Editor backup files
*.swo

# Temporary files
*.tmp

14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "header"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = "3.0.0"

[profile.release]
lto = true
codegen-units = 1
strip = true
82 changes: 82 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use clap::{App, Arg};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::process;

fn main() {
let matches = App::new("header")
.version("1.0")
.author("Your Name")
.about("Prints the first line of a file, optionally with field numbers.")
.arg(
Arg::with_name("delimiter")
.short('d')
.long("delimiter")
.value_name("DELIMITER")
.help("Sets the field delimiter")
.default_value("\t"),
)
.arg(
Arg::with_name("number")
.short('n')
.long("number")
.help("Show the field number"),
)
.arg(
Arg::with_name("input")
.value_name("FILE")
.help("Input file to process")
.required(true)
.index(1),
)
.get_matches();

let delimiter = matches.value_of("delimiter").unwrap();
let show_number = matches.is_present("number");
let input_file = matches.value_of("input").unwrap();

let file = File::open(input_file).unwrap_or_else(|err| {
eprintln!("Error opening file: {}", err);
process::exit(1);
});

let reader = BufReader::new(file);

if let Some(Ok(line)) = reader.lines().next() {
let fields: Vec<&str> = line.split(delimiter).collect();
for (index, field) in fields.iter().enumerate() {
if show_number {
println!("{}\t{}", field, index + 1);
} else {
println!("{}", field);
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;

#[test]
fn test_main() {
let test_input = "field1\tfield2\tfield3\n";
let mut test_file = tempfile::NamedTempFile::new().expect("Failed to create temp file");
write!(test_file, "{}", test_input).expect("Failed to write to temp file");

let test_file_path = test_file.path().to_str().unwrap();

let output = std::process::Command::new(std::env::current_exe().unwrap())
.arg("-d")
.arg("\t")
.arg("-n")
.arg(test_file_path)
.output()
.expect("Failed to execute process");

assert!(output.status.success());
let output_str = String::from_utf8_lossy(&output.stdout);
assert_eq!(output_str.trim(), "field1 1\nfield2 2\nfield3 3");
}
}

0 comments on commit 8e522db

Please sign in to comment.