par-term 0.46.0

Cross-platform GPU-accelerated terminal emulator with inline graphics support (Sixel, iTerm2, Kitty)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! tmux input routing: send/paste to tmux sessions and prefix key handling.

use crate::app::window_state::WindowState;

impl WindowState {
    /// Send input through tmux gateway mode.
    ///
    /// When in gateway mode, keyboard input is sent via `send-keys` command
    /// written to the gateway tab's PTY. This routes input to the appropriate tmux pane.
    ///
    /// Returns true if input was handled via tmux, false if it should go to PTY directly.
    pub fn send_input_via_tmux(&self, data: &[u8]) -> bool {
        // par-mux transport routes input through the daemon client — but
        // only when the user is focused on a MUX pane. A local tab's input
        // must fall through to its own PTY; consuming it here starves every
        // non-mux tab the moment a transport is attached.
        #[cfg(feature = "mux")]
        {
            if let Some(transport) = &self.tmux_state.transport {
                // The daemon REQUIRES -t (untargeted send-keys is an error),
                // and nothing sets mux_focused_pane until a pane click or a
                // daemon focus push — so a fresh attach with keyboard focus
                // only must fall back to the pane the user is actually
                // focused on, via the native→tmux reverse map.
                let focused = self.focused_mux_pane_from_native();
                return match focused {
                    Some(focused) => {
                        super::notifications::mux::route_input(&**transport, Some(focused), data)
                    }
                    // Not on a mux pane: let the caller write to the local PTY.
                    None => false,
                };
            }
        }

        // Check if tmux is enabled and connected
        if !self.config.load().tmux.tmux_enabled || !self.is_tmux_connected() {
            crate::debug_trace!(
                "TMUX",
                "send_input_via_tmux: not sending - enabled={}, connected={}",
                self.config.load().tmux.tmux_enabled,
                self.is_tmux_connected()
            );
            return false;
        }

        let session = match &self.tmux_state.tmux_session {
            Some(s) => s,
            None => return false,
        };

        // Format the send-keys command - try pane-specific first
        let cmd = match session.format_send_keys(data) {
            Some(c) => {
                crate::debug_trace!("TMUX", "Using pane-specific send-keys: {}", c.trim());
                c
            }
            None => {
                crate::debug_trace!("TMUX", "No focused pane for send-keys, trying window-based");
                // No focused pane - try window-based routing
                if let Some(cmd) = self.format_send_keys_for_window(data) {
                    crate::debug_trace!("TMUX", "Using window-based send-keys: {}", cmd.trim());
                    cmd
                } else {
                    // No window mapping either - use untargeted send-keys
                    // This sends to tmux's currently active pane
                    let escaped = crate::tmux::escape_keys_for_tmux(data);
                    format!("send-keys {}\n", escaped)
                }
            }
        };

        // Write the command to the gateway tab's PTY
        if self.write_to_gateway(&cmd) {
            crate::debug_trace!("TMUX", "Sent {} bytes via gateway send-keys", data.len());
            return true;
        }

        false
    }

    /// Send raw bytes to the focused tmux pane as literal input, bypassing tmux's
    /// key-name interpretation and any per-pane modifyOtherKeys / extended-keys
    /// re-encoding.
    ///
    /// Uses `send-keys -H`, which tags each byte as `KEYC_LITERAL`. tmux writes
    /// these bytes straight to the pane's PTY via `bufferevent_write`, skipping
    /// the `input_key()` encoder entirely — so whatever we put in arrives
    /// unchanged at the inner application.
    ///
    /// Used for cases where the normal `send-keys` path would mangle the bytes,
    /// e.g. Shift+Enter: the iTerm2 convention is to send raw LF (0x0a), but
    /// `escape_keys_for_tmux` translates that to `C-j`, and tmux's
    /// modifyOtherKeys-mode-2 encoder then delivers `\x1b[27;5;106~` instead of
    /// the literal newline the application expects.
    pub fn send_literal_bytes_via_tmux(&self, bytes: &[u8]) -> bool {
        if !self.config.load().tmux.tmux_enabled || !self.is_tmux_connected() {
            crate::debug_info!(
                "SHIFTENTER",
                "send_literal_bytes_via_tmux: refused - enabled={}, connected={}",
                self.config.load().tmux.tmux_enabled,
                self.is_tmux_connected(),
            );
            return false;
        }

        let session = match &self.tmux_state.tmux_session {
            Some(s) => s,
            None => {
                // par-mux transport: the literal form routes through the
                // daemon client too (send-keys -H).
                #[cfg(feature = "mux")]
                {
                    if let Some(transport) = &self.tmux_state.transport {
                        let focused = self.focused_mux_pane_from_native();
                        return match focused {
                            Some(focused) => super::notifications::mux::route_literal_bytes(
                                &**transport,
                                Some(focused),
                                bytes,
                            ),
                            // Not on a mux pane: local PTY input.
                            None => false,
                        };
                    }
                }
                crate::debug_info!("SHIFTENTER", "send_literal_bytes_via_tmux: no tmux_session");
                return false;
            }
        };

        let focused = session.focused_pane();
        let cmd = match session.format_send_hex_keys(bytes) {
            Some(c) => c,
            None => {
                crate::debug_info!(
                    "SHIFTENTER",
                    "format_send_hex_keys returned None (focused_pane={:?}, state={:?}, gateway={:?})",
                    focused,
                    session.state(),
                    session.gateway_state(),
                );
                return false;
            }
        };

        crate::debug_info!(
            "SHIFTENTER",
            "writing to gateway (focused_pane={:?}): {:?}",
            focused,
            cmd.trim_end(),
        );

        if self.write_to_gateway(&cmd) {
            crate::debug_info!(
                "SHIFTENTER",
                "send_literal_bytes_via_tmux: success ({} bytes)",
                bytes.len()
            );
            return true;
        }

        crate::debug_info!("SHIFTENTER", "write_to_gateway failed");
        false
    }

