1 use anyhow::{Context, Error, Result};
2 use helix_term::application::Application;
3 use helix_term::args::Args;
4 use helix_term::config::Config;
5 use helix_term::keymap::merge_keys;
6 use std::path::PathBuf;
7 
setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()>8 fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
9     let mut base_config = fern::Dispatch::new();
10 
11     base_config = match verbosity {
12         0 => base_config.level(log::LevelFilter::Warn),
13         1 => base_config.level(log::LevelFilter::Info),
14         2 => base_config.level(log::LevelFilter::Debug),
15         _3_or_more => base_config.level(log::LevelFilter::Trace),
16     };
17 
18     // Separate file config so we can include year, month and day in file logs
19     let file = std::fs::OpenOptions::new()
20         .write(true)
21         .create(true)
22         .truncate(true)
23         .open(logpath)?;
24     let file_config = fern::Dispatch::new()
25         .format(|out, message, record| {
26             out.finish(format_args!(
27                 "{} {} [{}] {}",
28                 chrono::Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
29                 record.target(),
30                 record.level(),
31                 message
32             ))
33         })
34         .chain(file);
35 
36     base_config.chain(file_config).apply()?;
37 
38     Ok(())
39 }
40 
41 #[tokio::main]
main() -> Result<()>42 async fn main() -> Result<()> {
43     let cache_dir = helix_core::cache_dir();
44     if !cache_dir.exists() {
45         std::fs::create_dir_all(&cache_dir).ok();
46     }
47 
48     let logpath = cache_dir.join("helix.log");
49     let help = format!(
50         "\
51 {} {}
52 {}
53 {}
54 
55 USAGE:
56     hx [FLAGS] [files]...
57 
58 ARGS:
59     <files>...    Sets the input file to use
60 
61 FLAGS:
62     -h, --help       Prints help information
63     --tutor          Loads the tutorial
64     -v               Increases logging verbosity each use for up to 3 times
65                      (default file: {})
66     -V, --version    Prints version information
67 ",
68         env!("CARGO_PKG_NAME"),
69         env!("CARGO_PKG_VERSION"),
70         env!("CARGO_PKG_AUTHORS"),
71         env!("CARGO_PKG_DESCRIPTION"),
72         logpath.display(),
73     );
74 
75     let args = Args::parse_args().context("could not parse arguments")?;
76 
77     // Help has a higher priority and should be handled separately.
78     if args.display_help {
79         print!("{}", help);
80         std::process::exit(0);
81     }
82 
83     if args.display_version {
84         println!("helix {}", env!("CARGO_PKG_VERSION"));
85         std::process::exit(0);
86     }
87 
88     let conf_dir = helix_core::config_dir();
89     if !conf_dir.exists() {
90         std::fs::create_dir_all(&conf_dir).ok();
91     }
92 
93     let config = match std::fs::read_to_string(conf_dir.join("config.toml")) {
94         Ok(config) => merge_keys(toml::from_str(&config)?),
95         Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
96         Err(err) => return Err(Error::new(err)),
97     };
98 
99     setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
100 
101     // TODO: use the thread local executor to spawn the application task separately from the work pool
102     let mut app = Application::new(args, config).context("unable to create new application")?;
103     app.run().await.unwrap();
104 
105     Ok(())
106 }
107