From 8a38a7f1c2ac979460a238c39d3ebd59758f18fe Mon Sep 17 00:00:00 2001 From: Able Date: Wed, 20 Nov 2024 08:49:50 -0600 Subject: [PATCH] Minor change adding a spaces_per_tab function --- assets/config.rhai | 6 +++++- src/config.rs | 21 +++++++++++---------- src/editor.rs | 13 ++++++++----- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/assets/config.rhai b/assets/config.rhai index dba4040..7bfd5e3 100644 --- a/assets/config.rhai +++ b/assets/config.rhai @@ -1,5 +1,9 @@ +fn spaces_per_tab(){ + return 1; +} + fn status_bar() { - let sb = ` ${file_name}:${y}:${x} | ${file_type} | ${line_count} Lines`; + let sb = ` ${file_name}:${y}:${x} | ${file_type} | ${line_count} Lines | HELLO`; return sb; } diff --git a/src/config.rs b/src/config.rs index a356d09..e52527f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -9,34 +9,35 @@ use rhai::Scope; use rhai::AST; pub struct Config<'a> { - cfg: String, - eng: Engine, + pub cfg: String, + pub engine: Engine, pub scope: Scope<'a>, - ast: AST, + pub ast: AST, } impl Config<'static> { pub fn new() -> Self { let mut engine = Engine::new(); let mut scope = Scope::new(); let ast = engine - .compile_file(PathBuf::from_str("/home/able/Projects/adit/assets/config.rhai").unwrap()) + .compile_file(PathBuf::from_str("/home/able/adit/assets/config.rhai").unwrap()) .unwrap(); + let spaces_per_tab = engine.call_fn::(&mut scope, &ast, "start", ()); let result = engine.call_fn::(&mut scope, &ast, "status_bar", ()); Self { cfg: String::new(), - eng: Engine::new(), - scope: Scope::new(), + engine: engine, + scope, ast, } } - pub fn call(&mut self, fn_name: &str) -> String { + pub fn call(&mut self, fn_name: &str) -> Dynamic { match self - .eng - .call_fn::(&mut self.scope, &self.ast, fn_name, ()) + .engine + .call_fn::(&mut self.scope, &self.ast, fn_name, ()) { - Ok(string) => string, + Ok(ret) => ret, Err(err) => panic!("{}", err), } } diff --git a/src/editor.rs b/src/editor.rs index e31391b..4f5075d 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -82,7 +82,7 @@ impl Editor<'static> { Document::default() }; - let mut cfg = Config::new(); +// let mut cfg = Config::new(); Self { should_quit: false, @@ -222,8 +222,11 @@ impl Editor<'static> { // NOTE: I am more of the opinion that adit should use hard tab. Key::Char('\t') => { - for c in " ".chars() { - self.document.insert(&self.cursor_position, c); + let spaces = self.config.engine.call_fn::(&mut self.config.scope, &self.config.ast, "spaces_per_tab", () ).unwrap_or(2); + + + for c in 0..spaces { + self.document.insert(&self.cursor_position, ' '); self.move_cursor(Key::Right); } } @@ -361,7 +364,7 @@ impl Editor<'static> { self.cursor_position = Position { x, y } } fn draw_welcome_message(&mut self) { - let mut welcome_message = self.config.call("welcome_message"); + let mut welcome_message:String = self.config.call("welcome_message").to_string(); let width = self.terminal.size().width as usize; let len = welcome_message.len(); #[allow(clippy::arithmetic_side_effects, clippy::integer_division)] @@ -429,7 +432,7 @@ impl Editor<'static> { .unwrap_or("buffer".to_owned()), ); - status = self.config.call("status_bar"); + status = self.config.call("status_bar").to_string(); #[allow(clippy::arithmetic_side_effects)] let len = status.len();