    /// The mux pane that input, splits, and size pushes target: the
    /// focused native pane's tmux id, or `None` when the user is on a LOCAL
    /// pane (the caller then writes to the local PTY). The focused native
    /// pane is authoritative. The tracked `mux_focused_pane` used to win,
    /// and it outlived its tab: closing the mux tab left it set, so every
    /// keystroke in the remaining local tab went to a daemon pane that no
    /// longer existed.
    #[cfg(feature = "mux")]
    pub(crate) fn focused_mux_pane_from_native(&self) -> Option<u64> {
        let tab = self.tab_manager.active_tab()?;
        let pane = tab.pane_manager()?.focused_pane()?;
        self.tmux_state
            .native_pane_to_tmux_pane
            .get(&pane.id)
            .copied()
    }

    /// Route an encoded mouse report to the focused par-mux pane's daemon
    /// PTY. Returns `true` when it was sent that way; `false` means the
    /// focused pane is not a mux pane and the caller writes locally.
    ///
    /// A mux pane's local terminal has no PTY — it only mirrors the daemon
    /// — so a local write silently dropped every click, drag, and wheel
    /// event (mouse-aware TUIs in mux panes ignored the mouse entirely).
    /// The raw bytes go as `send-keys -H` so key-name translation cannot
    /// mangle the escape sequence.
    #[cfg_attr(not(feature = "mux"), allow(unused_variables))]
    pub(crate) fn route_mouse_report_to_mux(&self, encoded: &[u8]) -> bool {
        #[cfg(feature = "mux")]
        if let Some(transport) = &self.tmux_state.transport
            && let Some(pane) = self.focused_mux_pane_from_native()
        {
            return super::notifications::mux::route_literal_bytes(
                &**transport,
                Some(pane),
                encoded,
            );
        }
        false
    }

    /// Format send-keys command for a specific window (if mapping exists)
    fn format_send_keys_for_window(&self, data: &[u8]) -> Option<String> {
        let active_tab_id = self.tab_manager.active_tab_id()?;

        // Find the tmux window for this tab
        let tmux_window_id = self.tmux_state.tmux_sync.get_window(active_tab_id)?;

        // Format send-keys command with window target using proper escaping
        let escaped = crate::tmux::escape_keys_for_tmux(data);
        Some(format!("send-keys -t @{} {}\n", tmux_window_id, escaped))
    }

    /// Send input via tmux window target (fallback when no pane ID is set).
    ///
    /// This method is a planned fallback path for `TmuxSync` integration: when
    /// the pane-level routing in `send_input_bytes` cannot resolve a pane ID,
    /// routing via the tmux window target (`@N`) is the intended recovery.
    /// It duplicates part of `format_send_keys_for_window` on purpose — the
    /// caller needs to write directly rather than just format the command.
    ///
    /// Not yet wired up because `TmuxSync::get_window` is not yet called from
    /// the hot-path input handler. Wire it up when implementing pane-less
    /// gateway fallback (tracked as a GitHub issue under "tmux integration").
    #[allow(dead_code)] // Infrastructure for TmuxSync pane-less fallback — not yet wired
    fn send_input_via_tmux_window(&self, data: &[u8]) -> bool {
        let active_tab_id = match self.tab_manager.active_tab_id() {
            Some(id) => id,
            None => return false,
        };

        // Find the tmux window for this tab
        let tmux_window_id = match self.tmux_state.tmux_sync.get_window(active_tab_id) {
            Some(id) => id,
            None => {
                crate::debug_trace!(
                    "TMUX",
                    "No tmux window mapping for tab {}, using untargeted send-keys",
                    active_tab_id
                );
                return false;
            }
        };

        // Format send-keys command with window target using proper escaping
        let escaped = crate::tmux::escape_keys_for_tmux(data);
        let cmd = format!("send-keys -t @{} {}\n", tmux_window_id, escaped);

        // Write to gateway tab
        if self.write_to_gateway(&cmd) {
            crate::debug_trace!(
                "TMUX",
                "Sent {} bytes via gateway to window @{}",
                data.len(),
                tmux_window_id
            );
            return true;
        }

        false
    }

