diff options
author | Akshay <[email protected]> | 2021-03-31 12:23:27 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-31 12:23:27 +0100 |
commit | 5d541cb5c87ffb80262c753394f06f4449609b9b (patch) | |
tree | 1c4f791deebed8b75e76b039adde88b48b6fdeae | |
parent | 92b532e70f7650d7a5fc942cc3a56bc838e57f8c (diff) |
integrate pico args to main
-rw-r--r-- | src/main.rs | 81 | ||||
-rw-r--r-- | src/utils.rs | 21 |
2 files changed, 60 insertions, 42 deletions
diff --git a/src/main.rs b/src/main.rs index 1b6e93a..9c186c9 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | mod app; | 1 | mod app; |
2 | mod bitmap; | 2 | mod bitmap; |
3 | mod brush; | 3 | mod brush; |
4 | mod cli; | ||
4 | mod command; | 5 | mod command; |
5 | mod consts; | 6 | mod consts; |
6 | mod dither; | 7 | mod dither; |
@@ -11,58 +12,58 @@ mod symmetry; | |||
11 | mod undo; | 12 | mod undo; |
12 | mod utils; | 13 | mod utils; |
13 | 14 | ||
14 | use app::AppState; | 15 | use { |
15 | use error::{AppError, SdlTTFError}; | 16 | app::AppState, |
16 | 17 | cli::Config, | |
17 | use std::{ | 18 | error::{AppError, SdlTTFError}, |
18 | env, | ||
19 | fs::OpenOptions, | ||
20 | io::{Cursor, Read}, | ||
21 | path::PathBuf, | ||
22 | }; | 19 | }; |
23 | 20 | ||
24 | use log::{error, info}; | 21 | use log::{error, info}; |
25 | use obi::Image; | ||
26 | 22 | ||
27 | pub fn error_sink() -> Result<(), AppError> { | 23 | pub fn error_sink() -> Result<(), AppError> { |
28 | env_logger::init(); | 24 | let init = || { |
25 | let sdl_context = sdl2::init().map_err(AppError::Sdl)?; | ||
26 | info!("Initialized SDL context"); | ||
29 | 27 | ||
30 | let sdl_context = sdl2::init().map_err(AppError::Sdl)?; | 28 | let ttf_context = sdl2::ttf::init() |
31 | info!("Initialized SDL context"); | 29 | .map_err(SdlTTFError::Init) |
30 | .map_err(AppError::SdlTTF)?; | ||
31 | info!("Initialized SDL_ttf context"); | ||
32 | Ok((sdl_context, ttf_context)) | ||
33 | }; | ||
32 | 34 | ||
33 | let ttf_context = sdl2::ttf::init().map_err(|e| AppError::SdlTTF(SdlTTFError::Init(e)))?; | 35 | match cli::parse_args().map_err(AppError::Cli)? { |
34 | info!("Initialized SDL_ttf context"); | 36 | Config::Help => { |
37 | println!("{}", cli::HELP_TEXT); | ||
38 | } | ||
35 | 39 | ||
36 | let args: Vec<_> = env::args().collect(); | 40 | Config::NewProject { |
37 | if args.len() < 2 { | 41 | file_name, |
38 | AppState::init(160, 160, &sdl_context, &ttf_context, None, None)?.run(); | 42 | dimensions: (width, height), |
39 | return Ok(()); | 43 | } => { |
40 | } else { | 44 | let (sdl_context, ttf_context) = init()?; |
41 | let path = PathBuf::from(args.get(1).unwrap()); | 45 | AppState::init(width, height, &sdl_context, &ttf_context, None, file_name)?.run(); |
42 | let mut image = OpenOptions::new() | 46 | } |
43 | .read(true) | 47 | |
44 | .write(false) | 48 | Config::ExistingProject { file_name } => { |
45 | .create(false) | 49 | let (sdl_context, ttf_context) = init()?; |
46 | .open(&path) | 50 | let image = utils::load_file(&file_name).map_err(AppError::File)?; |
47 | .map_err(AppError::File)?; | 51 | AppState::init( |
48 | let mut buf = Vec::new(); | 52 | image.width(), |
49 | image.read_to_end(&mut buf).map_err(AppError::File)?; | 53 | image.height(), |
50 | let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); // TODO: obi error | 54 | &sdl_context, |
51 | let (width, height) = (decoded.width(), decoded.height()); | 55 | &ttf_context, |
52 | AppState::init( | 56 | Some(image.data), |
53 | width, | 57 | Some(file_name), |
54 | height, | 58 | )? |
55 | &sdl_context, | 59 | .run(); |
56 | &ttf_context, | 60 | } |
57 | Some(decoded.data), | ||
58 | Some(&path), | ||
59 | )? | ||
60 | .run(); | ||
61 | return Ok(()); | ||
62 | } | 61 | } |
62 | return Ok(()); | ||
63 | } | 63 | } |
64 | 64 | ||
65 | pub fn main() { | 65 | pub fn main() { |
66 | env_logger::init(); | ||
66 | match error_sink() { | 67 | match error_sink() { |
67 | Err(e) => error!("{}", e), | 68 | Err(e) => error!("{}", e), |
68 | _ => (), | 69 | _ => (), |
diff --git a/src/utils.rs b/src/utils.rs index dcf652d..9859f39 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -4,15 +4,20 @@ use crate::{ | |||
4 | lisp::{ | 4 | lisp::{ |
5 | error::{EvalError, LispError, ParseError}, | 5 | error::{EvalError, LispError, ParseError}, |
6 | eval::Evaluator, | 6 | eval::Evaluator, |
7 | expr::LispExpr, | ||
8 | lex::Lexer, | 7 | lex::Lexer, |
9 | parse::Parser, | 8 | parse::Parser, |
10 | }, | 9 | }, |
11 | message::Message, | 10 | message::Message, |
12 | }; | 11 | }; |
13 | 12 | ||
14 | use std::{fs::OpenOptions, io::Read, path::Path}; | 13 | use std::{ |
14 | fs::OpenOptions, | ||
15 | io::{self, Cursor, Read}, | ||
16 | path::Path, | ||
17 | }; | ||
15 | 18 | ||
19 | use log::info; | ||
20 | use obi::Image; | ||
16 | use sdl2::{ | 21 | use sdl2::{ |
17 | keyboard::{Keycode, Mod}, | 22 | keyboard::{Keycode, Mod}, |
18 | pixels::Color, | 23 | pixels::Color, |
@@ -91,3 +96,15 @@ pub fn load_script<P: AsRef<Path>>(path: P, app: &mut AppState) -> Result<(), Li | |||
91 | } | 96 | } |
92 | return Ok(()); | 97 | return Ok(()); |
93 | } | 98 | } |
99 | |||
100 | pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Image, io::Error> { | ||
101 | info!("loading existing file `{:?}`", path.as_ref()); | ||
102 | let mut image = OpenOptions::new() | ||
103 | .read(true) | ||
104 | .write(false) | ||
105 | .create(false) | ||
106 | .open(&path)?; | ||
107 | let mut buf = Vec::new(); | ||
108 | image.read_to_end(&mut buf)?; | ||
109 | Ok(Image::decode(&mut (Cursor::new(buf))).unwrap()) // TODO: obi erro | ||
110 | } | ||