Skip to content

Commit

Permalink
Improved cli
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Jul 16, 2024
1 parent 4d4a649 commit f0ecbc8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .todo.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
- [ ] 0.3.2
- [ ] Better CLI
- [ ] Allow providing config file
- [ ] Allow providing syntax highlighting option
- [ ] Read only mode
- [X] Allow providing config file
- [ ] 0.3.3
- [ ] Help page inside editor
- [ ] UPDATE AUR PACKAGE
Expand All @@ -28,3 +27,5 @@
- [ ] More plugins - 0.4.2
- [ ] Bracket and Quote Pairs
- [ ] Auto indentation
- [ ] Tweaks - 0.4.3
- [ ] Read only mode
2 changes: 1 addition & 1 deletion 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
@@ -1,6 +1,6 @@
[package]
name = "ox"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Curlpipe <[email protected]>"]
description = "A Rust powered text editor."
Expand Down
26 changes: 18 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use jargon_args::Jargon;
use jargon_args::{Key, Jargon};

/// Holds the version number of the crate
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

/// Holds the help dialog
pub const HELP: &str = "\
Cactus: A compact and complete kaolinite implementation
Ox: A lightweight and flexible text editor
USAGE: cactus [options] [files]
USAGE: ox [options] [files]
OPTIONS:
--help, -h : Show this help message
--version, -v : Show the version number
--help, -h : Show this help message
--version, -v : Show the version number
--config [path], -c [path] : Specify the configuration file
EXAMPLES:
cactus test.txt
cactus test.txt test2.txt
cactus /home/user/docs/test.txt
ox test.txt
ox test.txt test2.txt
ox /home/user/docs/test.txt
";

/// Struct to help with starting ox
Expand Down Expand Up @@ -48,4 +49,13 @@ impl CommandLineInterface {
pub fn get_files(&mut self) {
self.to_open = self.jargon.clone().finish();
}

/// Configuration file path
pub fn get_config_path(&mut self) -> String {
let config_key: Key = ["-c", "--config"].into();
match self.jargon.option_arg::<String, Key>(config_key.clone()) {
Some(config) => config,
None => "~/.oxrc".to_string(),
}
}
}
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Config {
}

impl Config {
pub fn read() -> Result<Self> {
pub fn read(path: String) -> Result<Self> {
// Load defaults
let lua = Lua::new();

Expand All @@ -49,7 +49,7 @@ impl Config {
lua.load(DEFAULT_CONFIG).exec()?;

// Attempt to read config file from home directory
if let Ok(path) = shellexpand::full("~/.oxrc") {
if let Ok(path) = shellexpand::full(&path) {
if let Ok(config) = std::fs::read_to_string(path.to_string()) {
// Update configuration with user-defined values
lua.load(config).exec()?;
Expand Down
4 changes: 2 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ pub struct Editor {

impl Editor {
/// Create a new instance of the editor
pub fn new() -> Result<Self> {
pub fn new(config: String) -> Result<Self> {
Ok(Self {
doc: vec![],
ptr: 0,
terminal: Terminal::new(),
config: Config::read()?,
config: Config::read(config)?,
active: true,
greet: false,
highlighter: vec![],
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ fn main() {
return
}

cli.get_files();

let _ = run(cli);
}

fn run(cli: CommandLineInterface) -> Result<()> {
fn run(mut cli: CommandLineInterface) -> Result<()> {
let config_path = cli.get_config_path();
cli.get_files();
// Create editor and open requested files
let mut editor = Editor::new()?;
let mut editor = Editor::new(config_path)?;
for file in cli.to_open {
editor.open_or_new(file)?;
}
Expand Down

0 comments on commit f0ecbc8

Please sign in to comment.