Finished TP
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use std::io::{self, Write};
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -10,7 +11,10 @@ fn main() {
|
|||||||
print!("\x1b[2J\x1b[H");
|
print!("\x1b[2J\x1b[H");
|
||||||
println!("========== Justify some text ==========");
|
println!("========== Justify some text ==========");
|
||||||
println!(" A) Set file name (Name: {})", file_name);
|
println!(" A) Set file name (Name: {})", file_name);
|
||||||
println!(" B) Set column amount between 50 and 120 (Amount: {})", col_amount);
|
println!(
|
||||||
|
" B) Set column amount between 50 and 120 (Amount: {})",
|
||||||
|
col_amount
|
||||||
|
);
|
||||||
println!(" C) Justify and print text");
|
println!(" C) Justify and print text");
|
||||||
println!(" D) Justify and save text");
|
println!(" D) Justify and save text");
|
||||||
println!(" Quit (any other key)");
|
println!(" Quit (any other key)");
|
||||||
@@ -31,10 +35,10 @@ fn main() {
|
|||||||
col_amount = set_col_amount();
|
col_amount = set_col_amount();
|
||||||
}
|
}
|
||||||
"c" => {
|
"c" => {
|
||||||
print_justified_text();
|
print_justified_text(&file_name, col_amount);
|
||||||
}
|
}
|
||||||
"d" => {
|
"d" => {
|
||||||
save_justified_text();
|
save_justified_text(&file_name, col_amount);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
run = false;
|
run = false;
|
||||||
@@ -56,9 +60,8 @@ fn set_file_name() -> String {
|
|||||||
.expect("Failed to read from stdin");
|
.expect("Failed to read from stdin");
|
||||||
path = path.trim().to_string();
|
path = path.trim().to_string();
|
||||||
|
|
||||||
if Path::new(&path).exists() {
|
path_iscorrect = Path::new(&path).exists();
|
||||||
path_iscorrect = true;
|
if !path_iscorrect {
|
||||||
} else {
|
|
||||||
println!("The specified file does not exist. Please try again.");
|
println!("The specified file does not exist. Please try again.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,11 +70,98 @@ fn set_file_name() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_col_amount() -> i8 {
|
fn set_col_amount() -> i8 {
|
||||||
8
|
let mut input_text: String;
|
||||||
|
let mut col_amount: i8 = 0;
|
||||||
|
let mut amount_iscorrect: bool = false;
|
||||||
|
|
||||||
|
while !amount_iscorrect {
|
||||||
|
input_text = String::new();
|
||||||
|
print!("Enter the desired column amount: ");
|
||||||
|
let _ = io::stdout().flush();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input_text)
|
||||||
|
.expect("failed to read from stdin");
|
||||||
|
|
||||||
|
match input_text.trim().parse::<i8>() {
|
||||||
|
Ok(i) => {
|
||||||
|
col_amount = i;
|
||||||
|
amount_iscorrect = col_amount >= 50 && col_amount <= 120;
|
||||||
|
if !amount_iscorrect {
|
||||||
|
println!("The specified amount was not between 50 and 120. Please try again.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(..) => println!(
|
||||||
|
"input is not an integer: {}. Please try again.",
|
||||||
|
input_text.trim()
|
||||||
|
),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_justified_text() {}
|
return col_amount;
|
||||||
|
}
|
||||||
|
|
||||||
fn save_justified_text() {}
|
fn print_justified_text(filename: &str, col_amount: i8) {
|
||||||
|
if let Ok(lines) = read_lines(filename) {
|
||||||
|
for line in lines.flatten() {
|
||||||
|
let justified_text = justify_text(&line, col_amount);
|
||||||
|
println!("{}", justified_text);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Failed to read the file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn load_justify_text() {}
|
fn save_justified_text(filename: &str, col_amount: i8) {
|
||||||
|
let output_file = "output.txt";
|
||||||
|
let mut file = File::create(output_file).expect("Failed to create output file.");
|
||||||
|
|
||||||
|
if let Ok(lines) = read_lines(filename) {
|
||||||
|
for line in lines.flatten() {
|
||||||
|
let justified_text = justify_text(&line, col_amount);
|
||||||
|
writeln!(file, "{}", justified_text).expect("Failed to write to output file.");
|
||||||
|
}
|
||||||
|
println!("Written to {}", output_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn justify_text(text: &str, col_amount: i8) -> String {
|
||||||
|
if text.len() >= col_amount as usize {
|
||||||
|
return text.to_string();
|
||||||
|
}
|
||||||
|
if text == "" {
|
||||||
|
return text.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let words: Vec<&str> = text.split_whitespace().collect();
|
||||||
|
|
||||||
|
if words.len() == 1 {
|
||||||
|
return format!("{:<width$}", words[0], width = col_amount as usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut justified = String::new();
|
||||||
|
let words_len: usize = words.iter().map(|word| word.len()).sum();
|
||||||
|
let total_spaces = (col_amount as usize) - words_len;
|
||||||
|
let gaps = words.len() - 1;
|
||||||
|
let spaces_per_gap = total_spaces / gaps;
|
||||||
|
let extra_spaces = total_spaces % gaps;
|
||||||
|
|
||||||
|
for (i, word) in words.iter().enumerate() {
|
||||||
|
justified.push_str(word);
|
||||||
|
if i < gaps {
|
||||||
|
justified.push_str(&" ".repeat(spaces_per_gap));
|
||||||
|
if i < extra_spaces {
|
||||||
|
justified.push(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return justified;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
Ok(io::BufReader::new(file).lines())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user