From c3498ef7e2c5f5f1dfafb07e50370c4f62f4d69e Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 29 Mar 2021 12:13:33 +0530 Subject: fix: sdl panics on empty string drawing --- src/utils.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs index 902f939..3e5ff55 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use crate::consts::FONT_PATH; +use crate::{consts::FONT_PATH, lisp::error::LispError, message::Message}; use sdl2::{ keyboard::{Keycode, Mod}, pixels::Color, @@ -26,7 +26,10 @@ pub fn draw_text>( let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); font.set_style(sdl2::ttf::FontStyle::NORMAL); font.set_hinting(sdl2::ttf::Hinting::Mono); - let surface = font.render(text.as_ref()).blended(color).unwrap(); + let surface = font + .render(if text.is_empty() { " " } else { text.as_ref() }) + .blended(color) + .unwrap(); let texture = texture_creator .create_texture_from_surface(&surface) .unwrap(); @@ -43,3 +46,12 @@ pub fn is_copy_event(keycode: Option, keymod: Mod) -> bool { pub fn is_paste_event(keycode: Option, keymod: Mod) -> bool { keycode == Some(Keycode::V) && (keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD) } + +pub fn handle_error(err: LispError, src: &str) -> Message { + let mut message = Message::new(); + match err { + LispError::Parse(p) => message.set_error(p.display(&src)), + eval_err => message.set_error(eval_err.to_string()), + } + message +} -- cgit v1.2.3