From cad96556adaad9623101405de329d64abd39e674 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 31 Mar 2021 16:50:57 +0530 Subject: initial support for rc.lisp --- src/app.rs | 27 +++++++++++++++++++++------ src/consts.rs | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 29a292f..1263324 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,7 @@ use crate::{ bitmap::{positive_angle_with_x, MapPoint, Pixmap}, brush::{Brush, CircleBrush, LineBrush}, command::CommandBox, - consts::{colors::*, FONT_PATH, STDLIB_PATH}, + consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, dither, error::AppError, lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, @@ -13,7 +13,12 @@ use crate::{ utils::{draw_text, handle_error, is_copy_event, is_paste_event, load_script}, }; -use std::{convert::From, fs::File, io::prelude::*, path::Path}; +use std::{ + convert::From, + fs::File, + io::prelude::*, + path::{Path, PathBuf}, +}; use obi::Image; use sdl2::{ @@ -34,7 +39,7 @@ pub enum Mode { Command, } -pub struct AppState<'ctx, 'file> { +pub struct AppState<'ctx> { pub active_color: bool, pub brush: Brush, pub canvas: Canvas, @@ -42,7 +47,7 @@ pub struct AppState<'ctx, 'file> { pub context: &'ctx Sdl, pub current_operation: Vec, pub dither_level: u8, - pub file_name: Option<&'file Path>, + pub file_name: Option, pub grid: Grid, pub lisp_env: EnvList, pub message: Message, @@ -71,7 +76,7 @@ impl Grid { } // private actions on appstate -impl<'ctx, 'file> AppState<'ctx, 'file> { +impl<'ctx> AppState<'ctx> { fn pan>(&mut self, direction: P) { self.start += direction.into(); } @@ -516,7 +521,7 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { context: &'ctx Sdl, ttf_context: &'ctx Sdl2TtfContext, start_data: Option>, - file_name: Option<&'file Path>, + file_name: Option, ) -> Result { let video_subsystem = context.video().map_err(AppError::Sdl)?; @@ -557,6 +562,9 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { zoom: 5, }; load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; + std::fs::create_dir_all(RC_PATH).map_err(AppError::File)?; + load_script([RC_PATH, "rc.lisp"].iter().collect::(), &mut app) + .map_err(AppError::Lisp)?; Ok(app) } @@ -566,6 +574,13 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { image } + pub fn save_as>(&self, file_name: P) -> Result<(), AppError> { + let image = self.export().encode().unwrap(); + let mut file = File::create(file_name).map_err(AppError::File)?; + file.write_all(&image[..]).unwrap(); + return Ok(()); + } + pub fn run(&mut self) { self.canvas.set_draw_color(BLACK); self.canvas.clear(); diff --git a/src/consts.rs b/src/consts.rs index 52ab580..3f9ecb9 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -10,3 +10,4 @@ pub mod colors { pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf"; pub const STDLIB_PATH: &'static str = "./src/lisp/std.lisp"; +pub const RC_PATH: &'static str = "/home/np/.config/sdl-tests"; -- cgit v1.2.3