boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY support
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
---
title: PTY Features
description: Interactive terminal emulation in BoxMux boxes with keyboard interaction, ANSI processing, and process management.
---


## Table of Contents

- [Overview]#overview
- [Basic PTY Configuration]#basic-pty-configuration
- [Interactive Applications]#interactive-applications
- [PTY Process Management]#pty-process-management
- [Input and Navigation]#input-and-navigation
- [Visual Indicators]#visual-indicators
- [Socket Control]#socket-control
- [Error Handling]#error-handling
- [Best Practices]#best-practices

## Overview

PTY (Pseudo-Terminal) features allow you to run interactive terminal applications like `vim`, `htop`, `ssh`, `less`, `nano`, and database shells within BoxMux boxes. This provides terminal multiplexing with organized box layouts.

### Key Benefits

- **Interactivity**: Keyboard input routing to terminal applications
- **Process Management**: Kill, restart, and monitor PTY processes
- **ANSI Support**: Handling of colors, cursor movements, and escape sequences
- **Scrollback Buffer**: 10,000-line circular buffer for command history
- **Visual Feedback**: Lightning bolt indicators and color-coded borders
- **Error Recovery**: Fallback to regular execution on PTY failures

## Basic PTY Configuration

Enable PTY for any box by adding `pty: true`:

```yaml
app:
  layouts:
    - id: 'main'
      children:
        - id: 'interactive_box'
          title: 'Interactive Terminal ⚡'
          pty: true
          script:
            - htop
          position:
            x1: 0%
            y1: 0%
            x2: 100%
            y2: 100%
```

### PTY vs Regular Execution

```yaml
# Regular script execution (non-interactive)
- id: 'regular_box'
  title: 'System Info'
  script:
    - ps aux | head -10
    - df -h
  refresh_interval: 5000

# PTY execution (interactive)
- id: 'pty_box'
  title: 'Interactive Top ⚡'
  pty: true
  script:
    - htop
  # No refresh_interval needed - PTY runs continuously
```

## Interactive Applications

### System Monitoring

```yaml
- id: 'htop_monitor'
  title: 'System Monitor ⚡'
  pty: true
  script:
    - htop
  position:
    x1: 0%
    y1: 0%
    x2: 50%
    y2: 50%

- id: 'iotop_monitor'
  title: 'IO Monitor ⚡'
  pty: true
  script:
    - sudo iotop
  position:
    x1: 50%
    y1: 0%
    x2: 100%
    y2: 50%
```

### Text Editors

```yaml
- id: 'vim_editor'
  title: 'Text Editor ⚡'
  pty: true
  script:
    - vim /path/to/config.yaml
  position:
    x1: 0%
    y1: 0%
    x2: 100%
    y2: 70%

- id: 'nano_editor'
  title: 'Simple Editor ⚡'
  pty: true
  script:
    - nano /etc/hosts
```

### Remote Connections

```yaml
- id: 'ssh_session'
  title: 'Production Server ⚡'
  pty: true
  script:
    - ssh user@production-server.com
  position:
    x1: 0%
    y1: 0%
    x2: 50%
    y2: 100%

- id: 'database_shell'
  title: 'Database Console ⚡'
  pty: true
  script:
    - psql -h localhost -U postgres -d myapp
  position:
    x1: 50%
    y1: 0%
    x2: 100%
    y2: 100%
```

### File Navigation

```yaml
- id: 'file_manager'
  title: 'File Manager ⚡'
  pty: true
  script:
    - ranger
    # or: mc (midnight commander)
    # or: nnn
```

## PTY Process Management

### Lifecycle Control

PTY processes have full lifecycle management:

- **Automatic Start**: Process starts when box initializes
- **Process Monitoring**: Track running/stopped/failed states  
- **Manual Control**: Kill, restart via keyboard or socket
- **Resource Cleanup**: Proper cleanup when BoxMux exits

### Process Status

