From b5682f984b596da32662dc8b5918811318b0c59f Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Mon, 7 Jun 2021 12:40:21 +0800 Subject: [PATCH] Separate helix-term as a library helix-term stuff will now be documented in rustdoc. --- helix-term/src/application.rs | 2 +- helix-term/src/args.rs | 53 +++++++++++++++++++++++++ helix-term/src/lib.rs | 8 ++++ helix-term/src/main.rs | 75 ++++------------------------------- 4 files changed, 69 insertions(+), 69 deletions(-) create mode 100644 helix-term/src/args.rs create mode 100644 helix-term/src/lib.rs diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 84824b51a..60c816071 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 000000000..e2bb07c64 --- /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, +} + +impl Args { + pub fn parse_args() -> Result { + let mut args = Args::default(); + let argv: Vec = 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 000000000..2fd403589 --- /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 75b1dfdc1..ea9ed8048 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, -} - -fn parse_args(mut args: Args) -> Result { - let argv: Vec = 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(()) }