feat: `--execute` flag which runs the passed commands before launch

pull/13847/head
Nik Revenco 2025-06-27 18:06:37 +01:00
parent 171dfc60e5
commit a4ede54648
2 changed files with 23 additions and 2 deletions

View File

@ -19,6 +19,8 @@ pub struct Args {
pub config_file: Option<PathBuf>, pub config_file: Option<PathBuf>,
pub files: IndexMap<PathBuf, Vec<Position>>, pub files: IndexMap<PathBuf, Vec<Position>>,
pub working_directory: Option<PathBuf>, pub working_directory: Option<PathBuf>,
/// The macro to execute on startup
pub execute: Vec<helix_view::input::KeyEvent>,
} }
impl Args { impl Args {
@ -74,6 +76,13 @@ impl Args {
Some(path) => args.log_file = Some(path.into()), Some(path) => args.log_file = Some(path.into()),
None => anyhow::bail!("--log must specify a path to write"), 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() { "-w" | "--working-dir" => match argv.next().as_deref() {
Some(path) => { Some(path) => {
args.working_directory = if Path::new(path).is_dir() { args.working_directory = if Path::new(path).is_dir() {

View File

@ -1,5 +1,6 @@
use anyhow::{Context, Error, Result}; use anyhow::{Context, Error, Result};
use crossterm::event::EventStream; use crossterm::event::EventStream;
use futures_util::{stream, StreamExt};
use helix_loader::VERSION_AND_GIT_HASH; use helix_loader::VERSION_AND_GIT_HASH;
use helix_term::application::Application; use helix_term::application::Application;
use helix_term::args::Args; use helix_term::args::Args;
@ -40,7 +41,7 @@ fn main() -> Result<()> {
#[tokio::main] #[tokio::main]
async fn main_impl() -> Result<i32> { async fn main_impl() -> Result<i32> {
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_config_file(args.config_file.clone());
helix_loader::initialize_log_file(args.log_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 --hsplit Splits all given files horizontally into different windows
-w, --working-dir <path> Specify an initial working directory -w, --working-dir <path> Specify an initial working directory
+N Open the first given file at line number N +N Open the first given file at line number N
-x, --execute <command> Executes the given command on startup
", ",
env!("CARGO_PKG_NAME"), env!("CARGO_PKG_NAME"),
VERSION_AND_GIT_HASH, VERSION_AND_GIT_HASH,
@ -147,10 +149,20 @@ FLAGS:
helix_core::config::default_lang_loader() 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 // 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 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) Ok(exit_code)
} }