PTY boxes show process information in their titles:

```
Interactive Top ⚡ [PID: 12345, Running]
SSH Session ⚡ [PID: 12346, Connected]  
Text Editor ⚡ [Process Stopped]
Database Shell ⚡ [PID: 12347, Error]
```

### Process Actions

Available process management actions:

```yaml
# In choice menus for PTY control
- id: 'pty_controls'
  title: 'PTY Controls'
  choices:
    - id: 'kill_htop'
      content: 'Kill htop process'
      script:
        - echo '{"Command": {"action": "kill_pty", "box_id": "htop_monitor"}}' | nc -U /tmp/boxmux.sock
    
    - id: 'restart_ssh'
      content: 'Restart SSH session'
      script:
        - echo '{"Command": {"action": "restart_pty", "box_id": "ssh_session"}}' | nc -U /tmp/boxmux.sock
```

## Input and Navigation

### Keyboard Input Routing

When a PTY box is focused, all keyboard input is routed directly to the running process:

- **Regular Keys**: Letters, numbers, symbols sent directly
- **Special Keys**: Arrow keys, function keys (F1-F24), navigation keys
- **Modifier Keys**: Ctrl, Alt, Shift combinations
- **Terminal Keys**: Tab, Backspace, Delete, Enter, Escape

### Navigation Keys Supported

```
Arrow Keys:     ↑ ↓ ← →
Function Keys:  F1 F2 F3 ... F24
Navigation:     Home End PageUp PageDown
Editing:        Insert Delete Backspace
Modifiers:      Ctrl+C Ctrl+Z Ctrl+D etc.
```

### Focus Management

Switch between PTY and regular boxes:

- **Tab/Shift+Tab**: Navigate between focusable boxes
- **Mouse Click**: Focus PTY box and enable input routing
- **Box Selection**: Visual indicators show which box receives input

### Scrollback Navigation

PTY boxes maintain scrollback history:

- **Circular Buffer**: 10,000 lines of command history
- **Memory Efficient**: Automatic cleanup of old content
- **Search Support**: Search through command history
- **Thread Safe**: Concurrent access from PTY reader threads

## Visual Indicators

### Box Title Indicators

PTY boxes display special visual indicators:

```
Regular Box:      "System Info"
PTY Box:          "Interactive Top ⚡" 
PTY with Process:   "SSH Session ⚡ [PID: 12345, Running]"
PTY Error State:    "Failed Process ⚡ [Process Stopped]"
```

### Border Colors

PTY boxes use distinct border colors:

- **PTY Active**: Bright cyan borders
- **PTY Error**: Red borders with error indicators
- **Regular Box**: Standard border colors

### Status Information

Process status appears in box titles:

- **PID**: Process ID when running
- **State**: Running, Stopped, Error, Connected
- **Resource Info**: Memory usage, connection status

## Socket Control

### Remote PTY Management

Control PTY processes via Unix socket:

```bash
# Kill PTY process
echo '{"Command": {"action": "kill_pty", "box_id": "htop_box"}}' | nc -U /tmp/boxmux.sock

# Restart PTY process  
echo '{"Command": {"action": "restart_pty", "box_id": "ssh_session"}}' | nc -U /tmp/boxmux.sock

# Query PTY status
echo '{"Command": {"action": "pty_status", "box_id": "vim_box"}}' | nc -U /tmp/boxmux.sock

# Send input to PTY (for automation)
echo '{"Command": {"action": "pty_input", "box_id": "database", "input": "SELECT * FROM users LIMIT 5;\n"}}' | nc -U /tmp/boxmux.sock
```

### Batch Operations

```bash
# Kill all PTY processes
for box in htop_box ssh_session vim_editor; do
  echo '{"Command": {"action": "kill_pty", "box_id": "'$box'"}}' | nc -U /tmp/boxmux.sock
done

# Restart development environment
echo '{"Command": {"action": "restart_pty", "box_id": "vim_editor"}}' | nc -U /tmp/boxmux.sock
echo '{"Command": {"action": "restart_pty", "box_id": "test_runner"}}' | nc -U /tmp/boxmux.sock
```

