Skip to content

Conversation

@ticpu
Copy link

@ticpu ticpu commented Sep 8, 2025

This commit implements a new Cmd::Macro variant that allows replaying sequences of keystrokes character-by-character, enabling function key bindings to execute commands automatically.

Since this is replaying a command, it integrates with the command processing pipeline seamlessly, without allowing for completely arbitrary commands to be execute.

Example usage:

// Bind F1 to execute "help" command automatically
rl.bind_sequence(
    KeyEvent(KeyCode::F(1), Modifiers::NONE),
    Cmd::Macro("help\n".to_string())
);

When triggered, the macro replays each character as if the user typed it, and the trailing newline triggers AcceptLine to submit the command automatically.

It is related to issue #418: Execute Arbitrary Command Via Keybinding, I took insipration from that issue's name as I needed to "execute" arbitrary command (and not executable) in my application.

@gwenn
Copy link
Collaborator

gwenn commented Sep 9, 2025

@ticpu
Copy link
Author

ticpu commented Sep 9, 2025

@gwenn Hah, that's interesting, thanks for sharing.

I think both could co-exist in this case. Macro is addressing a couple use-cases by simulating the user's input; so it can't really get "deprecated"; I see the potential of adding the more complete Custom Bindings but right now, the only alternative would be to move to a different library until it is fully implemented.

I had to write this so I could have a feature-complete fs_cli (FreeSWITCH's CLI).

Would you see any issues to be able to do it both ways? It could even use the custom bindings architecture by converting the text to actions later on.

@ticpu ticpu force-pushed the feature/cmd-macro branch from 3f08f23 to c35f6a4 Compare September 9, 2025 18:57
ticpu added 2 commits October 20, 2025 13:49
This commit implements a new Cmd::Macro variant that allows replaying
sequences of keystrokes character-by-character, enabling function key
bindings to execute commands automatically.

Since this is replaying a command, it integrates with the command
processing pipeline seamlessly, without allowing for completely
arbitrary commands to be execute.

Example usage:
```rust
// Bind F1 to execute "help" command automatically
rl.bind_sequence(
    KeyEvent(KeyCode::F(1), Modifiers::NONE),
    Cmd::Macro("help\n".to_string())
);
```

When triggered, the macro replays each character as if the user typed it,
and the trailing newline triggers AcceptLine to submit the command
automatically.

It is related to issue kkawakam#418: Execute Arbitrary Command Via Keybinding, I
took insipration from that issue's name as I needed to "execute"
arbitrary command (and not executable) in my application.
This commit adds a new Cmd::MacroClearLine variant that clears the
current input line before executing a macro, with the ability to restore
the original content on the next readline call.

This is useful for key bindings that need to execute commands on a clean
line without losing the user's current input.
@ticpu ticpu force-pushed the feature/cmd-macro branch from c35f6a4 to f3f543a Compare October 20, 2025 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants