on master help sign in
Separate helix-term as a library
b5682f9Ivan Tham
helix-term/src/application.rs+1−1
@@ -1,6 +1,6 @@
1use helix_view::{document::Mode, Document, Editor, Theme, View};
1use helix_view::{document::Mode, Document, Editor, Theme, View};
2
2
3−use crate::{compositor::Compositor, ui, Args};
3+use crate::{args::Args, compositor::Compositor, ui};
4
4
5use log::{error, info};
5use log::{error, info};
6
6
helix-term/src/args.rsnew file+53
@@ -0,0 +1,53 @@
1+use anyhow::{Error, Result};
2+use std::path::PathBuf;
3+
4+#[derive(Default)]
5+pub struct Args {
6+ pub display_help: bool,
7+ pub display_version: bool,
8+ pub verbosity: u64,
9+ pub files: Vec<PathBuf>,
10+}
11+
12+impl Args {
13+ pub fn parse_args() -> Result<Args> {
14+ let mut args = Args::default();
15+ let argv: Vec<String> = std::env::args().collect();
16+ let mut iter = argv.iter();
17+
18+ iter.next(); // skip the program, we don't care about that
19+
20+ while let Some(arg) = iter.next() {
21+ match arg.as_str() {
22+ "--" => break, // stop parsing at this point treat the remaining as files
23+ "--version" => args.display_version = true,
24+ "--help" => args.display_help = true,
25+ arg if arg.starts_with("--") => {
26+ return Err(Error::msg(format!(
27+ "unexpected double dash argument: {}",
28+ arg
29+ )))
30+ }
31+ arg if arg.starts_with('-') => {
32+ let arg = arg.get(1..).unwrap().chars();
33+ for chr in arg {
34+ match chr {
35+ 'v' => args.verbosity += 1,
36+ 'V' => args.display_version = true,
37+ 'h' => args.display_help = true,
38+ _ => return Err(Error::msg(format!("unexpected short arg {}", chr))),
39+ }
40+ }
41+ }
42+ arg => args.files.push(PathBuf::from(arg)),
43+ }
44+ }
45+
46+ // push the remaining args, if any to the files
47+ for filename in iter {
48+ args.files.push(PathBuf::from(filename));
49+ }
50+
51+ Ok(args)
52+ }
53+}
helix-term/src/lib.rsnew file+8
@@ -0,0 +1,8 @@
1+#![allow(unused)]
2+
3+pub mod application;
4+pub mod args;
5+pub mod commands;
6+pub mod compositor;
7+pub mod keymap;
8+pub mod ui;
helix-term/src/main.rs+7−68
@@ -1,16 +1,9 @@
1−#![allow(unused)]
2−
3−mod application;
4−mod commands;
5−mod compositor;
6−mod keymap;
7−mod ui;
8−
9−use application::Application;
1+use helix_term::application::Application;
2+use helix_term::args::Args;
10
3
11use std::path::PathBuf;
4use std::path::PathBuf;
12
5
13−use anyhow::{Context, Error, Result};
6+use anyhow::{Context, Result};
14
7
15fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
8fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
16 let mut base_config = fern::Dispatch::new();
9 let mut base_config = fern::Dispatch::new();
@@ -45,58 +38,11 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
45 Ok(())
38 Ok(())
46}
39}
47
40
48−pub struct Args {
49− display_help: bool,
50− display_version: bool,
51− verbosity: u64,
52− files: Vec<PathBuf>,
53−}
54−
55−fn parse_args(mut args: Args) -> Result<Args> {
56− let argv: Vec<String> = std::env::args().collect();
57− let mut iter = argv.iter();
58−
59− iter.next(); // skip the program, we don't care about that
60−
61− while let Some(arg) = iter.next() {
62− match arg.as_str() {
63− "--" => break, // stop parsing at this point treat the remaining as files
64− "--version" => args.display_version = true,
65− "--help" => args.display_help = true,
66− arg if arg.starts_with("--") => {
67− return Err(Error::msg(format!(
68− "unexpected double dash argument: {}",
69− arg
70− )))
71− }
72− arg if arg.starts_with('-') => {
73− let arg = arg.get(1..).unwrap().chars();
74− for chr in arg {
75− match chr {
76− 'v' => args.verbosity += 1,
77− 'V' => args.display_version = true,
78− 'h' => args.display_help = true,
79− _ => return Err(Error::msg(format!("unexpected short arg {}", chr))),
80− }
81− }
82− }
83− arg => args.files.push(PathBuf::from(arg)),
84− }
85− }
86−
87− // push the remaining args, if any to the files
88− for filename in iter {
89− args.files.push(PathBuf::from(filename));
90− }
91−
92− Ok(args)
93−}
94−
95#[tokio::main]
41#[tokio::main]
96async fn main() -> Result<()> {
42async fn main() -> Result<()> {
97 let cache_dir = helix_core::cache_dir();
43 let cache_dir = helix_core::cache_dir();
98 if !cache_dir.exists() {
44 if !cache_dir.exists() {
99− std::fs::create_dir(&cache_dir);
45+ std::fs::create_dir(&cache_dir).ok();
100 }
46 }
101
47
102 let logpath = cache_dir.join("helix.log");
48 let logpath = cache_dir.join("helix.log");
@@ -125,14 +71,7 @@ FLAGS:
125 logpath.display(),
71 logpath.display(),
126 );
72 );
127
73
128− let mut args: Args = Args {
129− display_help: false,
130− display_version: false,
131− verbosity: 0,
132− files: [].to_vec(),
133− };
134−
135− args = parse_args(args).context("could not parse arguments")?;
74+ let args = Args::parse_args().context("could not parse arguments")?;
136
75
137 // Help has a higher priority and should be handled separately.
76 // Help has a higher priority and should be handled separately.
138 if args.display_help {
77 if args.display_help {
@@ -147,14 +86,14 @@ FLAGS:
147
86
148 let conf_dir = helix_core::config_dir();
87 let conf_dir = helix_core::config_dir();
149 if !conf_dir.exists() {
88 if !conf_dir.exists() {
150− std::fs::create_dir(&conf_dir);
89+ std::fs::create_dir(&conf_dir).ok();
151 }
90 }
152
91
153 setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
92 setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
154
93
155 // TODO: use the thread local executor to spawn the application task separately from the work pool
94 // TODO: use the thread local executor to spawn the application task separately from the work pool
156 let mut app = Application::new(args).context("unable to create new appliction")?;
95 let mut app = Application::new(args).context("unable to create new appliction")?;
157− app.run().await;
96+ app.run().await.unwrap();
158
97
159 Ok(())
98 Ok(())
160}
99}