Skip to content

Commit

Permalink
feat(tui): add colorized text output
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Nov 24, 2023
1 parent 16fdab3 commit 1709b4e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod tui;

mod cli;
use cli::{Commands, CLI};

Expand All @@ -9,6 +11,8 @@ use clap::Parser;
fn main() {
let cli = CLI::parse();

print!("\n");

match &cli.command {
Some(Commands::Square(square::Command { base, height })) => square::view(*base, *height),
Some(Commands::Circle(circle::Command { radius })) => circle::view(*radius),
Expand Down
7 changes: 6 additions & 1 deletion src/shapes/circle/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::shapes::circle::Circle;
use crate::tui;

pub fn view(radius: f64) {
let circle = Circle { radius };

println!("{}cm", circle.get_area());
println!(
"{}{}",
tui::title("Area"),
tui::content(circle.get_area().to_string().as_str())
);
}
8 changes: 6 additions & 2 deletions src/shapes/rhombus/view.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use crate::shapes::rhombus::Rhombus;
use crate::{shapes::rhombus::Rhombus, tui};

pub fn view(s_diagonal: f32, l_diagonal: f32) {
let rhombus = Rhombus {
l_diagonal,
s_diagonal,
};

println!("{}cm", rhombus.get_area())
println!(
"{}{}",
tui::title("Area"),
tui::content(rhombus.get_area().to_string().as_str())
)
}
8 changes: 6 additions & 2 deletions src/shapes/square/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::shapes::square::Square;
use crate::{shapes::square::Square, tui};

pub fn view(base: i32, height: i32) {
let square = Square { base, height };

println!("{}cm", square.get_area());
println!(
"{}{}",
tui::title("Area"),
tui::content(square.get_area().to_string().as_str())
);
}
8 changes: 6 additions & 2 deletions src/shapes/trapezoid/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::shapes::trapezoid::Trapezoid;
use crate::{shapes::trapezoid::Trapezoid, tui};

pub fn view(s_base: f32, l_base: f32, height: f32) {
let trapezoid = Trapezoid {
Expand All @@ -7,5 +7,9 @@ pub fn view(s_base: f32, l_base: f32, height: f32) {
s_base,
};

println!("Area: {}cm", trapezoid.get_area())
println!(
"{}{}",
tui::title("Area"),
tui::content(trapezoid.get_area().to_string().as_str())
)
}
24 changes: 20 additions & 4 deletions src/shapes/triangle/view.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::shapes::triangle::{SidesTriangle, SimpleTriangle};
use crate::{
shapes::triangle::{SidesTriangle, SimpleTriangle},
tui,
};

pub fn view(
base: Option<f32>,
Expand All @@ -11,15 +14,28 @@ pub fn view(
if let (Some(base), Some(height)) = (base, height) {
let triangle = SimpleTriangle { base, height };

println!("Area: {}cm", triangle.get_area());
println!(
"{}{}",
tui::title("Area"),
tui::content(triangle.get_area().to_string().as_str())
);
} else if let (Some(side_a), Some(side_b), Some(side_c)) = (side_a, side_b, side_c) {
let triangle = SidesTriangle {
side_a,
side_b,
side_c,
};

println!("Area: {}cm", triangle.get_area());
println!("Perimeter: {}cm", triangle.get_semi_perimeter());
println!(
"{}{}",
tui::title("Area"),
tui::content(triangle.get_area().to_string().as_str())
);
print!("\n");
println!(
"{}{}",
tui::title("Perimeter"),
tui::content(triangle.get_semi_perimeter().to_string().as_str())
);
}
}
15 changes: 15 additions & 0 deletions src/tui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use colored::*;

pub fn title(title: &str) -> ColoredString {
format!(" {} ", title)
.bold()
.truecolor(42, 157, 143)
.on_truecolor(38, 70, 83)
}

pub fn content(value: &str) -> ColoredString {
format!(" {} ", value)
.truecolor(42, 157, 143)
.on_truecolor(42, 157, 143)
.bold()
}

0 comments on commit 1709b4e

Please sign in to comment.