diff options
author | Akshay <[email protected]> | 2021-03-25 07:39:05 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-25 07:39:05 +0000 |
commit | 3fff29b73c0402f6cd82c47990a372fcf68b44c3 (patch) | |
tree | 557e7648a8de688a8e9eba4a5b5c4e05df3ab289 /src | |
parent | 8171b30adbc4cddd2c51f043c3379d78428666b8 (diff) |
impl message colors; reading file path on load
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 9 | ||||
-rw-r--r-- | src/message.rs | 37 |
2 files changed, 43 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs index 1f5dc8a..9309f63 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -15,6 +15,7 @@ use std::{ | |||
15 | env, | 15 | env, |
16 | fs::OpenOptions, | 16 | fs::OpenOptions, |
17 | io::{Cursor, Read}, | 17 | io::{Cursor, Read}, |
18 | path::{Path, PathBuf}, | ||
18 | }; | 19 | }; |
19 | 20 | ||
20 | use log::{error, info}; | 21 | use log::{error, info}; |
@@ -41,15 +42,15 @@ pub fn main() { | |||
41 | 42 | ||
42 | let args: Vec<_> = env::args().collect(); | 43 | let args: Vec<_> = env::args().collect(); |
43 | if args.len() < 2 { | 44 | if args.len() < 2 { |
44 | AppState::init(160, 160, &sdl_context, &ttf_context, None).run(); | 45 | AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run(); |
45 | return; | 46 | return; |
46 | } else { | 47 | } else { |
47 | let path = args.get(1).unwrap(); | 48 | let path = PathBuf::from(args.get(1).unwrap()); |
48 | let image_src = OpenOptions::new() | 49 | let image_src = OpenOptions::new() |
49 | .read(true) | 50 | .read(true) |
50 | .write(false) | 51 | .write(false) |
51 | .create(false) | 52 | .create(false) |
52 | .open(path); | 53 | .open(&path); |
53 | if let Ok(mut image) = image_src { | 54 | if let Ok(mut image) = image_src { |
54 | let mut buf = Vec::new(); | 55 | let mut buf = Vec::new(); |
55 | image.read_to_end(&mut buf).unwrap(); | 56 | image.read_to_end(&mut buf).unwrap(); |
@@ -61,8 +62,10 @@ pub fn main() { | |||
61 | &sdl_context, | 62 | &sdl_context, |
62 | &ttf_context, | 63 | &ttf_context, |
63 | Some(decoded.data), | 64 | Some(decoded.data), |
65 | Some(&path), | ||
64 | ) | 66 | ) |
65 | .run(); | 67 | .run(); |
68 | } else { | ||
66 | } | 69 | } |
67 | } | 70 | } |
68 | } | 71 | } |
diff --git a/src/message.rs b/src/message.rs index d1032cf..958dd68 100644 --- a/src/message.rs +++ b/src/message.rs | |||
@@ -1,3 +1,7 @@ | |||
1 | use crate::consts::colors::*; | ||
2 | |||
3 | use sdl2::pixels::Color; | ||
4 | |||
1 | #[derive(Debug)] | 5 | #[derive(Debug)] |
2 | pub struct Message { | 6 | pub struct Message { |
3 | pub text: String, | 7 | pub text: String, |
@@ -11,14 +15,36 @@ impl Message { | |||
11 | kind: MessageKind::Info, | 15 | kind: MessageKind::Info, |
12 | } | 16 | } |
13 | } | 17 | } |
18 | |||
14 | pub fn kind(mut self, kind: MessageKind) -> Self { | 19 | pub fn kind(mut self, kind: MessageKind) -> Self { |
15 | self.kind = kind; | 20 | self.kind = kind; |
16 | self | 21 | self |
17 | } | 22 | } |
23 | |||
18 | pub fn text<S: AsRef<str>>(mut self, text: S) -> Self { | 24 | pub fn text<S: AsRef<str>>(mut self, text: S) -> Self { |
19 | self.text = text.as_ref().into(); | 25 | self.text = text.as_ref().into(); |
20 | self | 26 | self |
21 | } | 27 | } |
28 | |||
29 | pub fn set_info<S: AsRef<str>>(&mut self, text: S) { | ||
30 | self.text = text.as_ref().into(); | ||
31 | self.kind = MessageKind::Info; | ||
32 | } | ||
33 | |||
34 | pub fn set_error<S: AsRef<str>>(&mut self, text: S) { | ||
35 | self.text = text.as_ref().into(); | ||
36 | self.kind = MessageKind::Error; | ||
37 | } | ||
38 | |||
39 | pub fn set_hint<S: AsRef<str>>(&mut self, text: S) { | ||
40 | self.text = text.as_ref().into(); | ||
41 | self.kind = MessageKind::Hint; | ||
42 | } | ||
43 | |||
44 | pub fn clear(&mut self) { | ||
45 | self.text.clear(); | ||
46 | self.kind = MessageKind::Info; | ||
47 | } | ||
22 | } | 48 | } |
23 | 49 | ||
24 | #[derive(Debug, Copy, Clone)] | 50 | #[derive(Debug, Copy, Clone)] |
@@ -40,3 +66,14 @@ where | |||
40 | }; | 66 | }; |
41 | } | 67 | } |
42 | } | 68 | } |
69 | |||
70 | impl Into<Color> for MessageKind { | ||
71 | fn into(self) -> Color { | ||
72 | match self { | ||
73 | Self::Error => PINK, | ||
74 | Self::Info => WHITE, | ||
75 | Self::Hint => CYAN, | ||
76 | Self::LispResult => GREY, | ||
77 | } | ||
78 | } | ||
79 | } | ||