2022-04-24 22:01:45 +02:00
|
|
|
use anyhow::{Context, Error, Result};
|
2022-04-19 07:21:31 +02:00
|
|
|
use crossterm::event::EventStream;
|
2022-12-09 04:57:03 +01:00
|
|
|
use helix_loader::VERSION_AND_GIT_HASH;
|
2021-06-07 06:40:21 +02:00
|
|
|
use helix_term::application::Application;
|
|
|
|
use helix_term::args::Args;
|
2023-01-31 00:31:21 +01:00
|
|
|
use helix_term::config::{Config, ConfigLoadError};
|
2020-06-02 03:57:01 +02:00
|
|
|
use std::path::PathBuf;
|
2020-05-20 11:14:51 +02:00
|
|
|
|
2021-06-02 18:19:56 +02:00
|
|
|
fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
|
2020-10-23 07:33:09 +02:00
|
|
|
let mut base_config = fern::Dispatch::new();
|
|
|
|
|
|
|
|
base_config = match verbosity {
|
|
|
|
0 => base_config.level(log::LevelFilter::Warn),
|
|
|
|
1 => base_config.level(log::LevelFilter::Info),
|
|
|
|
2 => base_config.level(log::LevelFilter::Debug),
|
|
|
|
_3_or_more => base_config.level(log::LevelFilter::Trace),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Separate file config so we can include year, month and day in file logs
|
|
|
|
let file_config = fern::Dispatch::new()
|
|
|
|
.format(|out, message, record| {
|
|
|
|
out.finish(format_args!(
|
|
|
|
"{} {} [{}] {}",
|
|
|
|
chrono::Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
|
|
|
|
record.target(),
|
|
|
|
record.level(),
|
|
|
|
message
|
|
|
|
))
|
|
|
|
})
|
2021-11-19 05:26:39 +01:00
|
|
|
.chain(fern::log_file(logpath)?);
|
2020-10-23 07:33:09 +02:00
|
|
|
|
|
|
|
base_config.chain(file_config).apply()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-11-15 05:06:12 +01:00
|
|
|
fn main() -> Result<()> {
|
|
|
|
let exit_code = main_impl()?;
|
|
|
|
std::process::exit(exit_code);
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:03:33 +02:00
|
|
|
#[tokio::main]
|
2021-11-15 05:06:12 +01:00
|
|
|
async fn main_impl() -> Result<i32> {
|
2022-02-16 14:57:20 +01:00
|
|
|
let logpath = helix_loader::log_file();
|
2022-03-08 06:25:46 +01:00
|
|
|
let parent = logpath.parent().unwrap();
|
|
|
|
if !parent.exists() {
|
|
|
|
std::fs::create_dir_all(parent).ok();
|
2021-06-02 18:19:56 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 10:07:43 +02:00
|
|
|
let help = format!(
|
|
|
|
"\
|
|
|
|
{} {}
|
|
|
|
{}
|
|
|
|
{}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
hx [FLAGS] [files]...
|
|
|
|
|
|
|
|
ARGS:
|
2022-01-23 08:54:03 +01:00
|
|
|
<files>... Sets the input file to use, position can also be specified via file[:row[:col]]
|
2021-05-31 10:07:43 +02:00
|
|
|
|
|
|
|
FLAGS:
|
2022-03-08 07:13:15 +01:00
|
|
|
-h, --help Prints help information
|
|
|
|
--tutor Loads the tutorial
|
2022-08-31 18:23:45 +02:00
|
|
|
--health [CATEGORY] Checks for potential errors in editor setup
|
|
|
|
CATEGORY can be a language or one of 'clipboard', 'languages'
|
|
|
|
or 'all'. 'all' is the default if not specified.
|
2022-03-10 14:33:51 +01:00
|
|
|
-g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml
|
2022-08-04 06:05:52 +02:00
|
|
|
-c, --config <file> Specifies a file to use for configuration
|
2022-03-08 07:13:15 +01:00
|
|
|
-v Increases logging verbosity each use for up to 3 times
|
2022-09-20 09:11:30 +02:00
|
|
|
--log Specifies a file to use for logging
|
2022-03-08 07:13:15 +01:00
|
|
|
(default file: {})
|
|
|
|
-V, --version Prints version information
|
2022-07-01 11:27:32 +02:00
|
|
|
--vsplit Splits all given files vertically into different windows
|
|
|
|
--hsplit Splits all given files horizontally into different windows
|
2021-05-31 10:07:43 +02:00
|
|
|
",
|
|
|
|
env!("CARGO_PKG_NAME"),
|
2022-12-09 04:57:03 +01:00
|
|
|
VERSION_AND_GIT_HASH,
|
2021-05-31 10:07:43 +02:00
|
|
|
env!("CARGO_PKG_AUTHORS"),
|
|
|
|
env!("CARGO_PKG_DESCRIPTION"),
|
2021-06-02 18:19:56 +02:00
|
|
|
logpath.display(),
|
2021-05-31 10:07:43 +02:00
|
|
|
);
|
|
|
|
|
2021-06-07 06:40:21 +02:00
|
|
|
let args = Args::parse_args().context("could not parse arguments")?;
|
2021-05-31 10:07:43 +02:00
|
|
|
|
|
|
|
// Help has a higher priority and should be handled separately.
|
2021-06-02 19:33:28 +02:00
|
|
|
if args.display_help {
|
2021-05-31 10:07:43 +02:00
|
|
|
print!("{}", help);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:33:28 +02:00
|
|
|
if args.display_version {
|
2022-12-09 04:57:03 +01:00
|
|
|
println!("helix {}", VERSION_AND_GIT_HASH);
|
2021-06-02 19:33:28 +02:00
|
|
|
std::process::exit(0);
|
2021-05-31 10:14:49 +02:00
|
|
|
}
|
2020-10-23 07:33:09 +02:00
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
if args.health {
|
2022-03-30 07:09:25 +02:00
|
|
|
if let Err(err) = helix_term::health::print_health(args.health_arg) {
|
|
|
|
// Piping to for example `head -10` requires special handling:
|
|
|
|
// https://stackoverflow.com/a/65760807/7115678
|
|
|
|
if err.kind() != std::io::ErrorKind::BrokenPipe {
|
|
|
|
return Err(err.into());
|
2022-03-08 06:25:46 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-30 07:09:25 +02:00
|
|
|
|
2022-03-08 06:25:46 +01:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
2022-02-14 03:08:28 +01:00
|
|
|
if args.fetch_grammars {
|
2022-02-16 14:57:20 +01:00
|
|
|
helix_loader::grammar::fetch_grammars()?;
|
2022-02-14 03:08:28 +01:00
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.build_grammars {
|
2022-07-20 16:56:26 +02:00
|
|
|
helix_loader::grammar::build_grammars(None)?;
|
2022-02-14 03:08:28 +01:00
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
2022-09-20 09:11:30 +02:00
|
|
|
let logpath = args.log_file.as_ref().cloned().unwrap_or(logpath);
|
2021-06-02 18:19:56 +02:00
|
|
|
setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
|
2021-05-31 10:14:49 +02:00
|
|
|
|
2022-04-24 22:01:45 +02:00
|
|
|
let config_dir = helix_loader::config_dir();
|
|
|
|
if !config_dir.exists() {
|
|
|
|
std::fs::create_dir_all(&config_dir).ok();
|
|
|
|
}
|
|
|
|
|
2022-08-04 06:05:52 +02:00
|
|
|
helix_loader::initialize_config_file(args.config_file.clone());
|
|
|
|
|
2023-01-31 00:31:21 +01:00
|
|
|
let config = match Config::load_default() {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => {
|
|
|
|
Config::default()
|
|
|
|
}
|
|
|
|
Err(ConfigLoadError::Error(err)) => return Err(Error::new(err)),
|
|
|
|
Err(ConfigLoadError::BadConfig(err)) => {
|
|
|
|
eprintln!("Bad config: {}", err);
|
|
|
|
eprintln!("Press <ENTER> to continue with default config");
|
|
|
|
use std::io::Read;
|
|
|
|
let _ = std::io::stdin().read(&mut []);
|
|
|
|
Config::default()
|
|
|
|
}
|
2022-04-24 22:01:45 +02:00
|
|
|
};
|
|
|
|
|
2022-09-17 05:17:48 +02:00
|
|
|
let syn_loader_conf = helix_core::config::user_syntax_loader().unwrap_or_else(|err| {
|
|
|
|
eprintln!("Bad language config: {}", err);
|
|
|
|
eprintln!("Press <ENTER> to continue with default language config");
|
|
|
|
use std::io::Read;
|
|
|
|
// This waits for an enter press.
|
|
|
|
let _ = std::io::stdin().read(&mut []);
|
|
|
|
helix_core::config::default_syntax_loader()
|
|
|
|
});
|
|
|
|
|
2021-05-06 06:56:34 +02:00
|
|
|
// TODO: use the thread local executor to spawn the application task separately from the work pool
|
2022-09-17 05:17:48 +02:00
|
|
|
let mut app = Application::new(args, config, syn_loader_conf)
|
|
|
|
.context("unable to create new application")?;
|
2021-06-01 23:24:33 +02:00
|
|
|
|
2022-04-19 07:21:31 +02:00
|
|
|
let exit_code = app.run(&mut EventStream::new()).await?;
|
2021-11-15 05:06:12 +01:00
|
|
|
|
|
|
|
Ok(exit_code)
|
2020-05-20 11:14:51 +02:00
|
|
|
}
|