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-05-20 11:14:51 +02:00
|
|
|
|
2023-07-09 16:35:07 +02:00
|
|
|
fn setup_logging(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
|
|
|
|
))
|
|
|
|
})
|
2023-07-09 16:35:07 +02:00
|
|
|
.chain(fern::log_file(helix_loader::log_file())?);
|
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> {
|
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
|
2023-06-10 18:40:39 +02:00
|
|
|
--log <file> 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
|
2023-10-03 03:18:27 +02:00
|
|
|
-w, --working-dir <path> Specify an initial working directory
|
2023-10-12 10:35:43 +02:00
|
|
|
+N Open the first given file at line number N
|
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"),
|
2023-07-09 16:35:07 +02:00
|
|
|
helix_loader::default_log_file().display(),
|
2021-05-31 10:07:43 +02:00
|
|
|
);
|
|
|
|
|
2023-10-18 10:45:05 +02:00
|
|
|
let mut args = Args::parse_args().context("could not parse arguments")?;
|
2021-05-31 10:07:43 +02:00
|
|
|
|
2023-07-09 18:30:43 +02:00
|
|
|
helix_loader::initialize_config_file(args.config_file.clone());
|
|
|
|
helix_loader::initialize_log_file(args.log_file.clone());
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-07-09 16:35:07 +02:00
|
|
|
setup_logging(args.verbosity).context("failed to initialize logging")?;
|
2022-08-04 06:05:52 +02:00
|
|
|
|
2023-11-21 12:07:00 +01:00
|
|
|
// Before setting the working directory, resolve all the paths in args.files
|
|
|
|
for (path, _) in args.files.iter_mut() {
|
|
|
|
*path = helix_core::path::get_canonicalized_path(path);
|
|
|
|
}
|
|
|
|
|
2023-10-09 18:38:09 +02:00
|
|
|
// NOTE: Set the working directory early so the correct configuration is loaded. Be aware that
|
|
|
|
// Application::new() depends on this logic so it must be updated if this changes.
|
2023-10-18 10:45:05 +02:00
|
|
|
if let Some(path) = &args.working_directory {
|
2023-10-09 18:38:09 +02:00
|
|
|
helix_loader::set_current_working_dir(path)?;
|
2023-11-21 12:07:00 +01:00
|
|
|
} else if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) {
|
|
|
|
// If the first file is a directory, it will be the working directory unless -w was specified
|
2023-10-09 18:38:09 +02:00
|
|
|
helix_loader::set_current_working_dir(path)?;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|