Skip to content

Commit f8b5553

Browse files
committed
Use Crate for Color Assignment
1 parent cb8322b commit f8b5553

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

Cargo.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ serde_json = "1.0.71"
1212
serde = "1.0.130"
1313
ncurses = "5.101.0"
1414
http = "0.2.5"
15+
term = "*"
1516

1617
[dependencies.isahc]
1718
version = "1.6.0"

src/commands/list.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
use clap::ArgMatches;
22
use isahc::{HttpClient, ReadResponseExt, Request};
3-
3+
use std::io::prelude::*;
44
use serde_json::{Map, Value};
55

66
use crate::api::APIError;
77
use crate::api::API;
88

9-
static RST: &'static str = "\x1B[0m";
10-
static CYAN: &'static str = "\x1B[36m";
11-
static WHITE: &'static str = "\x1B[37m";
12-
static RED: &'static str = "\x1B[31m";
13-
149
pub fn list(matches: &ArgMatches<'_>, api: &mut API) -> Result<(), Box<dyn std::error::Error>> {
1510
let token = api.get_token()?;
1611

12+
let mut term = term::stdout().unwrap();
13+
1714
let client = HttpClient::new()?;
1815
let mut url = "https://drink.csh.rit.edu/drinks".to_string();
1916
if let Some(machine) = matches.value_of("machine") {
@@ -51,8 +48,9 @@ pub fn list(matches: &ArgMatches<'_>, api: &mut API) -> Result<(), Box<dyn std::
5148
None => return Err(Box::new(APIError::BadFormat)),
5249
};
5350
let subject_line = format!("{} ({})", display_name, name);
54-
println!("{}{}{}", &CYAN, &subject_line, &RST);
55-
println!("{}{}{}", &CYAN, "=".repeat(subject_line.len()), &RST);
51+
term.fg(term::color::CYAN).unwrap();
52+
writeln!(term, "{}", subject_line).unwrap();
53+
writeln!(term, "{}", "=".repeat(subject_line.len())).unwrap();
5654
let slots: &Vec<Value> = match machine["slots"].as_array() {
5755
Some(slots) => slots,
5856
None => return Err(Box::new(APIError::BadFormat)),
@@ -72,10 +70,13 @@ pub fn list(matches: &ArgMatches<'_>, api: &mut API) -> Result<(), Box<dyn std::
7270
let slot_number = slot["number"].as_u64().unwrap();
7371
let name = item["name"].as_str().unwrap();
7472
if slot["empty"].as_bool().unwrap() {
75-
print!("{}{}. {} ({} Credits) [EMPTY]{}", &RED, slot_number, name, price, &RST)
73+
term.fg(term::color::RED).unwrap();
74+
write!(term, "{}. {} ({} Credits) [EMPTY]", slot_number, name, price).unwrap();
7675
} else {
77-
print!("{}. {} ({} Credits)", slot_number, name, price)
76+
term.fg(term::color::WHITE).unwrap();
77+
write!(term, "{}. {} ({} Credits)", slot_number, name, price).unwrap();
7878
}
79+
term.reset().unwrap();
7980
println!("");
8081
}
8182
println!("");

0 commit comments

Comments
 (0)