Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/tty7-core/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ pub struct Config {
/// they wrote.
#[serde(default, deserialize_with = "de_lenient")]
pub custom_shells: Vec<CustomShell>,
/// Text sent to a pane when what is before its cursor matches a pattern.
/// Lenient for the same reason as `custom_shells`.
#[serde(default, deserialize_with = "de_lenient")]
pub triggers: Vec<Trigger>,

pub link_url: bool,
/// What a clicked file link opens in. `None` means the key predates this
Expand Down Expand Up @@ -751,6 +755,33 @@ pub struct CustomShell {
pub args: Vec<String>,
}

/// When the text before the cursor matches `regex`, `text` is typed into the
/// pane (iTerm2's "Send Text" trigger). It fires once per match: the line has
/// to stop matching before the trigger can fire again.
///
/// `text` is sent as written, so a trailing `\r` is what presses Return.
/// It sits in `config.json` in the clear.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[serde(default)]
pub struct Trigger {
pub name: String,
pub regex: String,
pub text: String,
#[serde(default = "default_true")]
pub enabled: bool,
}

impl Default for Trigger {
fn default() -> Self {
Self {
name: String::new(),
regex: String::new(),
text: String::new(),
enabled: true,
}
}
}

pub fn default_font_fallbacks() -> Vec<String> {
let names: &[&str] = if cfg!(target_os = "macos") {
&[
Expand Down Expand Up @@ -817,6 +848,7 @@ impl Default for Config {
prefix: default_prefix(),
shell: None,
custom_shells: Vec::new(),
triggers: Vec::new(),
link_url: true,
link_file_open: Some(LinkFileOpen::Internal),
link_file_command: None,
Expand Down
1 change: 1 addition & 0 deletions docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ their id from the file name. [More about themes →](/customization/themes)
|---|---|---|---|
| `shell` | object | — | `{"program": "fish", "args": ["-l"]}`. Unset uses the platform default. `args` you write are launched verbatim, which also turns off [shell integration](/reference/shell-integration) for that shell — leave them out to keep it. |
| `custom_shells` | array | `[]` | Extra entries for the new-tab menu: `[{"label": "Ubuntu", "program": "wsl.exe", "args": ["-d", "Ubuntu"]}]`. Launched exactly as written, listed after the detected shells — in the menu's **Other Shells…** list until you have opened it, and in Search Everywhere's Terminals tab as *Shell:* followed by its label. Give an entry arguments and tty7 also skips [shell integration](/reference/shell-integration) for it, so it has no prompt marks, working-directory tracking, or command-finished notifications; an entry with no arguments, on a shell tty7 recognizes, is integrated like the detected row beside it. An entry with no `program` is skipped; with no `label` it is named after its program. |
| `triggers` | array | `[]` | Edited under **Settings → Triggers**. Text to type into a pane when what is before its cursor matches a regular expression, like iTerm2's *Send Text* triggers: `[{"name": "ssh", "regex": "(?i)password:\\s*$", "text": "hunter2\r"}]`. `regex` is a Rust [regex](https://docs.rs/regex) matched against the line up to the cursor (a soft-wrapped prompt is read from its start), so anchor prompts with `$`. `text` is sent as written; end it with `\r` to press Return. A trigger fires once per match and again only after the line stops matching; nothing fires on the alternate screen or while a pane is being reattached. `enabled` defaults to `true`; an entry that is disabled, has no `regex`, or whose regex does not compile is skipped. `text` is stored in `config.json` in plain text. |
| `working_directory` | object | `{"strategy":"inherit"}` | `strategy` is `inherit`, `home`, or `custom`; `path` is used when custom. |
| `env` | object | `{}` | Extra environment variables for every pane. |
| `scrollback_limit` | number | `10000` | Lines per pane (100–100,000). New panes only. |
Expand Down
1 change: 1 addition & 0 deletions src/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod search;
mod signature;
mod size;
mod smart_select;
pub(crate) mod trigger;
mod typeahead;
pub mod view;

Expand Down
56 changes: 56 additions & 0 deletions src/terminal/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ struct ReaderSignals {
local_conpty: Arc<AtomicBool>,
/// Whether the program in the pane switched on DEC mode 2031.
color_scheme_updates: Arc<AtomicBool>,
triggers: super::trigger::SharedTriggers,
}

/// What kind of pty is at the far end of a pane's link, which is what decides
Expand Down Expand Up @@ -655,6 +656,9 @@ pub struct RemoteTerminal {
local_conpty: Arc<AtomicBool>,
/// Whether the program in the pane switched on DEC mode 2031.
color_scheme_updates: Arc<AtomicBool>,
/// The compiled `triggers` of the user's config, read by the reader and
/// swapped by [`Self::apply_user_config`].
triggers: super::trigger::SharedTriggers,
}

/// The workspace id a spawn carries, so the pane's shell gets `$TTY7_WS` and a
Expand Down Expand Up @@ -992,6 +996,7 @@ impl RemoteTerminal {
// remembers what a `RemoteContext` taught the old reader.
local_conpty: self.local_conpty.clone(),
color_scheme_updates: self.color_scheme_updates.clone(),
triggers: self.triggers.clone(),
},
);
self.reader_thread = Some(reader);
Expand Down Expand Up @@ -1095,6 +1100,7 @@ impl RemoteTerminal {
let reader_quit = Arc::new(AtomicBool::new(false));
let local_conpty = Arc::new(AtomicBool::new(pty == PtySource::LocalConpty));
let color_scheme_updates = Arc::new(AtomicBool::new(false));
let triggers = super::trigger::TriggerSet::shared(&user_config.triggers);
let reader_thread = Self::spawn_reader(
term.clone(),
proxy.clone(),
Expand Down Expand Up @@ -1122,6 +1128,7 @@ impl RemoteTerminal {
lease: lease.clone(),
local_conpty: local_conpty.clone(),
color_scheme_updates: color_scheme_updates.clone(),
triggers: triggers.clone(),
},
);

Expand Down Expand Up @@ -1168,6 +1175,7 @@ impl RemoteTerminal {
reader_quit,
local_conpty,
color_scheme_updates,
triggers,
})
}

Expand Down Expand Up @@ -1208,6 +1216,7 @@ impl RemoteTerminal {
pub fn apply_user_config(&self, user_config: &crate::core::config::Config) {
let mut term = self.term.lock();
term.set_options(terminal_config_from_user(user_config));
super::trigger::replace(&self.triggers, &user_config.triggers);
}

fn spawn_reader(
Expand Down Expand Up @@ -1242,6 +1251,7 @@ impl RemoteTerminal {
lease,
local_conpty,
color_scheme_updates,
triggers,
} = signals;
let mut awaiting_replay = awaiting_replay;
crate::core::threads::promote_to_user_interactive();
Expand Down Expand Up @@ -1269,6 +1279,8 @@ impl RemoteTerminal {
let mut command_tok = OscTokenizer::new(&[b"133"]);
let mut command_cursor = CommandCursorStyle::default();
let mut prompt_break = PromptBreak::default();
let mut triggers = super::trigger::Matcher::new(triggers);
let mut trigger_line = String::new();
// A new link replays the pane from scratch, 2031 included
// (`term_modes` restores it ahead of the ring).
let mut modes = TerminalModes::new();
Expand Down Expand Up @@ -1384,6 +1396,17 @@ impl RemoteTerminal {
tr_adv_t += t0.elapsed() - waited;
}
}
// Not on the replay of an attach: a `Password:`
// the pane answered long ago is still in it.
if !replaying_state && triggers.active() {
let any = super::trigger::text_before_cursor(
&*term.lock(),
&mut trigger_line,
);
for text in triggers.check(any.then_some(&*trigger_line)) {
proxy.send_event(AlacEvent::PtyWrite(text));
}
}
if let Ok(mut notes) = osc_notes.lock() {
osc.feed(&out_batch, &mut *notes);
}
Expand Down Expand Up @@ -5539,6 +5562,39 @@ mod tests {
);
}

#[test]
fn a_trigger_types_its_text_when_the_prompt_arrives() {
crate::core::config::pin_test_config_dir();
let (client_side, mut daemon_side) = UnixStream::pair().unwrap();
let term = RemoteTerminal::from_stream(client_side, TermSize::new(80, 24)).unwrap();
let mut user_config = crate::core::config::Config::default();
user_config.triggers = vec![crate::core::config::Trigger {
regex: r"(?i)password:\s*$".into(),
text: "hunter2\r".into(),
..Default::default()
}];
term.apply_user_config(&user_config);

DaemonMsg::Output(b"ssh host\r\nPassword: ".to_vec())
.encode(&mut daemon_side)
.unwrap();
daemon_side.flush().unwrap();

let mut typed = None;
for _ in 0..200 {
while let Ok(event) = term.events.try_recv() {
if let AlacEvent::PtyWrite(text) = event {
typed = Some(text);
}
}
if typed.is_some() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(5));
}
assert_eq!(typed.as_deref(), Some("hunter2\r"));
}

#[test]
fn kitty_keyboard_negotiation_reports_the_requested_mode() {
let config = terminal_config_from_user(&crate::core::config::Config::default());
Expand Down
Loading
Loading