tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
//! Native-only helpers for streaming child-process output into components.
//!
//! This module is available only on non-wasm targets. It is intended for
//! non-interactive subprocesses whose stdout/stderr should be consumed by the
//! TUI while it keeps running. Interactive programs that need the real terminal
//! should use [`crate::terminal_handoff`] instead.

use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command as StdCommand, ExitStatus, Stdio};
use std::sync::Arc;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use crate::{Command, TaskPolicy};

/// Description of a native child process to run and stream.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProcessSpec {
    program: Arc<str>,
    args: Vec<Arc<str>>,
    cwd: Option<PathBuf>,
    env: Vec<(Arc<str>, Arc<str>)>,
    stdin: Option<Vec<u8>>,
}

impl ProcessSpec {
    /// Create a process spec for `program`.
    pub fn new(program: impl Into<Arc<str>>) -> Self {
        Self {
            program: program.into(),
            args: Vec::new(),
            cwd: None,
            env: Vec::new(),
            stdin: None,
        }
    }

    /// Append a single argument.
    #[must_use]
    pub fn arg(mut self, arg: impl Into<Arc<str>>) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Append multiple arguments.
    #[must_use]
    pub fn args<I, S>(mut self, args: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<Arc<str>>,
    {
        self.args.extend(args.into_iter().map(Into::into));
        self
    }

    /// Set the child process working directory.
    #[must_use]
    pub fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
        self.cwd = Some(cwd.into());
        self
    }

    /// Set an environment variable for the child process.
    #[must_use]
    pub fn env(mut self, key: impl Into<Arc<str>>, value: impl Into<Arc<str>>) -> Self {
        self.env.push((key.into(), value.into()));
        self
    }

    /// Provide bytes to write to the child process stdin, then close stdin.
    #[must_use]
    pub fn stdin(mut self, stdin: impl Into<Vec<u8>>) -> Self {
        self.stdin = Some(stdin.into());
        self
    }

    /// Program executable name or path.
    pub fn program(&self) -> &str {
        &self.program
    }

    /// Process arguments.
    pub fn args_slice(&self) -> &[Arc<str>] {
        &self.args
    }

    /// Working directory, when configured.
    pub fn cwd_path(&self) -> Option<&Path> {
        self.cwd.as_deref()
    }

    /// Environment overrides.
    pub fn env_slice(&self) -> &[(Arc<str>, Arc<str>)] {
        &self.env
    }

    /// Bytes that will be written to stdin, when configured.
    pub fn stdin_bytes(&self) -> Option<&[u8]> {
        self.stdin.as_deref()
    }

    /// Run the process on the current thread and emit streaming events.
    ///
    /// Stdout and stderr are drained on separate helper threads so a child that
    /// writes heavily to both streams cannot deadlock on a full pipe.
    pub fn stream(self, emit: impl FnMut(ProcessEvent)) -> io::Result<()> {
        stream_process(self, emit)
    }

    /// Create an unkeyed background [`Command`] that streams process events as messages.
    ///
    /// Unkeyed commands do not have a normal runtime coalescing/cancellation
    /// owner. Use [`Self::command_keyed`] when newer work should cancel an
    /// active process.
    pub fn command<Msg, F>(self, map: F) -> Command
    where
        Msg: Send + 'static,
        F: Fn(ProcessEvent) -> Msg + Send + 'static,
    {
        process_command(self, map)
    }

    /// Create a keyed background [`Command`] that streams process events as messages.
    ///
    /// With [`TaskPolicy::LatestOnly`], submitting newer work for the same key
    /// cancels the active token. This helper observes that token, kills the
    /// child process, drains stdout/stderr, and suppresses stale messages.
    pub fn command_keyed<Msg, F>(
        self,
        key: impl Into<Arc<str>>,
        policy: TaskPolicy,
        map: F,
    ) -> Command
    where
        Msg: Send + 'static,
        F: Fn(ProcessEvent) -> Msg + Send + 'static,
    {
        process_command_keyed(key, policy, self, map)
    }

