diff --git a/.todo.md b/.todo.md index 6ae6d03..4f4896c 100644 --- a/.todo.md +++ b/.todo.md @@ -1,9 +1,11 @@ - [ ] 0.3.2 - - [ ] Better CLI - - [ ] Allow providing syntax highlighting option - - [X] Allow providing config file -- [ ] 0.3.3 - - [ ] Help page inside editor + - [ ] Command line + - [ ] Help message + - [X] Switch to read only mode + - [X] No saving + - [X] No exe + - [X] Switch syntax highlighting mode + - [X] Allow providing config file - [ ] UPDATE AUR PACKAGE - [ ] Better documentation (particularly around config file) - [X] 0.3.1 - various fixes @@ -27,5 +29,3 @@ - [ ] More plugins - 0.4.2 - [ ] Bracket and Quote Pairs - [ ] Auto indentation -- [ ] Tweaks - 0.4.3 - - [ ] Read only mode diff --git a/Cargo.lock b/Cargo.lock index 6d42ac9..13a6abe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -150,9 +150,9 @@ checksum = "b9d38e5712e29fb0c2caeb33b1803c8feade6e3380c7a92788fb219999b2849e" [[package]] name = "kaolinite" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa23d0ca201d2a2b921752b47756831936abddf75e5f3c365a7ba45ac4736152" +checksum = "75e99c129d29095bc179446426f0ca274c9cb2654b6aeed3533f16fd7f433b34" dependencies = [ "quick-error", "regex", diff --git a/Cargo.toml b/Cargo.toml index bcd5211..b62a011 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ codegen-units = 1 alinio = "0.2.1" crossterm = "0.27.0" jargon-args = "0.2.7" -kaolinite = "0.8.0" +kaolinite = "0.8.1" mlua = { version = "0.9.9", features = ["lua54", "vendored"] } quick-error = "2.0.1" shellexpand = "3.1.0" diff --git a/config/.oxrc b/config/.oxrc index deb7426..20561c9 100644 --- a/config/.oxrc +++ b/config/.oxrc @@ -52,6 +52,7 @@ Ctrl + Y: Redo Ctrl + F: Find Ctrl + R: Replace Ctrl + D: Delete Line +Ctrl + K: Command Line Shift + ->: Next Tab Shift + <-: Previous Tab {highlight_end} diff --git a/src/cli.rs b/src/cli.rs index a462045..2ca4c8f 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,14 +10,19 @@ Ox: A lightweight and flexible text editor USAGE: ox [options] [files] OPTIONS: - --help, -h : Show this help message - --version, -v : Show the version number - --config [path], -c [path] : Specify the configuration file + --help, -h : Show this help message + --version, -v : Show the version number + --config [path], -c [path] : Specify the configuration file + --readonly, -r : Prevent opened files from writing + --filetype [ext], -f [ext] : Set the file type of files opened EXAMPLES: - ox test.txt - ox test.txt test2.txt - ox /home/user/docs/test.txt + ox + ox test.txt + ox test.txt test2.txt + ox /home/user/docs/test.txt + ox -c config.lua test.txt + ox -r -c ~/.config/.oxrc -f lua my_file.lua\ "; /// Struct to help with starting ox @@ -45,11 +50,21 @@ impl CommandLineInterface { self.jargon.contains(["-v", "--version"]) } + /// Determine if the user wishes to open files in read only + pub fn read_only(&mut self) -> bool { + self.jargon.contains(["-r", "--readonly"]) + } + /// Get all the files the user has requested pub fn get_files(&mut self) { self.to_open = self.jargon.clone().finish(); } + /// Get file types + pub fn get_file_type(&mut self) -> Option { + let filetype_key: Key = ["-f", "--filetype"].into(); + self.jargon.option_arg::(filetype_key.clone()) + } /// Configuration file path pub fn get_config_path(&mut self) -> String { let config_key: Key = ["-c", "--config"].into(); diff --git a/src/editor.rs b/src/editor.rs index 4e69221..77cdc91 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -112,6 +112,22 @@ impl Editor { } } + pub fn set_readonly(&mut self, idx: usize) -> Result<()> { + if let Some(doc) = self.doc.get_mut(idx) { + doc.read_only = true; + } + Ok(()) + } + + /// Set the highlighter of a document + pub fn set_highlighter(&mut self, mut highlighter: Highlighter, idx: usize) -> Result<()> { + if let Some(doc) = self.doc.get_mut(idx) { + highlighter.run(&doc.lines); + self.highlighter[idx] = highlighter; + } + Ok(()) + } + /// Gets a reference to the current document pub fn doc(&self) -> &Document { self.doc.get(self.ptr).unwrap() @@ -183,6 +199,8 @@ impl Editor { (KMod::NONE, KCode::Delete) => self.delete()?, (KMod::NONE, KCode::Enter) => self.enter()?, (KMod::CONTROL, KCode::Char('d')) => self.delete_line()?, + // Command + (KMod::CONTROL, KCode::Char('k')) => self.command()?, _ => (), }, CEvent::Resize(w, h) => { @@ -766,4 +784,28 @@ impl Editor { self.terminal.show_cursor()?; Ok(result) } + + /// Open command line + pub fn command(&mut self) -> Result<()> { + let cmd = self.prompt("Command")?; + self.run_command(cmd)?; + Ok(()) + } + + /// Run a command + pub fn run_command(&mut self, cmd: String) -> Result<()> { + match cmd.split(' ').collect::>().as_slice() { + ["filetype", ext] => { + // Change the highlighter of the current file + self.highlighter[self.ptr] = from_extension(ext, 4) + .unwrap_or_else(|| Highlighter::new(4)); + } + ["readonly", "true"] => self.doc_mut().read_only = true, + ["readonly", "false"] => self.doc_mut().read_only = false, + _ => { + self.feedback = Feedback::Error(format!("Command '{}' not found", cmd)); + } + } + Ok(()) + } } diff --git a/src/error.rs b/src/error.rs index 0ce332e..8b95e35 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,6 +14,7 @@ quick_error! { match err { KError::NoFileName => "This document has no file name, please use 'save as' instead".to_string(), KError::OutOfRange => "Requested operation is out of range".to_string(), + KError::ReadOnlyFile => "This file is read only and can't be saved or edited".to_string(), KError::Rope(rerr) => format!("Backend had an issue processing text: {rerr}"), KError::Io(ioerr) => format!("I/O Error: {ioerr}"), } diff --git a/src/main.rs b/src/main.rs index 3753216..3cf5413 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod error; mod cli; mod ui; +use synoptic::{Highlighter, from_extension}; use error::Result; use cli::CommandLineInterface; use editor::Editor; @@ -25,12 +26,25 @@ fn main() { } fn run(mut cli: CommandLineInterface) -> Result<()> { + let read_only = cli.read_only(); let config_path = cli.get_config_path(); + let file_type = cli.get_file_type(); cli.get_files(); // Create editor and open requested files let mut editor = Editor::new(config_path)?; - for file in cli.to_open { - editor.open_or_new(file)?; + for (c, file) in cli.to_open.iter().enumerate() { + // Open the file + editor.open_or_new(file.to_string())?; + // Set read only if applicable + if read_only { + editor.set_readonly(c)?; + } + // Set highlighter if applicable + if let Some(ref ext) = file_type { + let highlighter = from_extension(&ext, 4) + .unwrap_or_else(|| Highlighter::new(4)); + editor.set_highlighter(highlighter, c)?; + } } // Run the editor and handle errors if applicable