    /// Send paste text through tmux gateway mode.
    ///
    /// Uses send-keys -l for literal text to handle special characters properly.
    pub fn paste_via_tmux(&self, text: &str) -> bool {
        // par-mux transport: the panes live in the daemon and the local
        // mirror has no PTY, so falling through to a local paste would be
        // dropped. Route the bytes daemon-side in the same literal
        // `send-keys -H` form the mouse-report fix uses — the caller
        // (Cmd+V, option-click, middle-click) has already sanitized the
        // text, and hex form cannot mangle multi-line or special content.
        #[cfg(feature = "mux")]
        if let Some(transport) = &self.tmux_state.transport
            && let Some(pane) = self.focused_mux_pane_from_native()
        {
            return super::notifications::mux::route_literal_bytes(
                &**transport,
                Some(pane),
                text.as_bytes(),
            );
        }

        if !self.config.load().tmux.tmux_enabled || !self.is_tmux_connected() {
            return false;
        }

        let session = match &self.tmux_state.tmux_session {
            Some(s) => s,
            None => return false,
        };

        // Format the literal send command
        let cmd = match session.format_send_literal(text) {
            Some(c) => c,
            None => return false,
        };

        // Write to gateway tab
        if self.write_to_gateway(&cmd) {
            crate::debug_info!("TMUX", "Pasted {} chars via gateway", text.len());
            return true;
        }

        false
    }

    /// Handle tmux prefix key mode
    ///
    /// In control mode, we intercept the prefix key (e.g., Ctrl+B or Ctrl+Space)
    /// and wait for the next key to translate into a tmux command.
    ///
    /// Returns true if the key was handled by the prefix system.
    pub fn handle_tmux_prefix_key(&mut self, event: &winit::event::KeyEvent) -> bool {
        // Only handle on key press
        if event.state != winit::event::ElementState::Pressed {
            return false;
        }

        // Only handle if tmux is connected
        if !self.config.load().tmux.tmux_enabled || !self.is_tmux_connected() {
            return false;
        }

        let modifiers = self.input_handler.modifiers.state();

        // Check if we're in prefix mode (waiting for command key)
        if self.tmux_state.tmux_prefix_state.is_active() {
            // Ignore modifier-only key presses (Shift, Ctrl, Alt, Super)
            // These are needed to type shifted characters like " and %
            use winit::keyboard::{Key, NamedKey};
            let is_modifier_only = matches!(
                event.logical_key,
                Key::Named(
                    NamedKey::Shift
                        | NamedKey::Control
                        | NamedKey::Alt
                        | NamedKey::Super
                        | NamedKey::Meta
                )
            );
            if is_modifier_only {
                crate::debug_trace!(
                    "TMUX",
                    "Ignoring modifier-only key in prefix mode: {:?}",
                    event.logical_key
                );
                return false; // Don't consume - let the modifier key through
            }

            // Exit prefix mode
            self.tmux_state.tmux_prefix_state.exit();

            // Get focused pane ID for targeted commands
            let focused_pane = self
                .tmux_state
                .tmux_session
                .as_ref()
                .and_then(|s| s.focused_pane());

            // Translate the command key to a tmux command
            if let Some(cmd) =
                crate::tmux::translate_command_key(&event.logical_key, modifiers, focused_pane)
            {
                crate::debug_info!(
                    "TMUX",
                    "Prefix command: {:?} -> {}",
                    event.logical_key,
                    cmd.trim()
                );

                // Send the command to tmux
                if self.write_to_gateway(&cmd) {
                    // Show toast for certain commands (check command base, ignoring target)
                    let cmd_base = cmd.split(" -t").next().unwrap_or(&cmd).trim();
                    match cmd_base {
                        "detach-client" => self.show_toast("tmux: Detaching..."),
                        "new-window" => self.show_toast("tmux: New window"),
                        _ => {}
                    }
                    return true;
                }
            } else {
                // Unknown command key - show feedback
                crate::debug_info!(
                    "TMUX",
                    "Unknown prefix command key: {:?}",
                    event.logical_key
                );
                self.show_toast(format!(
                    "tmux: Unknown command key: {:?}",
                    event.logical_key
                ));
            }
            return true; // Consumed the key even if unknown
        }

        // Check if this is the prefix key
        if let Some(ref prefix_key) = self.tmux_state.tmux_prefix_key
            && prefix_key.matches(&event.logical_key, modifiers)
        {
            crate::debug_info!("TMUX", "Prefix key pressed, entering prefix mode");
            self.tmux_state.tmux_prefix_state.enter();
            self.show_toast("tmux: prefix...");
            return true;
        }

        false
    }
}