From 680b4e99394013c4589b4efdc0c954ef559df53d Mon Sep 17 00:00:00 2001 From: Artur Manuel Date: Thu, 8 Aug 2024 06:54:29 +0100 Subject: [PATCH] feat(complete): completed --- Cargo.toml | 2 -- src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 049ac53..f839af9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,3 @@ name = "ralsay-rs" version = "0.1.0" edition = "2021" - -[dependencies] diff --git a/src/main.rs b/src/main.rs index e7a11a9..c35a0c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,76 @@ -fn main() { - println!("Hello, world!"); +use std::env; + +fn text_wrap(text: &String, width: usize) -> Vec { + let mut lines: Vec = vec![]; + let mut s_buf: String = "".to_string(); + for word in text.split(" ") { + if s_buf.len() + word.len() >= width { + s_buf.pop(); + lines.push(s_buf); + s_buf = "".to_string(); + } + s_buf.push_str(&word); + s_buf.push_str(" "); + } + lines.push(s_buf); + lines +} + +fn ralsay(text: String) { + let art: &'static str = " + -^- + _/\\/_ \\/\\_ + (____ ))____) + / - - \\ + / ( ^)-(^ ) \\ + / ____v-v____ \\ + \\(- _- __)/ + \\- -__---/ + / / V \\ \\ + /__/\\ / \\ + = V = + _=_ _=_ + -=---______---=- + _| | | |_ + (___- -___) + "; + + let initial_width: usize = 40; + + let wrapped: Vec = text_wrap(&text, initial_width); + let mut max_length: usize = 0; + + for line in &wrapped { + if line.len() > max_length { + max_length = line.len() + } + } + + let box_width: usize = max_length + 2; + println!(" {}", "_".repeat(box_width),); + println!("/{}\\", " ".repeat(box_width)); + + for line in &wrapped { + println!("| {}{} |", line, " ".repeat(box_width - line.len() - 2)); + } + + println!("\\{}/", " ".repeat(box_width)); + println!(" {}", "=".repeat(box_width)); + println!("{}", art); +} + +fn check_args() -> Vec { + let mut args: Vec = env::args().collect(); + if args.len() == 1 { + eprintln!("You seem to have not written your message. Ralsei will therefore not respond to your command."); + std::process::exit(1); + } + args.remove(0); + + args +} + +fn main() { + let args: Vec = check_args(); + ralsay(args.join(" ")); }