2020-06-02 03:57:01 +02:00
|
|
|
#![allow(unused)]
|
2020-09-19 16:16:00 +02:00
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
mod application;
|
2020-12-10 10:13:42 +01:00
|
|
|
mod commands;
|
2020-12-06 03:53:58 +01:00
|
|
|
mod compositor;
|
2020-12-10 10:13:42 +01:00
|
|
|
mod keymap;
|
2020-12-13 05:35:30 +01:00
|
|
|
mod ui;
|
2020-05-20 11:14:51 +02:00
|
|
|
|
2020-10-16 05:29:22 +02:00
|
|
|
use application::Application;
|
2020-05-20 11:14:51 +02:00
|
|
|
|
2021-06-02 12:25:25 +02:00
|
|
|
use helix_core::config_dir;
|
|
|
|
|
2020-06-02 03:57:01 +02:00
|
|
|
use std::path::PathBuf;
|
2020-05-20 11:14:51 +02:00
|
|
|
|
2021-06-02 19:33:28 +02:00
|
|
|
use anyhow::{Context, Error, Result};
|
2020-05-20 11:14:51 +02:00
|
|
|
|
2021-06-01 23:24:33 +02:00
|
|
|
fn setup_logging(verbosity: u64) -> Result<()> {
|
2020-10-23 07:33:09 +02:00
|
|
|
let mut base_config = fern::Dispatch::new();
|
|
|
|
|
|
|
|
// Let's say we depend on something which whose "info" level messages are too
|
|
|
|
// verbose to include in end-user output. If we don't need them,
|
|
|
|
// let's not include them.
|
|
|
|
// .level_for("overly-verbose-target", log::LevelFilter::Warn)
|
|
|
|
|
|
|
|
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-06-02 12:25:25 +02:00
|
|
|
.chain(fern::log_file(config_dir().join("helix.log"))?);
|
2020-10-23 07:33:09 +02:00
|
|
|
|
|
|
|
base_config.chain(file_config).apply()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-05-31 10:07:43 +02:00
|
|
|
pub struct Args {
|
2021-06-02 19:33:28 +02:00
|
|
|
display_help: bool,
|
|
|
|
display_version: bool,
|
|
|
|
verbosity: u64,
|
2021-05-31 10:07:43 +02:00
|
|
|
files: Vec<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:33:28 +02:00
|
|
|
fn parse_args(mut args: Args) -> Result<Args> {
|
|
|
|
let argv: Vec<String> = std::env::args().collect();
|
|
|
|
let mut iter = argv.iter();
|
|
|
|
|
|
|
|
iter.next(); // skip the program, we don't care about that
|
|
|
|
|
2021-06-02 21:24:08 +02:00
|
|
|
while let Some(arg) = iter.next() {
|
|
|
|
match arg.as_str() {
|
|
|
|
"--" => break, // stop parsing at this point treat the remaining as files
|
|
|
|
"--version" => args.display_version = true,
|
|
|
|
"--help" => args.display_help = true,
|
|
|
|
arg if arg.starts_with("--") => {
|
2021-06-02 19:33:28 +02:00
|
|
|
return Err(Error::msg(format!(
|
|
|
|
"unexpected double dash argument: {}",
|
|
|
|
arg
|
|
|
|
)))
|
|
|
|
}
|
2021-06-02 21:24:08 +02:00
|
|
|
arg if arg.starts_with('-') => {
|
|
|
|
let arg = arg.get(1..).unwrap().chars();
|
2021-06-02 19:33:28 +02:00
|
|
|
for chr in arg {
|
|
|
|
match chr {
|
|
|
|
'v' => args.verbosity += 1,
|
|
|
|
'V' => args.display_version = true,
|
|
|
|
'h' => args.display_help = true,
|
|
|
|
_ => return Err(Error::msg(format!("unexpected short arg {}", chr))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 21:24:08 +02:00
|
|
|
arg => args.files.push(PathBuf::from(arg)),
|
2021-06-02 19:33:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// push the remaining args, if any to the files
|
|
|
|
for filename in iter {
|
|
|
|
args.files.push(PathBuf::from(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(args)
|
|
|
|
}
|
|
|
|
|
2021-06-02 16:03:33 +02:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-05-31 10:07:43 +02:00
|
|
|
let help = format!(
|
|
|
|
"\
|
|
|
|
{} {}
|
|
|
|
{}
|
|
|
|
{}
|
|
|
|
|
|
|
|
USAGE:
|
|
|
|
hx [FLAGS] [files]...
|
|
|
|
|
|
|
|
ARGS:
|
|
|
|
<files>... Sets the input file to use
|
|
|
|
|
|
|
|
FLAGS:
|
|
|
|
-h, --help Prints help information
|
|
|
|
-v Increases logging verbosity each use for up to 3 times
|
|
|
|
-V, --version Prints version information
|
|
|
|
",
|
|
|
|
env!("CARGO_PKG_NAME"),
|
|
|
|
env!("CARGO_PKG_VERSION"),
|
|
|
|
env!("CARGO_PKG_AUTHORS"),
|
|
|
|
env!("CARGO_PKG_DESCRIPTION"),
|
|
|
|
);
|
|
|
|
|
2021-06-02 19:33:28 +02:00
|
|
|
let mut args: Args = Args {
|
|
|
|
display_help: false,
|
|
|
|
display_version: false,
|
|
|
|
verbosity: 0,
|
|
|
|
files: [].to_vec(),
|
|
|
|
};
|
|
|
|
|
|
|
|
args = parse_args(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 {
|
|
|
|
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
|
|
|
|
std::process::exit(0);
|
2021-05-31 10:14:49 +02:00
|
|
|
}
|
2020-10-23 07:33:09 +02:00
|
|
|
|
2021-06-02 12:25:25 +02:00
|
|
|
let conf_dir = config_dir();
|
|
|
|
|
|
|
|
if !conf_dir.exists() {
|
|
|
|
std::fs::create_dir(&conf_dir);
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:33:28 +02:00
|
|
|
setup_logging(args.verbosity).context("failed to initialize logging")?;
|
2021-05-31 10:14:49 +02:00
|
|
|
|
2021-03-25 07:26:25 +01:00
|
|
|
// initialize language registry
|
|
|
|
use helix_core::syntax::{Loader, LOADER};
|
2021-04-07 16:56:20 +02:00
|
|
|
|
|
|
|
// load $HOME/.config/helix/languages.toml, fallback to default config
|
|
|
|
let config = std::fs::read(config_dir().join("languages.toml"));
|
|
|
|
let toml = config
|
|
|
|
.as_deref()
|
|
|
|
.unwrap_or(include_bytes!("../../languages.toml"));
|
|
|
|
|
2021-06-01 23:24:33 +02:00
|
|
|
let config = toml::from_slice(toml).context("Could not parse languages.toml")?;
|
|
|
|
LOADER.get_or_init(|| Loader::new(config));
|
2021-03-25 07:26:25 +01:00
|
|
|
|
2021-05-06 06:56:34 +02:00
|
|
|
// TODO: use the thread local executor to spawn the application task separately from the work pool
|
2021-06-01 23:24:33 +02:00
|
|
|
let mut app = Application::new(args).context("unable to create new appliction")?;
|
2021-06-02 16:03:33 +02:00
|
|
|
app.run().await;
|
2021-06-01 23:24:33 +02:00
|
|
|
|
|
|
|
Ok(())
|
2020-05-20 11:14:51 +02:00
|
|
|
}
|