From 589649298c1925fbf1cd1ac2e7bfac03dcbc0da3 Mon Sep 17 00:00:00 2001 From: able Date: Sat, 16 Sep 2023 17:30:39 -0500 Subject: [PATCH] Adding in and renaming stuff and such --- README.md | 11 +++++++++++ assets/config.rhai | 18 ++++++++++++----- src/document.rs | 21 ++++++++++++-------- src/editor.rs | 48 +++++++++++++++++++++++++++++++++++----------- 4 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6f3f12b --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# adit + + +## TODO + + +- Custom key binds +- Custom syntax highlighting + - + + diff --git a/assets/config.rhai b/assets/config.rhai index 8a20581..dba4040 100644 --- a/assets/config.rhai +++ b/assets/config.rhai @@ -1,5 +1,5 @@ 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`; return sb; } @@ -9,11 +9,19 @@ fn welcome_message(){ return wm; } -fn on_start(){ - +fn presave() { + // Make this more usable later + + + return ""; } -fn filetype(file){ +fn on_start(){} + +fn on_key(){} + + +fn filetype(file) { switch file { "rs" => { let name = "Rust"; @@ -26,4 +34,4 @@ fn filetype(file){ return (name); } } -} \ No newline at end of file +} diff --git a/src/document.rs b/src/document.rs index 87d12a9..76e9a3b 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,9 +1,13 @@ -use crate::FileType; -use crate::Position; -use crate::Row; -use crate::SearchDirection; -use std::fs; -use std::io::{Error, Write}; +use crate::{ + FileType, + Position, + Row, + SearchDirection +}; +use std::{ + fs, + io::{Error, Write}, +}; #[derive(Default)] pub struct Document { @@ -60,6 +64,7 @@ impl Document { return; } self.dirty = true; + if c == '\n' { self.insert_newline(at); } else if at.y == self.rows.len() { @@ -98,7 +103,7 @@ impl Document { self.unhighlight_rows(at.y); } pub fn save(&mut self) -> Result<(), Error> { - if let Some(file_name) = &self.file_name { + if let Some(file_name) = &self.file_name { let mut file = fs::File::create(file_name)?; self.file_type = FileType::from(file_name); for row in &mut self.rows { @@ -110,7 +115,7 @@ impl Document { Ok(()) } pub fn is_dirty(&self) -> bool { - self.dirty + self.dirty } #[allow(clippy::indexing_slicing)] pub fn find(&self, query: &str, at: &Position, direction: SearchDirection) -> Option { diff --git a/src/editor.rs b/src/editor.rs index a9dac98..e31391b 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,12 +1,12 @@ -use crate::config::Config; -use crate::config::Theme; -use crate::Document; -use crate::Row; -use crate::Terminal; -use core::time::Duration; -use std::env; -use std::time::Instant; -use termion::event::Key; +use { + crate::{ + config::{Config, Theme}, + Document, Row, Terminal, + }, + core::time::Duration, + std::{env, time::Instant}, + termion::event::Key, +}; const VERSION: &str = env!("CARGO_PKG_VERSION"); const QUIT_TIMES: u8 = 2; @@ -64,6 +64,7 @@ impl Editor<'static> { } } } + pub fn default() -> Self { let args: Vec = env::args().collect(); let mut initial_status = @@ -155,7 +156,8 @@ impl Editor<'static> { } self.document.file_name = new_name; } - + // TODO + self.config.call("presave"); if self.document.save().is_ok() { self.status_message = StatusMessage::from("File saved successfully.".to_string()); } else { @@ -202,6 +204,7 @@ impl Editor<'static> { } fn process_keypress(&mut self) -> Result<(), std::io::Error> { let pressed_key = Terminal::read_key()?; + // TODO: break this out into the config match pressed_key { Key::Ctrl('q') => { if self.quit_times > 0 && self.document.is_dirty() { @@ -225,6 +228,29 @@ impl Editor<'static> { } } + Key::Char('(') => { + self.document.insert(&self.cursor_position, ')'); + self.document.insert(&self.cursor_position, '('); + self.move_cursor(Key::Right); + } + + Key::Char('[') => { + self.document.insert(&self.cursor_position, ']'); + self.document.insert(&self.cursor_position, '['); + self.move_cursor(Key::Right); + } + + Key::Char('{') => { + self.document.insert(&self.cursor_position, '}'); + self.document.insert(&self.cursor_position, '{'); + self.move_cursor(Key::Right); + } + Key::Char('"') => { + self.document.insert(&self.cursor_position, '"'); + self.document.insert(&self.cursor_position, '"'); + self.move_cursor(Key::Right); + } + Key::Char(c) => { self.document.insert(&self.cursor_position, c); self.move_cursor(Key::Right); @@ -408,7 +434,7 @@ impl Editor<'static> { #[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();