    fn to_std_command(&self) -> StdCommand {
        let mut command = StdCommand::new(self.program.as_ref());
        command.args(self.args.iter().map(AsRef::<str>::as_ref));
        if let Some(cwd) = &self.cwd {
            command.current_dir(cwd);
        }
        for (key, value) in &self.env {
            command.env(key.as_ref(), value.as_ref());
        }
        command.stdin(if self.stdin.is_some() {
            Stdio::piped()
        } else {
            Stdio::null()
        });
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());
        command
    }
}

/// Framework-owned child-process exit status.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ProcessExitStatus {
    code: Option<i32>,
    success: bool,
}

impl ProcessExitStatus {
    /// Platform exit code, when the platform reports one.
    pub fn code(self) -> Option<i32> {
        self.code
    }

    /// Whether the process exited successfully.
    pub fn success(self) -> bool {
        self.success
    }
}

impl From<ExitStatus> for ProcessExitStatus {
    fn from(status: ExitStatus) -> Self {
        Self {
            code: status.code(),
            success: status.success(),
        }
    }
}

/// Streaming child-process event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProcessEvent {
    /// Bytes read from stdout.
    Stdout(Vec<u8>),
    /// Bytes read from stderr.
    Stderr(Vec<u8>),
    /// Process exited and all captured output has been drained.
    Exited(ProcessExitStatus),
    /// Process-level or pipe-level error.
    Error(Arc<str>),
}

/// Run a process on the current thread and emit stdout/stderr/exit events.
pub fn stream_process(spec: ProcessSpec, mut emit: impl FnMut(ProcessEvent)) -> io::Result<()> {
    stream_process_until(spec, || false, &mut emit)
}

/// Run a process and stop it when `should_cancel` returns `true`.
///
/// On cancellation the child is killed, stdout/stderr are drained, and an
/// `Exited` event is emitted with the platform status reported by `wait`.
pub fn stream_process_until(
    mut spec: ProcessSpec,
    should_cancel: impl Fn() -> bool,
    mut emit: impl FnMut(ProcessEvent),
) -> io::Result<()> {
    let mut command = spec.to_std_command();
    let stdin_bytes = spec.stdin.take();
    let mut child = command.spawn()?;

    let stdout = child.stdout.take();
    let stderr = child.stderr.take();
    let child_stdin = child.stdin.take();
    let (tx, rx) = mpsc::channel();

    let stdout_thread = stdout.map(|stdout| spawn_reader(stdout, tx.clone(), ProcessEvent::Stdout));
    let stderr_thread = stderr.map(|stderr| spawn_reader(stderr, tx.clone(), ProcessEvent::Stderr));
    let stdin_thread = stdin_bytes.and_then(|bytes| {
        child_stdin.map(|mut stdin| {
            let tx = tx.clone();
            thread::spawn(move || {
                if let Err(err) = stdin.write_all(&bytes) {
                    send_error(&tx, err);
                }
            })
        })
    });

    let status = loop {
        while let Ok(event) = rx.try_recv() {
            emit(event);
        }

        if should_cancel() {
            let _ = child.kill();
            break child.wait();
        }

        if let Some(status) = child.try_wait()? {
            break Ok(status);
        }

        match rx.recv_timeout(Duration::from_millis(20)) {
            Ok(event) => emit(event),
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                if let Some(status) = child.try_wait()? {
                    break Ok(status);
                }
            }
        }
    };

    join_optional(stdin_thread);
    join_optional(stdout_thread);
    join_optional(stderr_thread);

    for event in rx.try_iter() {
        emit(event);
    }

    emit(ProcessEvent::Exited(status?.into()));
    Ok(())
}

/// Create a background command that maps process events into component messages.
pub fn process_command<Msg, F>(spec: ProcessSpec, map: F) -> Command
where
    Msg: Send + 'static,
    F: Fn(ProcessEvent) -> Msg + Send + 'static,
{
    Command::spawn::<Msg, _>(move |link| {
        if let Err(err) = stream_process_until(
            spec,
            || link.is_cancelled(),
            |event| {
                let _ = link.send_if_not_cancelled(map(event));
            },
        ) {
            let _ =
                link.send_if_not_cancelled(map(ProcessEvent::Error(Arc::from(err.to_string()))));
        }
    })
}

