Skip to content

Commit

Permalink
added command line, and ability to set read only files, and change fi…
Browse files Browse the repository at this point in the history
…le type
  • Loading branch information
curlpipe committed Jul 16, 2024
1 parent f0ecbc8 commit a1dcf65
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 18 deletions.
14 changes: 7 additions & 7 deletions .todo.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -27,5 +29,3 @@
- [ ] More plugins - 0.4.2
- [ ] Bracket and Quote Pairs
- [ ] Auto indentation
- [ ] Tweaks - 0.4.3
- [ ] Read only mode
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/.oxrc
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
27 changes: 21 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String> {
let filetype_key: Key = ["-f", "--filetype"].into();
self.jargon.option_arg::<String, Key>(filetype_key.clone())
}
/// Configuration file path
pub fn get_config_path(&mut self) -> String {
let config_key: Key = ["-c", "--config"].into();
Expand Down
42 changes: 42 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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::<Vec<&str>>().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(())
}
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
}
Expand Down
18 changes: 16 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod error;
mod cli;
mod ui;

use synoptic::{Highlighter, from_extension};
use error::Result;
use cli::CommandLineInterface;
use editor::Editor;
Expand All @@ -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
Expand Down

0 comments on commit a1dcf65

Please sign in to comment.