aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-31 12:23:27 +0100
committerAkshay <[email protected]>2021-03-31 12:23:27 +0100
commit5d541cb5c87ffb80262c753394f06f4449609b9b (patch)
tree1c4f791deebed8b75e76b039adde88b48b6fdeae
parent92b532e70f7650d7a5fc942cc3a56bc838e57f8c (diff)
integrate pico args to main
-rw-r--r--src/main.rs81
-rw-r--r--src/utils.rs21
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 @@
1mod app; 1mod app;
2mod bitmap; 2mod bitmap;
3mod brush; 3mod brush;
4mod cli;
4mod command; 5mod command;
5mod consts; 6mod consts;
6mod dither; 7mod dither;
@@ -11,58 +12,58 @@ mod symmetry;
11mod undo; 12mod undo;
12mod utils; 13mod utils;
13 14
14use app::AppState; 15use {
15use error::{AppError, SdlTTFError}; 16 app::AppState,
16 17 cli::Config,
17use std::{ 18 error::{AppError, SdlTTFError},
18 env,
19 fs::OpenOptions,
20 io::{Cursor, Read},
21 path::PathBuf,
22}; 19};
23 20
24use log::{error, info}; 21use log::{error, info};
25use obi::Image;
26 22
27pub fn error_sink() -> Result<(), AppError> { 23pub 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
65pub fn main() { 65pub 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
14use std::{fs::OpenOptions, io::Read, path::Path}; 13use std::{
14 fs::OpenOptions,
15 io::{self, Cursor, Read},
16 path::Path,
17};
15 18
19use log::info;
20use obi::Image;
16use sdl2::{ 21use 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
100pub 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}