Skip to content

Commit 1709b4e

Browse files
committed
feat(tui): add colorized text output
1 parent 16fdab3 commit 1709b4e

File tree

7 files changed

+63
-11
lines changed

7 files changed

+63
-11
lines changed

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod tui;
2+
13
mod cli;
24
use cli::{Commands, CLI};
35

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

14+
print!("\n");
15+
1216
match &cli.command {
1317
Some(Commands::Square(square::Command { base, height })) => square::view(*base, *height),
1418
Some(Commands::Circle(circle::Command { radius })) => circle::view(*radius),

src/shapes/circle/view.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use crate::shapes::circle::Circle;
2+
use crate::tui;
23

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

6-
println!("{}cm", circle.get_area());
7+
println!(
8+
"{}{}",
9+
tui::title("Area"),
10+
tui::content(circle.get_area().to_string().as_str())
11+
);
712
}

src/shapes/rhombus/view.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
use crate::shapes::rhombus::Rhombus;
1+
use crate::{shapes::rhombus::Rhombus, tui};
22

33
pub fn view(s_diagonal: f32, l_diagonal: f32) {
44
let rhombus = Rhombus {
55
l_diagonal,
66
s_diagonal,
77
};
88

9-
println!("{}cm", rhombus.get_area())
9+
println!(
10+
"{}{}",
11+
tui::title("Area"),
12+
tui::content(rhombus.get_area().to_string().as_str())
13+
)
1014
}

src/shapes/square/view.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::shapes::square::Square;
1+
use crate::{shapes::square::Square, tui};
22

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

6-
println!("{}cm", square.get_area());
6+
println!(
7+
"{}{}",
8+
tui::title("Area"),
9+
tui::content(square.get_area().to_string().as_str())
10+
);
711
}

src/shapes/trapezoid/view.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::shapes::trapezoid::Trapezoid;
1+
use crate::{shapes::trapezoid::Trapezoid, tui};
22

33
pub fn view(s_base: f32, l_base: f32, height: f32) {
44
let trapezoid = Trapezoid {
@@ -7,5 +7,9 @@ pub fn view(s_base: f32, l_base: f32, height: f32) {
77
s_base,
88
};
99

10-
println!("Area: {}cm", trapezoid.get_area())
10+
println!(
11+
"{}{}",
12+
tui::title("Area"),
13+
tui::content(trapezoid.get_area().to_string().as_str())
14+
)
1115
}

src/shapes/triangle/view.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::shapes::triangle::{SidesTriangle, SimpleTriangle};
1+
use crate::{
2+
shapes::triangle::{SidesTriangle, SimpleTriangle},
3+
tui,
4+
};
25

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

14-
println!("Area: {}cm", triangle.get_area());
17+
println!(
18+
"{}{}",
19+
tui::title("Area"),
20+
tui::content(triangle.get_area().to_string().as_str())
21+
);
1522
} else if let (Some(side_a), Some(side_b), Some(side_c)) = (side_a, side_b, side_c) {
1623
let triangle = SidesTriangle {
1724
side_a,
1825
side_b,
1926
side_c,
2027
};
2128

22-
println!("Area: {}cm", triangle.get_area());
23-
println!("Perimeter: {}cm", triangle.get_semi_perimeter());
29+
println!(
30+
"{}{}",
31+
tui::title("Area"),
32+
tui::content(triangle.get_area().to_string().as_str())
33+
);
34+
print!("\n");
35+
println!(
36+
"{}{}",
37+
tui::title("Perimeter"),
38+
tui::content(triangle.get_semi_perimeter().to_string().as_str())
39+
);
2440
}
2541
}

src/tui.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use colored::*;
2+
3+
pub fn title(title: &str) -> ColoredString {
4+
format!(" {} ", title)
5+
.bold()
6+
.truecolor(42, 157, 143)
7+
.on_truecolor(38, 70, 83)
8+
}
9+
10+
pub fn content(value: &str) -> ColoredString {
11+
format!(" {} ", value)
12+
.truecolor(42, 157, 143)
13+
.on_truecolor(42, 157, 143)
14+
.bold()
15+
}

0 commit comments

Comments
 (0)