diff --git a/Cargo.lock b/Cargo.lock index bf33089..fd6cdda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,13 +4,11 @@ version = 3 [[package]] name = "adit" -version = "0.1.1" +version = "0.2.0" dependencies = [ - "dirs", "rhai", "serde", "termion", - "toml", "unicode-segmentation", ] @@ -33,12 +31,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.4.0" @@ -79,26 +71,6 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" -[[package]] -name = "dirs" -version = "4.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" -dependencies = [ - "dirs-sys", -] - -[[package]] -name = "dirs-sys" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" -dependencies = [ - "libc", - "redox_users", - "winapi", -] - [[package]] name = "getrandom" version = "0.2.10" @@ -176,32 +148,13 @@ version = "0.1.56" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" -[[package]] -name = "redox_syscall" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "redox_termios" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" dependencies = [ - "redox_syscall 0.1.56", -] - -[[package]] -name = "redox_users" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" -dependencies = [ - "getrandom", - "redox_syscall 0.2.10", + "redox_syscall", ] [[package]] @@ -211,7 +164,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "637a4f79f65571b1fd1a0ebbae05bbbf58a01faf612abbc3eea15cda34f0b87a" dependencies = [ "ahash", - "bitflags 2.4.0", + "bitflags", "instant", "num-traits", "once_cell", @@ -304,7 +257,7 @@ checksum = "6a8fb22f7cde82c8220e5aeacb3258ed7ce996142c77cba193f203515e26c330" dependencies = [ "libc", "numtoa", - "redox_syscall 0.1.56", + "redox_syscall", "redox_termios", ] @@ -317,15 +270,6 @@ dependencies = [ "crunchy", ] -[[package]] -name = "toml" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" -dependencies = [ - "serde", -] - [[package]] name = "unicode-ident" version = "1.0.12" @@ -355,25 +299,3 @@ name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 457a115..55ea904 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "adit" -version = "0.1.1" +version = "0.2.0" authors = ["able"] edition = "2018" @@ -9,8 +9,6 @@ edition = "2018" [dependencies] termion = "1" unicode-segmentation = "1" -toml = "0.5.8" -dirs = "*" rhai = "1.16.1" [dependencies.serde] diff --git a/assets/config.rhai b/assets/config.rhai index 3f3c829..8a20581 100644 --- a/assets/config.rhai +++ b/assets/config.rhai @@ -4,3 +4,26 @@ fn status_bar() { return sb; } +fn welcome_message(){ + let wm = `Adit | version ${adit_version}`; + return wm; +} + +fn on_start(){ + +} + +fn filetype(file){ + switch file { + "rs" => { + let name = "Rust"; + + return (name); + } + _ => { + let name = "unknown"; + + return (name); + } +} +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 7caa2ff..351dba2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,9 +1,6 @@ -use std::path::PathBuf; -use std::str::FromStr; -use std::{fs::File, io::Read}; - -use crate::Editor; +use core::str::FromStr; use serde::Deserialize; +use std::path::PathBuf; use termion::color::Rgb; use rhai::Dynamic; @@ -35,9 +32,13 @@ impl Config<'static> { } pub fn call(&mut self, fn_name: &str) -> String { - self.eng + match self + .eng .call_fn::(&mut self.scope, &self.ast, fn_name, ()) - .unwrap() + { + Ok(string) => string, + Err(err) => panic!("{}", err), + } } } #[derive(Deserialize)] @@ -62,35 +63,4 @@ impl Theme { status_bg_color: RGB(239, 239, 239), } } - - pub fn read_config() -> ThemeReturn { - match dirs::home_dir() { - Some(pathbuf) => { - let mut file = File::open(pathbuf); - match file { - Ok(mut file_handle) => { - let mut contents = String::new(); - match file_handle.read_to_string(&mut contents) { - Ok(ma) => { - let config: Theme = toml::from_str(&contents).unwrap(); - - return ThemeReturn::Theme(config); - } - - Err(_) => todo!(), - } - } - Err(_) => todo!(), - } - } - None => ThemeReturn::NoHomeDir, - } - } -} - -pub enum ThemeReturn { - Theme(Theme), - NoHomeDir, - - GenericError, } diff --git a/src/document.rs b/src/document.rs index ba7c027..87d12a9 100644 --- a/src/document.rs +++ b/src/document.rs @@ -16,6 +16,7 @@ pub struct Document { impl Document { pub fn open(filename: &str) -> Result { let contents = fs::read_to_string(filename)?; + // TODO: Call filetype in config.rhai let file_type = FileType::from(filename); let mut rows = Vec::new(); for value in contents.lines() { diff --git a/src/editor.rs b/src/editor.rs index 07ec123..a9dac98 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -3,8 +3,8 @@ use crate::config::Theme; use crate::Document; use crate::Row; use crate::Terminal; +use core::time::Duration; use std::env; -use std::time::Duration; use std::time::Instant; use termion::event::Key; @@ -12,12 +12,14 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); const QUIT_TIMES: u8 = 2; #[derive(PartialEq, Copy, Clone)] +#[non_exhaustive] pub enum SearchDirection { Forward, Backward, } #[derive(Default, Clone)] +#[non_exhaustive] pub struct Position { pub x: usize, pub y: usize, @@ -99,6 +101,8 @@ impl Editor<'static> { .scope .set_or_push("line_count", self.document.len() as u64); + self.config.scope.set_or_push("adit_version", VERSION); + self.config .scope .set_or_push("file_type", self.document.file_type()); @@ -109,6 +113,10 @@ impl Editor<'static> { self.config .scope .set_or_push("y", self.cursor_position.y as u64); + + self.config + .scope + .set_or_push("quit_times", self.quit_times as u64); } fn refresh_screen(&mut self) -> Result<(), std::io::Error> { @@ -326,26 +334,26 @@ impl Editor<'static> { self.cursor_position = Position { x, y } } - fn draw_welcome_message(&self) { - let mut welcome_message = format!("Able editor -- version {}", VERSION); + fn draw_welcome_message(&mut self) { + let mut welcome_message = self.config.call("welcome_message"); let width = self.terminal.size().width as usize; let len = welcome_message.len(); - #[allow(clippy::integer_arithmetic, clippy::integer_division)] + #[allow(clippy::arithmetic_side_effects, clippy::integer_division)] let padding = width.saturating_sub(len) / 2; let spaces = " ".repeat(padding.saturating_sub(1)); - welcome_message = format!("▻{}{}", spaces, welcome_message); + welcome_message = format!("▻{spaces}{welcome_message}"); welcome_message.truncate(width); - println!("{}\r", welcome_message); + println!("{welcome_message}\r"); } pub fn draw_row(&self, row: &Row) { let width = self.terminal.size().width as usize; let start = self.offset.x; let end = self.offset.x.saturating_add(width); let row = row.render(start, end); - println!("{}\r", row) + println!("{row}\r"); } - #[allow(clippy::integer_division, clippy::integer_arithmetic)] - fn draw_rows(&self) { + #[allow(clippy::integer_division, clippy::arithmetic_side_effects)] + fn draw_rows(&mut self) { let height = self.terminal.size().height; for terminal_row in 0..height { Terminal::clear_current_line(); @@ -392,22 +400,22 @@ impl Editor<'static> { self.document .file_name .clone() - .unwrap_or("buffer".to_string()), + .unwrap_or("buffer".to_owned()), ); status = self.config.call("status_bar"); - #[allow(clippy::integer_arithmetic)] + #[allow(clippy::arithmetic_side_effects)] let len = status.len(); status.push_str(&" ".repeat(width.saturating_sub(len))); - status = format!(" {}", status); + status = format!(" {status}"); status.truncate(width); let theme = Theme::default(); Terminal::set_bg_color(theme.status_bg_color.to_term_color()); Terminal::set_fg_color(theme.status_fg_color.to_term_color()); - println!("{}\r", status); + println!("{status}\r"); Terminal::reset_fg_color(); Terminal::reset_bg_color(); }