Commit initial

This commit is contained in:
2024-09-12 08:55:26 +02:00
commit 92ab4d0087
8 changed files with 387 additions and 0 deletions

110
src/main.rs Normal file
View File

@@ -0,0 +1,110 @@
use clap::Parser;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
#[derive(Parser, Debug)]
#[command(version)]
struct Args {
/// Numérotation des lignes non vides
#[arg(short = 'b', long)]
number_nonblank: bool,
/// Affichage d'un $ à la fin de chaque ligne
#[arg(short = 'E', long)]
show_ends: bool,
/// Numérotation des lignes
#[arg(short, long)]
number: bool,
/// Suppression des lignes vides répétées
#[arg(short, long)]
squeeze_blank: bool,
/// Affichage des tabulations avec la séquence ^I
#[arg(short = 'T', long)]
show_tabs: bool,
/// Ignoré (conservé par compatibilité)
#[arg(short = 'u')]
_u: bool,
/// Utilisation de la notation ^ et M- sauf pour LFD et TAB
#[arg(short = 'v', long)]
show_nonprinting: bool,
/// Equivalent à -vET
#[arg(short = 'A', long)]
show_all: bool,
/// Equivalent à -vE
#[arg(short = 'e')]
show_all_no_tabs: bool,
/// Equivalent à -vT
#[arg(short = 't')]
show_all_no_ends: bool,
/// Liste de fichiers
files: Vec<String>,
}
fn main() {
let mut args = Args::parse();
args.show_nonprinting = args.show_nonprinting || args.show_all || args.show_all_no_tabs || args.show_all_no_ends;
args.show_ends = args.show_ends || args.show_all || args.show_all_no_tabs;
args.show_tabs = args.show_tabs || args.show_all || args.show_all_no_ends;
args.number_nonblank = args.number_nonblank && !args.number;
//args.files = vec!(String::from("fichier1"));
let line_ends = if args.show_ends { "$" } else { "" };
let mut line_number = 0;
let mut line_prefix: String;
let mut last_was_blank = false;
for file_path in args.files {
let f = File::open(file_path).unwrap();
let mut reader = BufReader::new(f);
loop {
let mut line = String::new();
if let Ok(len) = reader.read_line(&mut line) {
if len==0 {
break;
}
let line_is_blank = line.len()==0;
let skip_line = args.squeeze_blank && last_was_blank && line_is_blank;
if args.number || (args.number_nonblank && !line_is_blank) {
line_number += 1;
line_prefix = format!("{:>6} ", line_number);
if line_prefix.len()<8 {
line_prefix.push(' ');
}
} else {
line_prefix = String::from("");
}
let mut line_to_display = String::from("");
for c in line.to_string().chars() {
match c as u8 {
9 => if args.show_tabs { line_to_display.push(c) } else { line_to_display.push_str("^I") }
10 => {},
0 ..= 127 => line_to_display.push(c),
128 ..= 255 => if args.show_nonprinting { line_to_display.push_str("M^"); line_to_display.push((c as u8 - 128) as char) } else { line_to_display.push(c) },
_ => {}
};
}
if !skip_line {
println!("{}{}{}", line_prefix, line_to_display, line_ends);
}
last_was_blank = line_is_blank;
}
}
}
}