Skip to content

Commit

Permalink
Merge pull request #4 from Multirious/multirious
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
Multirious authored Jan 18, 2024
2 parents 41ffcd3 + 64581b9 commit 8672b21
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 32 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ This tool is currently made as a workaround for it.

- File switching
- Focuses Helix window that already exists or create one if not.
- Jump to errors and warnings
- Supports using Helix in WSL.

![preview](https://github.com/Multirious/helix-win-runner/assets/77918086/3a5396ca-5a1d-40f2-900d-9557769f67b9)
![demo](https://github-production-user-asset-6210df.s3.amazonaws.com/77918086/295451165-24059abe-350c-41a1-a617-7a1e1391e806.gif?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20240110%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240110T044835Z&X-Amz-Expires=300&X-Amz-Signature=24455f8f2ac93a467edd19051b3acfb74fc59bb490ede68028343062e2fa0f52&X-Amz-SignedHeaders=host&actor_id=77918086&key_id=0&repo_id=612031113)

You can issue a feature request!

# How to use
# Usages

Run `-h`, `--help` to see availiable flags to use.
Basic usages are explained in the help command.
Expand All @@ -34,9 +35,8 @@ See [Quick Start section](#quick-start).
> [!NOTE]
> This tool do not launch Helix on its own.
This tool actually don't contains any script for initializing Helix on a terminal.
You can use any terminal and configuration you want by creating a launch script and provide them to the `-e`, `--execute-path` flag.
In the [Quick Start section](#quick-start) I've used a batch script for this purpose.
Use `--run` flag to let the CLI run a command when window is not found.
The CLI will run a macro after running a command by default, to prevent this use `--no-init-macro`.
The CLI also works without a launch script but then you need to launch Helix yourself (only need to be done one time per session).

Use `--list` flag to display windows currently opened. Use the information given for `-n`, `--window-process-name` or `-t`, `--window-title`.
Expand All @@ -45,14 +45,13 @@ Recommended to use `-r`, `--relative` and/or `--clipboard` to speed up the proce

# Quick Start

This is a batch script to launch Helix in Windows Terminal:
```bat
wt nt -p "Windows PowerShell" --title "Helix" hx
```
Launch Helix in Windows Terminal

Godot settings:
| Property | Value |
|---------- |------------------|
|`exec_path` |`<path to helix-win-runner>`|
|`exec_flags`|`-e <path to launch script> -w 3 -t "Helix" -n "WindowsTerminal.exe" -p {project} -f {file} -l {line} -c {col} -r --clipboard`|
|`exec_flags`|`--clipboard -r --run "wt nt --title 'Helix' hx -w {project} {file}:{line}:{col}" --no-init-macro -t "Helix" -n "WindowsTerminal.exe" -p {project} -f {file} -l {line} -c {col}`|



29 changes: 18 additions & 11 deletions src/keyboard_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,40 @@ fn quote(e: &mut std::sync::MutexGuard<'static, enigo::Enigo>) {

pub fn helix_change_directory(directory: &str, clipboard: bool) {
let mut enigo = enigo();
enigo.key_sequence(":cd ");
quote(&mut enigo);
enigo.key_click(Key::Escape);
enigo.key_down(Key::Shift);
enigo.key_click(Key::Layout(';'));
enigo.key_up(Key::Shift);
if clipboard {
paste_restore(&mut enigo, directory);
paste_restore(&mut enigo, &format!(r#"cd "{directory}""#));
} else {
enigo.key_sequence("cd ");
quote(&mut enigo);
enigo.key_sequence(directory);
quote(&mut enigo);
}
quote(&mut enigo);
enigo.key_click(Key::Return)
}

pub fn helix_open_file(file: &str, line: u32, column: u32, clipboard: bool) {
let mut enigo = enigo();
enigo.key_sequence(":o ");
quote(&mut enigo);
enigo.key_click(Key::Escape);
enigo.key_down(Key::Shift);
enigo.key_click(Key::Layout(';'));
enigo.key_up(Key::Shift);
if clipboard {
paste_restore(&mut enigo, file);
paste_restore(&mut enigo, &format!(r#"o {file}"#));
} else {
enigo.key_sequence("o ");
quote(&mut enigo);
enigo.key_sequence(file);
quote(&mut enigo);
}
quote(&mut enigo);
enigo.key_click(Key::Return);
enigo.key_sequence(&format!("{line}gg")[..]);
if column > 1 {
enigo.key_sequence(&format!("{column}l")[..]);
}
enigo.key_click(Key::Return)
}

pub fn sleep(secs: f64) {
Expand All @@ -57,12 +64,12 @@ pub fn paste_restore(enigo: &mut enigo::Enigo, msg: &str) {
clipboard_win::set(clipboard_win::formats::Unicode, msg).expect("To set clipboard")
})
.expect("To open clipboard");
sleep(0.1);
sleep(0.05);
enigo.key_down(Key::Control);
enigo.key_down(Key::Layout('v'));
enigo.key_up(Key::Layout('v'));
enigo.key_up(Key::Control);
sleep(0.1);
sleep(0.05);
if let Some(store) = clipboard_store.take() {
clipboard_win::with_clipboard_attempts(10, move || {
store.restore().expect("To set clipboard")
Expand Down
45 changes: 35 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ mod test;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Command to execute when no window found.
/// [DEPRECATED] Command to execute when no window found.
/// If not provided, only search will be done.
///
/// DEPRECATED: use `run` flag.
#[arg(short = 'e', long, value_name = "PATH")]
execute_path: Option<String>,

/// Time to wait after executing `execute_path`.
/// [DEPRECATED] Time to wait after executing `execute_path`.
/// Does nothing if not execute.
///
/// DEPRECATED: use `run_wait` flag.
#[arg(short = 'w', long, value_name = "SECS")]
execute_wait: Option<f64>,

Expand Down Expand Up @@ -73,6 +77,21 @@ struct Args {
/// This will not replace your current clipboard!
#[arg(long)]
clipboard: bool,

/// Use no macro when a command is ran
#[arg(long)]
no_init_macro: bool,

/// Command to run when no window is found.
/// If not provided, only search will be done.
#[arg(long = "run", value_name = "CMD")]
run_command: Option<Vec<String>>,

/// After running a command, wait for a bit before running a macro.
///
/// `no-launch-macro` flag disable this entirely
#[arg(long = "run-wait", value_name = "SECS")]
run_command_and_wait: Option<f64>,
}

fn main() -> Result<()> {
Expand All @@ -99,13 +118,19 @@ fn main() -> Result<()> {

let res = focus_window(args.all, &window_title, &window_process_name);
let mut is_change_directory = false;
match (res, args.execute_path) {
(Err(Error::WindowNotFound), Some(path)) => {
execute_path(path)?;
if let Some(execute_wait) = args.execute_wait {
sleep(execute_wait);
let run_command_args = args
.run_command
.or_else(|| args.execute_path.map(|p| vec![p]));
match (res, run_command_args) {
(Err(Error::WindowNotFound), Some(run_command_args)) => {
run_command(run_command_args)?;
if let Some(wait) = args.run_command_and_wait.or(args.execute_wait) {
sleep(wait);
}
focus_window(args.all, &window_title, &window_process_name)?;
if args.no_init_macro {
return Ok(());
}
if let Some(project_path) = &args.project_path {
let project_path = if args.wsl {
Cow::Owned(window_path_to_wsl(project_path))
Expand All @@ -120,7 +145,7 @@ fn main() -> Result<()> {
_ => {}
}

sleep(0.3);
sleep(0.1);

if let Some(file_path) = args.file_path {
if is_change_directory {
Expand All @@ -147,9 +172,9 @@ fn main() -> Result<()> {
Ok(())
}

fn execute_path<P: AsRef<std::path::Path>>(path: P) -> Result<()> {
fn run_command(args: Vec<String>) -> Result<()> {
use std::process::Command;
Command::new("cmd").arg("/C").arg(path.as_ref()).spawn()?;
Command::new("cmd").arg("/C").args(args).spawn()?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl Window {
SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE,
)?;
self.set_foreground();
// self.set_focus()?;
self.set_focus()?;
self.set_active()?;
Ok(())
}
Expand Down

0 comments on commit 8672b21

Please sign in to comment.