## Error Handling

### Automatic Fallback

When PTY fails, BoxMux automatically falls back to regular execution:

```yaml
- id: 'robust_box'
  title: 'System Status'
  pty: true  # Try PTY first
  script:
    - htop  # Interactive if PTY works, static output if PTY fails
```

### Failure Tracking

BoxMux tracks PTY failures and avoids repeated attempts:

- **Failure Threshold**: After 3 consecutive failures, avoid PTY for that box
- **Success Recovery**: Clear failure count on successful PTY startup
- **Box-Specific**: Failure tracking per box, not global

### Error States

PTY boxes can be in various error states:

```yaml
# Error state indicators in titles
"Process Name ⚡ [Process Stopped]"      # Process exited
"Process Name ⚡ [PTY Failed]"           # PTY allocation failed  
"Process Name ⚡ [Connection Lost]"      # SSH/network failure
"Process Name ⚡ [Permission Denied]"    # Insufficient permissions
```

### Error Recovery

Manual recovery options:

- **Box Restart**: Kill and restart PTY process
- **Configuration Reload**: Reload YAML with updated settings
- **Fallback Mode**: Disable PTY and use regular execution

## Best Practices

### Performance Considerations

```yaml
# Good: Specific PTY processes
- id: 'htop_box'
  pty: true
  script:
    - htop

# Avoid: Heavy output in PTY
- id: 'log_box'  
  pty: false  # Use regular execution for high-volume logs
  script:
    - tail -f /var/log/messages
  auto_scroll_bottom: true
```

### Resource Management

```yaml
# Limit concurrent PTY processes
app:
  layouts:
    - id: 'development'
      children:
        # Core interactive tools (keep PTY)
        - id: 'vim_editor'
          pty: true
          script: [vim]
        
        - id: 'htop_monitor'  
          pty: true
          script: [htop]
          
        # Background processes (regular execution)
        - id: 'build_output'
          pty: false
          streaming: true
          script: [./watch-build.sh]
```

### Security Considerations

```yaml
# Be careful with PTY and sensitive operations
- id: 'secure_session'
  title: 'Admin Console ⚡'
  pty: true
  script:
    - ssh -o ServerAliveInterval=60 admin@secure-server
  # Consider: timeout, logging, access controls
```

### Layout Design

```yaml
# Organize PTY boxes logically
app:
  layouts:
    - id: 'development'
      title: 'Development Environment'
      children:
        # Main editor (large space)
        - id: 'editor'
          title: 'Code Editor ⚡'
          pty: true
          script: [vim]
          position: {x1: 0%, y1: 0%, x2: 70%, y2: 70%}
          
        # Monitoring (side box)  
        - id: 'system'
          title: 'System Monitor ⚡'
          pty: true
          script: [htop]
          position: {x1: 70%, y1: 0%, x2: 100%, y2: 50%}
          
        # Terminal (bottom)
        - id: 'shell'
          title: 'Shell ⚡'
          pty: true
          script: [bash]
          position: {x1: 0%, y1: 70%, x2: 100%, y2: 100%}
```

### Troubleshooting

Common PTY issues and solutions:

```yaml
# Issue: PTY not starting
# Solution: Check permissions and binary paths
- id: 'debug_box'
  pty: true
  script:
    - /usr/bin/htop  # Use full path
    
# Issue: Input not working
# Solution: Ensure box is focused and check key bindings

# Issue: Display corruption
# Solution: Use ANSI processing and proper terminal size
- id: 'clean_display'
  pty: true
  script:
    - TERM=xterm-256color htop
```

PTY features provide powerful terminal multiplexing capabilities within BoxMux's organized box system, enabling complex interactive workflows with proper process management and visual organization.