/// Create a keyed background command that maps process events into component messages.
pub fn process_command_keyed<Msg, F>(
    key: impl Into<Arc<str>>,
    policy: TaskPolicy,
    spec: ProcessSpec,
    map: F,
) -> Command
where
    Msg: Send + 'static,
    F: Fn(ProcessEvent) -> Msg + Send + 'static,
{
    Command::spawn_keyed::<Msg, _>(key, policy, move |link| {
        if let Err(err) = stream_process_until(
            spec,
            || link.is_cancelled(),
            |event| {
                let _ = link.send_if_not_cancelled(map(event));
            },
        ) {
            let _ =
                link.send_if_not_cancelled(map(ProcessEvent::Error(Arc::from(err.to_string()))));
        }
    })
}

fn spawn_reader<R>(
    mut reader: R,
    tx: mpsc::Sender<ProcessEvent>,
    wrap: fn(Vec<u8>) -> ProcessEvent,
) -> thread::JoinHandle<()>
where
    R: Read + Send + 'static,
{
    thread::spawn(move || {
        let mut buf = [0_u8; 8192];
        loop {
            match reader.read(&mut buf) {
                Ok(0) => break,
                Ok(n) => {
                    if tx.send(wrap(buf[..n].to_vec())).is_err() {
                        break;
                    }
                }
                Err(err) => {
                    send_error(&tx, err);
                    break;
                }
            }
        }
    })
}

fn join_optional(handle: Option<thread::JoinHandle<()>>) {
    if let Some(handle) = handle {
        let _ = handle.join();
    }
}

fn send_error(tx: &mpsc::Sender<ProcessEvent>, err: io::Error) {
    let _ = tx.send(ProcessEvent::Error(Arc::from(err.to_string())));
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn process_spec_builder_sets_fields() {
        let spec = ProcessSpec::new("sh")
            .arg("-c")
            .args(["printf", "ok"])
            .cwd("/")
            .env("KEY", "VALUE")
            .stdin(b"input".to_vec());

        assert_eq!(spec.program(), "sh");
        assert_eq!(spec.args_slice().len(), 3);
        assert_eq!(spec.args_slice()[0].as_ref(), "-c");
        assert_eq!(spec.cwd_path(), Some(Path::new("/")));
        assert_eq!(spec.env_slice()[0].0.as_ref(), "KEY");
        assert_eq!(spec.env_slice()[0].1.as_ref(), "VALUE");
        assert_eq!(spec.stdin_bytes(), Some(b"input".as_slice()));
    }

    #[cfg(unix)]
    #[test]
    fn streams_stdout_stderr_and_exit() {
        let spec = ProcessSpec::new("sh").args(["-c", "printf out; printf err >&2"]);
        let mut events = Vec::new();

        spec.stream(|event| events.push(event)).unwrap();

        let stdout: Vec<u8> = events
            .iter()
            .filter_map(|event| match event {
                ProcessEvent::Stdout(bytes) => Some(bytes.as_slice()),
                _ => None,
            })
            .flatten()
            .copied()
            .collect();
        let stderr: Vec<u8> = events
            .iter()
            .filter_map(|event| match event {
                ProcessEvent::Stderr(bytes) => Some(bytes.as_slice()),
                _ => None,
            })
            .flatten()
            .copied()
            .collect();

        assert_eq!(stdout, b"out");
        assert_eq!(stderr, b"err");
        assert!(matches!(events.last(), Some(ProcessEvent::Exited(status)) if status.success()));
    }

    #[cfg(unix)]
    #[test]
    fn stream_process_until_kills_child_when_cancelled() {
        let spec = ProcessSpec::new("sh").args(["-c", "sleep 5"]);
        let mut events = Vec::new();

        stream_process_until(spec, || true, |event| events.push(event)).unwrap();

        assert!(matches!(events.last(), Some(ProcessEvent::Exited(status)) if !status.success()));
    }
}