diff options
author | Ivan Tham | 2021-06-07 04:40:21 +0000 |
---|---|---|
committer | Ivan Tham | 2021-06-07 13:35:31 +0000 |
commit | b5682f984b596da32662dc8b5918811318b0c59f (patch) | |
tree | b76d1f4c111b4ca7c2e9c1d98ed5e5f71e31b661 | |
parent | 68affa3c598723a8b9451ef3dcceda83ae161e39 (diff) |
Separate helix-term as a library
helix-term stuff will now be documented in rustdoc.
-rw-r--r-- | helix-term/src/application.rs | 2 | ||||
-rw-r--r-- | helix-term/src/args.rs | 53 | ||||
-rw-r--r-- | helix-term/src/lib.rs | 8 | ||||
-rw-r--r-- | helix-term/src/main.rs | 75 |
4 files changed, 69 insertions, 69 deletions
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 84824b51..60c81607 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -1,6 +1,6 @@ use helix_view::{document::Mode, Document, Editor, Theme, View}; -use crate::{compositor::Compositor, ui, Args}; +use crate::{args::Args, compositor::Compositor, ui}; use log::{error, info}; diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs new file mode 100644 index 00000000..e2bb07c6 --- /dev/null +++ b/helix-term/src/args.rs @@ -0,0 +1,53 @@ +use anyhow::{Error, Result}; +use std::path::PathBuf; + +#[derive(Default)] +pub struct Args { + pub display_help: bool, + pub display_version: bool, + pub verbosity: u64, + pub files: Vec<PathBuf>, +} + +impl Args { + pub fn parse_args() -> Result<Args> { + let mut args = Args::default(); + let argv: Vec<String> = std::env::args().collect(); + let mut iter = argv.iter(); + + iter.next(); // skip the program, we don't care about that + + while let Some(arg) = iter.next() { + match arg.as_str() { + "--" => break, // stop parsing at this point treat the remaining as files + "--version" => args.display_version = true, + "--help" => args.display_help = true, + arg if arg.starts_with("--") => { + return Err(Error::msg(format!( + "unexpected double dash argument: {}", + arg + ))) + } + arg if arg.starts_with('-') => { + let arg = arg.get(1..).unwrap().chars(); + for chr in arg { + match chr { + 'v' => args.verbosity += 1, + 'V' => args.display_version = true, + 'h' => args.display_help = true, + _ => return Err(Error::msg(format!("unexpected short arg {}", chr))), + } + } + } + arg => args.files.push(PathBuf::from(arg)), + } + } + + // push the remaining args, if any to the files + for filename in iter { + args.files.push(PathBuf::from(filename)); + } + + Ok(args) + } +} diff --git a/helix-term/src/lib.rs b/helix-term/src/lib.rs new file mode 100644 index 00000000..2fd40358 --- /dev/null +++ b/helix-term/src/lib.rs @@ -0,0 +1,8 @@ +#![allow(unused)] + +pub mod application; +pub mod args; +pub mod commands; +pub mod compositor; +pub mod keymap; +pub mod ui; diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 75b1dfdc..ea9ed804 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -1,16 +1,9 @@ -#![allow(unused)] - -mod application; -mod commands; -mod compositor; -mod keymap; -mod ui; - -use application::Application; +use helix_term::application::Application; +use helix_term::args::Args; use std::path::PathBuf; -use anyhow::{Context, Error, Result}; +use anyhow::{Context, Result}; fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { let mut base_config = fern::Dispatch::new(); @@ -45,58 +38,11 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { Ok(()) } -pub struct Args { - display_help: bool, - display_version: bool, - verbosity: u64, - files: Vec<PathBuf>, -} - -fn parse_args(mut args: Args) -> Result<Args> { - let argv: Vec<String> = std::env::args().collect(); - let mut iter = argv.iter(); - - iter.next(); // skip the program, we don't care about that - - while let Some(arg) = iter.next() { - match arg.as_str() { - "--" => break, // stop parsing at this point treat the remaining as files - "--version" => args.display_version = true, - "--help" => args.display_help = true, - arg if arg.starts_with("--") => { - return Err(Error::msg(format!( - "unexpected double dash argument: {}", - arg - ))) - } - arg if arg.starts_with('-') => { - let arg = arg.get(1..).unwrap().chars(); - for chr in arg { - match chr { - 'v' => args.verbosity += 1, - 'V' => args.display_version = true, - 'h' => args.display_help = true, - _ => return Err(Error::msg(format!("unexpected short arg {}", chr))), - } - } - } - arg => args.files.push(PathBuf::from(arg)), - } - } - - // push the remaining args, if any to the files - for filename in iter { - args.files.push(PathBuf::from(filename)); - } - - Ok(args) -} - #[tokio::main] async fn main() -> Result<()> { let cache_dir = helix_core::cache_dir(); if !cache_dir.exists() { - std::fs::create_dir(&cache_dir); + std::fs::create_dir(&cache_dir).ok(); } let logpath = cache_dir.join("helix.log"); @@ -125,14 +71,7 @@ FLAGS: logpath.display(), ); - let mut args: Args = Args { - display_help: false, - display_version: false, - verbosity: 0, - files: [].to_vec(), - }; - - args = parse_args(args).context("could not parse arguments")?; + let args = Args::parse_args().context("could not parse arguments")?; // Help has a higher priority and should be handled separately. if args.display_help { @@ -147,14 +86,14 @@ FLAGS: let conf_dir = helix_core::config_dir(); if !conf_dir.exists() { - std::fs::create_dir(&conf_dir); + std::fs::create_dir(&conf_dir).ok(); } setup_logging(logpath, args.verbosity).context("failed to initialize logging")?; // TODO: use the thread local executor to spawn the application task separately from the work pool let mut app = Application::new(args).context("unable to create new appliction")?; - app.run().await; + app.run().await.unwrap(); Ok(()) } |