| 1 |
use anyhow::{Context, Error, Result}; |
| 2 |
use helix_loader::VERSION_AND_GIT_HASH; |
| 3 |
use helix_term::application::Application; |
| 4 |
use helix_term::args::Args; |
| 5 |
use helix_term::config::{Config, ConfigLoadError}; |
| 6 |
|
| 7 |
fn setup_logging(verbosity: u64) -> Result<()> { |
| 8 |
let level = match verbosity { |
| 9 |
0 => log::LevelFilter::Warn, |
| 10 |
1 => log::LevelFilter::Info, |
| 11 |
2 => log::LevelFilter::Debug, |
| 12 |
_3_or_more => log::LevelFilter::Trace, |
| 13 |
}; |
| 14 |
|
| 15 |
helix_term::logging::init_file(level, &helix_loader::log_file())?; |
| 16 |
|
| 17 |
Ok(()) |
| 18 |
} |
| 19 |
|
| 20 |
fn main() -> Result<()> { |
| 21 |
let exit_code = main_impl()?; |
| 22 |
std::process::exit(exit_code); |
| 23 |
} |
| 24 |
|
| 25 |
#[tokio::main] |
| 26 |
async fn main_impl() -> Result<i32> { |
| 27 |
let args = Args::parse_args().context("could not parse arguments")?; |
| 28 |
|
| 29 |
helix_loader::initialize_config_file(args.config_file.clone()); |
| 30 |
helix_loader::initialize_log_file(args.log_file.clone()); |
| 31 |
|
| 32 |
|
| 33 |
if args.display_help { |
| 34 |
print!( |
| 35 |
"\ |
| 36 |
{} {} |
| 37 |
{} |
| 38 |
{} |
| 39 |
|
| 40 |
USAGE: |
| 41 |
hx [FLAGS] [files]... |
| 42 |
|
| 43 |
ARGS: |
| 44 |
<files>... Set the input file to use, position can also be specified via file[:row[:col]] |
| 45 |
|
| 46 |
FLAGS: |
| 47 |
-h, --help Print help information |
| 48 |
--strict Bail on error for commands that can fail. |
| 49 |
--tutor Load the tutorial |
| 50 |
--health [CATEGORY] Check for potential errors in editor setup |
| 51 |
CATEGORY can be a language or one of 'clipboard', 'languages', |
| 52 |
'all-languages' or 'all'. 'languages' is filtered according to |
| 53 |
user config, 'all-languages' and 'all' are not. If not specified, |
| 54 |
the default is the same as 'all', but with languages filtering. |
| 55 |
-g, --grammar {{fetch|build}} Fetch or builds tree-sitter grammars listed in languages.toml. |
| 56 |
-c, --config <file> Specify a file to use for configuration |
| 57 |
-v Increase logging verbosity each use for up to 3 times |
| 58 |
--log <file> Specify a file to use for logging |
| 59 |
(default file: {}) |
| 60 |
-V, --version Print version information |
| 61 |
--vsplit Split all given files vertically into different windows |
| 62 |
--hsplit Split all given files horizontally into different windows |
| 63 |
-w, --working-dir <path> Specify an initial working directory |
| 64 |
+[N] Open the first given file at line number N, or the last line, if |
| 65 |
N is not specified. |
| 66 |
", |
| 67 |
env!("CARGO_PKG_NAME"), |
| 68 |
VERSION_AND_GIT_HASH, |
| 69 |
env!("CARGO_PKG_AUTHORS"), |
| 70 |
env!("CARGO_PKG_DESCRIPTION"), |
| 71 |
helix_loader::default_log_file().display(), |
| 72 |
); |
| 73 |
std::process::exit(0); |
| 74 |
} |
| 75 |
|
| 76 |
if args.display_version { |
| 77 |
println!("helix {}", VERSION_AND_GIT_HASH); |
| 78 |
std::process::exit(0); |
| 79 |
} |
| 80 |
|
| 81 |
if args.health { |
| 82 |
if let Err(err) = helix_term::health::print_health(args.health_arg) { |
| 83 |
|
| 84 |
|
| 85 |
if err.kind() != std::io::ErrorKind::BrokenPipe { |
| 86 |
return Err(err.into()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
std::process::exit(0); |
| 91 |
} |
| 92 |
|
| 93 |
if args.fetch_grammars { |
| 94 |
helix_loader::grammar::fetch_grammars(args.strict)?; |
| 95 |
return Ok(0); |
| 96 |
} |
| 97 |
|
| 98 |
if args.build_grammars { |
| 99 |
helix_loader::grammar::build_grammars(None, args.strict)?; |
| 100 |
return Ok(0); |
| 101 |
} |
| 102 |
|
| 103 |
setup_logging(args.verbosity).context("failed to initialize logging")?; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if let Some(path) = &args.working_directory { |
| 108 |
helix_stdx::env::set_current_working_dir(path)?; |
| 109 |
} else if let Some((path, _)) = args.files.first().filter(|p| p.0.is_dir()) { |
| 110 |
|
| 111 |
helix_stdx::env::set_current_working_dir(path)?; |
| 112 |
} else if let Err(err) = std::env::current_dir() { |
| 113 |
eprintln!("Couldn't determine the current working directory: {err}"); |
| 114 |
eprintln!("Check that it still exists, or pass an initial directory with `--working-dir`"); |
| 115 |
return Ok(1); |
| 116 |
} |
| 117 |
|
| 118 |
let config = match Config::load_default() { |
| 119 |
Ok(config) => config, |
| 120 |
Err(ConfigLoadError::Error(err)) if err.kind() == std::io::ErrorKind::NotFound => { |
| 121 |
Config::default() |
| 122 |
} |
| 123 |
Err(ConfigLoadError::Error(err)) => return Err(Error::new(err)), |
| 124 |
Err(ConfigLoadError::BadConfig(err)) => { |
| 125 |
eprintln!("Bad config: {}", err); |
| 126 |
eprintln!("Press <ENTER> to continue with default config"); |
| 127 |
use std::io::Read; |
| 128 |
let _ = std::io::stdin().read(&mut []); |
| 129 |
Config::default() |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
let workspace_trust = |
| 134 |
helix_loader::workspace_trust::WorkspaceTrust::new((&config.editor.workspace_trust).into()); |
| 135 |
|
| 136 |
let lang_loader = |
| 137 |
helix_core::config::user_lang_loader(&workspace_trust).unwrap_or_else(|err| { |
| 138 |
eprintln!("{}", err); |
| 139 |
eprintln!("Press <ENTER> to continue with default language config"); |
| 140 |
use std::io::Read; |
| 141 |
|
| 142 |
let _ = std::io::stdin().read(&mut []); |
| 143 |
helix_core::config::default_lang_loader() |
| 144 |
}); |
| 145 |
|
| 146 |
|
| 147 |
let mut app = Application::new(args, config, lang_loader, workspace_trust) |
| 148 |
.context("unable to start Helix")?; |
| 149 |
let mut events = app.event_stream(); |
| 150 |
|
| 151 |
let exit_code = app.run(&mut events).await?; |
| 152 |
|
| 153 |
Ok(exit_code) |
| 154 |
} |