1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use colored::Colorize;
use serde::Serialize;
use super::schemas::problems;
#[derive(AsChangeset, Clone, Identifiable, Insertable, Queryable, Serialize)]
#[table_name = "problems"]
pub struct Problem {
pub category: String,
pub fid: i32,
pub id: i32,
pub level: i32,
pub locked: bool,
pub name: String,
pub percent: f32,
pub slug: String,
pub starred: bool,
pub state: String,
}
static DONE: &'static str = " ✔";
static ETC: &'static str = "...";
static LOCK: &'static str = "🔒";
static NDONE: &'static str = "✘";
static SPACE: &'static str = " ";
impl std::fmt::Display for Problem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let space_2 = SPACE.repeat(2);
let mut lock = space_2.as_str();
let mut done = space_2.normal();
let mut id = "".to_string();
let mut name = "".to_string();
let mut level = "".normal();
if self.locked { lock = LOCK };
if self.state == "ac".to_string() {
done = DONE.green().bold();
} else if self.state == "notac" {
done = NDONE.green().bold();
}
match self.id.to_string().len() {
1 => {
id.push_str(&SPACE.repeat(2));
id.push_str(&self.id.to_string());
id.push_str(&SPACE.repeat(1));
},
2 => {
id.push_str(&SPACE.repeat(1));
id.push_str(&self.id.to_string());
id.push_str(&SPACE.repeat(1));
},
3 => {
id.push_str(&SPACE.repeat(1));
id.push_str(&self.id.to_string());
},
4 => {
id.push_str(&self.id.to_string());
},
_ => {
id.push_str(&space_2);
id.push_str(&space_2);
}
}
if &self.name.len() < &60_usize {
name.push_str(&self.name);
name.push_str(&SPACE.repeat(60 - &self.name.len()));
} else {
name.push_str(&self.name[..49]);
name = name.trim_end().to_string();
name.push_str(ETC);
name.push_str(&SPACE.repeat(60 - name.len()));
}
level = match self.level {
1 => "Easy ".bright_green(),
2 => "Medium".bright_yellow(),
3 => "Hard ".bright_red(),
_ => level
};
write!(
f,
" {} {} [{}] {} {} ({})",
lock, done, id, name, level,
&self.percent.to_string()[0..5]
)
}
}