From 3fff29b73c0402f6cd82c47990a372fcf68b44c3 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 25 Mar 2021 13:09:05 +0530 Subject: impl message colors; reading file path on load --- src/main.rs | 9 ++++++--- src/message.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) (limited to 'src') 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::{ env, fs::OpenOptions, io::{Cursor, Read}, + path::{Path, PathBuf}, }; use log::{error, info}; @@ -41,15 +42,15 @@ pub fn main() { let args: Vec<_> = env::args().collect(); if args.len() < 2 { - AppState::init(160, 160, &sdl_context, &ttf_context, None).run(); + AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run(); return; } else { - let path = args.get(1).unwrap(); + let path = PathBuf::from(args.get(1).unwrap()); let image_src = OpenOptions::new() .read(true) .write(false) .create(false) - .open(path); + .open(&path); if let Ok(mut image) = image_src { let mut buf = Vec::new(); image.read_to_end(&mut buf).unwrap(); @@ -61,8 +62,10 @@ pub fn main() { &sdl_context, &ttf_context, Some(decoded.data), + Some(&path), ) .run(); + } else { } } } 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 @@ +use crate::consts::colors::*; + +use sdl2::pixels::Color; + #[derive(Debug)] pub struct Message { pub text: String, @@ -11,14 +15,36 @@ impl Message { kind: MessageKind::Info, } } + pub fn kind(mut self, kind: MessageKind) -> Self { self.kind = kind; self } + pub fn text>(mut self, text: S) -> Self { self.text = text.as_ref().into(); self } + + pub fn set_info>(&mut self, text: S) { + self.text = text.as_ref().into(); + self.kind = MessageKind::Info; + } + + pub fn set_error>(&mut self, text: S) { + self.text = text.as_ref().into(); + self.kind = MessageKind::Error; + } + + pub fn set_hint>(&mut self, text: S) { + self.text = text.as_ref().into(); + self.kind = MessageKind::Hint; + } + + pub fn clear(&mut self) { + self.text.clear(); + self.kind = MessageKind::Info; + } } #[derive(Debug, Copy, Clone)] @@ -40,3 +66,14 @@ where }; } } + +impl Into for MessageKind { + fn into(self) -> Color { + match self { + Self::Error => PINK, + Self::Info => WHITE, + Self::Hint => CYAN, + Self::LispResult => GREY, + } + } +} -- cgit v1.2.3