From a4ede546481ab91d43bd21ec4e0c5b89f007e67e Mon Sep 17 00:00:00 2001 From: Nik Revenco Date: Fri, 27 Jun 2025 18:06:37 +0100 Subject: [PATCH] feat: `--execute` flag which runs the passed commands before launch --- helix-term/src/args.rs | 9 +++++++++ helix-term/src/main.rs | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/helix-term/src/args.rs b/helix-term/src/args.rs index 9b1b4409b..dba0b755b 100644 --- a/helix-term/src/args.rs +++ b/helix-term/src/args.rs @@ -19,6 +19,8 @@ pub struct Args { pub config_file: Option, pub files: IndexMap>, pub working_directory: Option, + /// The macro to execute on startup + pub execute: Vec, } impl Args { @@ -74,6 +76,13 @@ impl Args { Some(path) => args.log_file = Some(path.into()), None => anyhow::bail!("--log must specify a path to write"), }, + "-x" | "--execute" => { + if let Some(command) = argv.next().as_deref() { + args.execute = helix_view::input::parse_macro(command)?; + } else { + anyhow::bail!("--execute receives a command to execute") + } + } "-w" | "--working-dir" => match argv.next().as_deref() { Some(path) => { args.working_directory = if Path::new(path).is_dir() { diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 31ab85cff..79ec6f7cc 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -1,5 +1,6 @@ use anyhow::{Context, Error, Result}; use crossterm::event::EventStream; +use futures_util::{stream, StreamExt}; use helix_loader::VERSION_AND_GIT_HASH; use helix_term::application::Application; use helix_term::args::Args; @@ -40,7 +41,7 @@ fn main() -> Result<()> { #[tokio::main] async fn main_impl() -> Result { - let args = Args::parse_args().context("could not parse arguments")?; + let mut args = Args::parse_args().context("could not parse arguments")?; helix_loader::initialize_config_file(args.config_file.clone()); helix_loader::initialize_log_file(args.log_file.clone()); @@ -75,6 +76,7 @@ FLAGS: --hsplit Splits all given files horizontally into different windows -w, --working-dir Specify an initial working directory +N Open the first given file at line number N + -x, --execute Executes the given command on startup ", env!("CARGO_PKG_NAME"), VERSION_AND_GIT_HASH, @@ -147,10 +149,20 @@ FLAGS: helix_core::config::default_lang_loader() }); + // sequence of key events to execute before launching the editor + let execute_at_start = + stream::iter(std::mem::take(&mut args.execute).into_iter().map(|item| { + Ok(crossterm::event::Event::Key( + crossterm::event::KeyEvent::from(item), + )) + })); + // TODO: use the thread local executor to spawn the application task separately from the work pool let mut app = Application::new(args, config, lang_loader).context("unable to start Helix")?; - let exit_code = app.run(&mut EventStream::new()).await?; + let exit_code = app + .run(&mut execute_at_start.chain(EventStream::new())) + .await?; Ok(exit_code) }