Skip to content

Commit

Permalink
refactor(terminal): use crossterm Style trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Lioness100 committed Aug 14, 2022
1 parent f19d301 commit cf730d4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "guess-that-lang"
version = "1.0.14"
version = "1.0.15"
rust-version = "1.63"
authors = ["Lioness100 <jchickmm2@gmail.com>"]
edition = "2021"
Expand Down
10 changes: 5 additions & 5 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{
time::Duration,
};

use ansi_term::Color;
use crossterm::{
cursor::Show,
execute,
style::Stylize,
terminal::{disable_raw_mode, LeaveAlternateScreen},
};
use rand::{seq::SliceRandom, thread_rng};
Expand Down Expand Up @@ -69,17 +69,17 @@ impl Drop for Game {

println!(
"\nYou scored {} points!",
Color::Green.bold().paint(self.points.to_string())
self.points.to_string().green().bold()
);

if self.points > CONFIG.high_score {
if CONFIG.high_score > 0 {
println!(
"You beat your high score of {}!\n\nShare it: {}",
Color::Purple.bold().paint(CONFIG.high_score.to_string()),
Color::Cyan
CONFIG.high_score.to_string().magenta().bold(),
"https://github.com/Lioness100/guess-that-lang/discussions/6"
.cyan()
.bold()
.paint("https://github.com/Lioness100/guess-that-lang/discussions/6")
);
}

Expand Down
30 changes: 13 additions & 17 deletions src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crossterm::{
cursor::{Hide, MoveTo, MoveToColumn, MoveUp, RestorePosition, SavePosition},
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
execute, queue,
style::Print,
style::{Print, Stylize},
terminal::{self, enable_raw_mode, Clear, ClearType, EnterAlternateScreen},
};
use rand::{seq::SliceRandom, thread_rng};
Expand Down Expand Up @@ -241,15 +241,15 @@ impl Terminal {
width: &usize,
total_points: u32,
) {
let pipe = Color::White.dimmed().paint("│");
let pipe = "│".white().dim();

let points = format!(
"{padding}{pipe} {}{}\r\n{padding}{pipe} {}{}\r\n{padding}{pipe} {}{}",
Color::White.bold().paint("High Score: "),
Color::Purple.paint(CONFIG.high_score.to_string()),
Color::White.bold().paint("Total Points: "),
Color::Cyan.paint(total_points.to_string()),
Color::White.bold().paint("Available Points: "),
"High Score: ".bold(),
CONFIG.high_score.to_string().magenta(),
"Total Points: ".bold(),
total_points.to_string().cyan(),
"Available Points: ".bold(),
Color::RGB(0, 255, 0).paint("100"),
padding = " ".repeat(7),
);
Expand All @@ -258,10 +258,9 @@ impl Terminal {
let line_separator_end = "─".repeat(width - 8);

let [top, mid, bottom] = ["┬", "┼", "┴"].map(|char| {
Color::White
.dimmed()
.paint(line_separator_start.clone() + char + &line_separator_end)
.to_string()
(line_separator_start.clone() + char + &line_separator_end)
.white()
.dim()
});

let dotted_code = code_lines
Expand Down Expand Up @@ -403,10 +402,7 @@ impl Terminal {

let correct_option_text = Self::format_option(
&(correct_option_idx + 1).to_string(),
&Color::Green
.bold()
.paint(correct_option_name_text)
.to_string(),
&correct_option_name_text.green().bold().to_string(),
);

queue!(
Expand Down Expand Up @@ -468,8 +464,8 @@ impl Terminal {
format!(
"{padding}[{key}] {name}",
padding = " ".repeat(5),
key = Color::White.bold().paint(key),
name = Color::White.paint(name)
key = key.bold(),
name = name
)
}

Expand Down

0 comments on commit cf730d4

Please sign in to comment.