From 5d541cb5c87ffb80262c753394f06f4449609b9b Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 31 Mar 2021 16:53:27 +0530 Subject: integrate pico args to main --- src/main.rs | 81 ++++++++++++++++++++++++++++++------------------------------ 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 @@ mod app; mod bitmap; mod brush; +mod cli; mod command; mod consts; mod dither; @@ -11,58 +12,58 @@ mod symmetry; mod undo; mod utils; -use app::AppState; -use error::{AppError, SdlTTFError}; - -use std::{ - env, - fs::OpenOptions, - io::{Cursor, Read}, - path::PathBuf, +use { + app::AppState, + cli::Config, + error::{AppError, SdlTTFError}, }; use log::{error, info}; -use obi::Image; pub fn error_sink() -> Result<(), AppError> { - env_logger::init(); + let init = || { + let sdl_context = sdl2::init().map_err(AppError::Sdl)?; + info!("Initialized SDL context"); - let sdl_context = sdl2::init().map_err(AppError::Sdl)?; - info!("Initialized SDL context"); + let ttf_context = sdl2::ttf::init() + .map_err(SdlTTFError::Init) + .map_err(AppError::SdlTTF)?; + info!("Initialized SDL_ttf context"); + Ok((sdl_context, ttf_context)) + }; - let ttf_context = sdl2::ttf::init().map_err(|e| AppError::SdlTTF(SdlTTFError::Init(e)))?; - info!("Initialized SDL_ttf context"); + match cli::parse_args().map_err(AppError::Cli)? { + Config::Help => { + println!("{}", cli::HELP_TEXT); + } - let args: Vec<_> = env::args().collect(); - if args.len() < 2 { - AppState::init(160, 160, &sdl_context, &ttf_context, None, None)?.run(); - return Ok(()); - } else { - let path = PathBuf::from(args.get(1).unwrap()); - let mut image = OpenOptions::new() - .read(true) - .write(false) - .create(false) - .open(&path) - .map_err(AppError::File)?; - let mut buf = Vec::new(); - image.read_to_end(&mut buf).map_err(AppError::File)?; - let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); // TODO: obi error - let (width, height) = (decoded.width(), decoded.height()); - AppState::init( - width, - height, - &sdl_context, - &ttf_context, - Some(decoded.data), - Some(&path), - )? - .run(); - return Ok(()); + Config::NewProject { + file_name, + dimensions: (width, height), + } => { + let (sdl_context, ttf_context) = init()?; + AppState::init(width, height, &sdl_context, &ttf_context, None, file_name)?.run(); + } + + Config::ExistingProject { file_name } => { + let (sdl_context, ttf_context) = init()?; + let image = utils::load_file(&file_name).map_err(AppError::File)?; + AppState::init( + image.width(), + image.height(), + &sdl_context, + &ttf_context, + Some(image.data), + Some(file_name), + )? + .run(); + } } + return Ok(()); } pub fn main() { + env_logger::init(); match error_sink() { Err(e) => error!("{}", e), _ => (), 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::{ lisp::{ error::{EvalError, LispError, ParseError}, eval::Evaluator, - expr::LispExpr, lex::Lexer, parse::Parser, }, message::Message, }; -use std::{fs::OpenOptions, io::Read, path::Path}; +use std::{ + fs::OpenOptions, + io::{self, Cursor, Read}, + path::Path, +}; +use log::info; +use obi::Image; use sdl2::{ keyboard::{Keycode, Mod}, pixels::Color, @@ -91,3 +96,15 @@ pub fn load_script>(path: P, app: &mut AppState) -> Result<(), Li } return Ok(()); } + +pub fn load_file>(path: P) -> Result { + info!("loading existing file `{:?}`", path.as_ref()); + let mut image = OpenOptions::new() + .read(true) + .write(false) + .create(false) + .open(&path)?; + let mut buf = Vec::new(); + image.read_to_end(&mut buf)?; + Ok(Image::decode(&mut (Cursor::new(buf))).unwrap()) // TODO: obi erro +} -- cgit v1.2.3