runandlog 0.3.3

CLI / TUI that runs the shell commands in a Markdown file and writes the results back
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
//! Entry point of the Run and Log CLI / TUI.

use std::path::PathBuf;
use std::process::ExitCode;
#[cfg(unix)]
use std::sync::atomic::{AtomicI32, Ordering};
use std::time::Duration;

use clap::Parser;
use runandlog::session::Session;
use runandlog::tui;
use runandlog_core::{Canceller, ExecOptions};

/// Exit code for a run stopped by a signal, following the shell convention of
/// 128 + the signal number: 130 for SIGINT (Ctrl-C), 143 for SIGTERM.
fn signal_exit_code(signal: i32) -> u8 {
    (128 + signal) as u8
}

/// Runs the shell commands written in a Markdown file and writes the results
/// back into the same file.
#[derive(Parser, Debug)]
#[command(name = "runandlog", version, about, long_about = None)]
struct Args {
    /// The Markdown file to work on.
    file: PathBuf,

    /// Open in the desktop app (GUI).
    #[arg(short, long)]
    gui: bool,

    /// Print the list of cells and exit.
    #[arg(short, long)]
    list: bool,

    /// Run only the cell with this number (1-based). May be repeated.
    #[arg(short, long, value_name = "N")]
    run: Vec<usize>,

    /// Run every cell in order.
    #[arg(short = 'a', long)]
    run_all: bool,

    /// Write output to a separate file once it exceeds this many lines.
    #[arg(long, value_name = "N", default_value_t = 50)]
    max_inline_lines: usize,

    /// Shell the commands are handed to. Defaults to the SHELL environment variable.
    #[arg(long, value_name = "PATH")]
    shell: Option<PathBuf>,

    /// Working directory for the commands. Defaults to the directory of the Markdown file.
    #[arg(long, value_name = "DIR")]
    cwd: Option<PathBuf>,

    /// Time limit per cell, in seconds. Unlimited by default.
    #[arg(long, value_name = "SECONDS")]
    timeout: Option<u64>,
}

fn main() -> ExitCode {
    let args = Args::parse();
    match dispatch(args) {
        Ok(code) => code,
        Err(error) => {
            eprintln!("runandlog: {error}");
            // A run that was interrupted still exits by its signal, even when what
            // ended it was the write-back failing rather than the loop finishing.
            // Reporting a plain failure would hide the interruption from a job
            // runner reading the code.
            if interrupt_requested() {
                ExitCode::from(interrupt_exit_code())
            } else {
                ExitCode::FAILURE
            }
        }
    }
}

fn dispatch(args: Args) -> std::io::Result<ExitCode> {
    let mut session = Session::load(&args.file, exec_options(&args), args.max_inline_lines)?;

    if args.gui {
        // The GUI is checked before the other flags on purpose: --gui is a request
        // for a window, and quietly doing something else instead would be worse
        // than refusing.
        return open_gui(session).map(|()| ExitCode::SUCCESS);
    }

    if args.list {
        print_list(&session);
        return Ok(ExitCode::SUCCESS);
    }

    // Whether to run non-interactively is decided by the flags, not by how many
    // targets they happen to select. `--run-all` on a file with no cells is a
    // request to run nothing, not a request for the TUI -- opening it would hang a
    // script that has no terminal.
    if !args.run_all && args.run.is_empty() {
        tui::run(session)?;
        return Ok(ExitCode::SUCCESS);
    }
    let targets = targets(&args, &session)?;

    let canceller = Canceller::new();
    catch_interrupts(&canceller);

    let mut failed = false;
    for index in targets {
        // Asked before the cell starts as well as after it ends. A signal that lands
        // while the previous result is being written back would otherwise start this
        // cell only for the cancellation to kill it moments later -- and its empty
        // "cancelled" result would replace the result this cell already had, for a
        // run nobody asked for.
        //
        // What is checked is the flag the handler itself sets, not the canceller: the
        // thread that turns one into the other sleeps between looks, so for up to a
        // poll interval after the signal the canceller still says nothing happened.
        if interrupt_requested() {
            break;
        }
        let cell = &session.doc().cells[index];
        println!("[{}] {}", index + 1, first_line(&cell.command));
        // Printed as it arrives rather than once the command has finished. A run
        // that takes minutes otherwise looks like a hung terminal, and the output
        // that would have said what it is doing only appears once it no longer
        // matters. The chunks add up to `outcome.output`, so nothing is printed
        // twice by not printing that afterwards.
        let outcome = session.run_cell_streaming(index, &canceller, print_as_it_arrives())?;
        println!("--- {}", outcome.status_text());
        failed |= !outcome.is_success();
        if interrupt_requested() {
            // The result of the cell that was interrupted has been written back;
            // going on to the next one is not what Ctrl-C asked for.
            break;
        }
    }
    // Asked once more rather than remembered from the loop: a signal landing after
    // the last cell still stopped this run, and reporting success for it would tell
    // a job runner the opposite of what happened.
    if interrupt_requested() {
        eprintln!("runandlog: interrupted");
        return Ok(ExitCode::from(interrupt_exit_code()));
    }
    Ok(if failed {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    })
}

/// A printer for a running command's output, showing each piece as it arrives.
///
/// Flushed by hand: stdout is only line-buffered when it is a terminal, and even
/// then a command printing a prompt or a progress line without a newline would sit
/// in the buffer -- which is the very output a live view exists to show. Redirected
/// to a file it is block-buffered, and nothing would appear until the block filled.
///
/// Written through `write_all` rather than `print!`, and silenced for the rest of
/// the run once stdout refuses a write. The macro panics on a closed stdout -- a run
/// piped into `head`, say -- and this callback is called from inside the run, so the
/// panic would come back out of it before the finished result could be written into
/// the Markdown. By then the output on screen is going nowhere anyway; the file must
/// still get it.
fn print_as_it_arrives() -> impl FnMut(&str) + Send {
    printer(std::io::stdout())
}

/// The body of [`print_as_it_arrives`], with the destination passed in so that what
/// it does when writing fails can be tested without closing the real stdout.
fn printer(mut out: impl std::io::Write + Send) -> impl FnMut(&str) + Send {
    let mut printable = true;
    move |chunk| {
        if !printable {
            return;
        }
        if out
            .write_all(chunk.as_bytes())
            .and_then(|()| out.flush())
            .is_err()
        {
            printable = false;
        }
    }
}

/// Turns Ctrl-C into a request to stop the running command.
///
/// The command is spawned into a process group of its own, so the SIGINT the
/// terminal delivers to the foreground group reaches this process alone. Left at
/// its default, that would kill runandlog and leave the command running with
/// nothing written back -- exactly the output the user was waiting for. So the
/// signal is caught, the command is killed through `canceller`, and the partial
/// result still goes into the Markdown.
///
/// SIGTERM is treated the same way: it also means "stop", and the command would
/// otherwise be orphaned just as thoroughly.
#[cfg(unix)]
fn catch_interrupts(canceller: &Canceller) {
    extern "C" fn on_signal(signal: libc::c_int) {
        // `Canceller::cancel` cannot be called from here at all: it takes a lock, and
        // a signal that interrupts the polling thread while that thread holds the
        // lock would leave the handler waiting on itself. Raising a flag for the
        // thread below is the only thing a handler can safely leave behind.
        if INTERRUPTED_BY.swap(signal, Ordering::SeqCst) != 0 {
            // Already interrupted once and still here: the user is asking again, so
            // stop without waiting for the write-back.
            // SAFETY: _exit is async-signal-safe, unlike a normal exit.
            unsafe { libc::_exit(signal_exit_code(signal) as libc::c_int) };
        }
    }

    // SAFETY: installing a handler that only touches an atomic and _exit.
    unsafe {
        let handler = on_signal as *const () as libc::sighandler_t;
        libc::signal(libc::SIGINT, handler);
        libc::signal(libc::SIGTERM, handler);
    }

    let canceller = canceller.clone();
    std::thread::spawn(move || {
        while INTERRUPTED_BY.load(Ordering::SeqCst) == 0 {
            std::thread::sleep(Duration::from_millis(50));
        }
        canceller.cancel();
    });
}

/// The signal that interrupted the run, or zero while none has arrived.
///
/// Kept rather than a plain flag so the exit code can say *which* signal stopped
/// the run: a job runner that sent SIGTERM should not be told the run was
/// interrupted at a keyboard.
#[cfg(unix)]
static INTERRUPTED_BY: AtomicI32 = AtomicI32::new(0);

/// Whether a signal asking the run to stop has arrived.
///
/// Read straight from what the handler sets, so that it is true the moment the
/// signal lands rather than once the watching thread has noticed.
#[cfg(unix)]
fn interrupt_requested() -> bool {
    INTERRUPTED_BY.load(Ordering::SeqCst) != 0
}

/// Exit code for the interrupted run.
#[cfg(unix)]
fn interrupt_exit_code() -> u8 {
    signal_exit_code(INTERRUPTED_BY.load(Ordering::SeqCst))
}

/// Nothing interrupts a run off unix, so these are only here to keep the caller
/// compiling.
#[cfg(not(unix))]
fn interrupt_requested() -> bool {
    false
}

#[cfg(not(unix))]
fn interrupt_exit_code() -> u8 {
    signal_exit_code(2)
}

/// Off unix the signal handling above has no equivalent, and `exec` cannot kill a
/// process group there either. Ctrl-C keeps its default meaning.
#[cfg(not(unix))]
fn catch_interrupts(_canceller: &Canceller) {}

/// Opens the desktop app.
///
/// Built only when the `gui` feature is on. Without it the binary still accepts
/// `--gui` so that the message explains what happened, rather than clap reporting
/// an unknown flag.
#[cfg(feature = "gui")]
fn open_gui(session: Session) -> std::io::Result<()> {
    runandlog::gui::run(session)
}

#[cfg(not(feature = "gui"))]
fn open_gui(_session: Session) -> std::io::Result<()> {
    Err(std::io::Error::other(
        "this build has no GUI; rebuild with the \"gui\" feature to use --gui",
    ))
}

fn exec_options(args: &Args) -> ExecOptions {
    let cwd = args.cwd.clone().unwrap_or_else(|| {
        // Resolve first: Session::load canonicalizes the document, so with a symlink
        // argument the directory "containing the Markdown file" is the one holding
        // the real file, not the one holding the link.
        let file = args
            .file
            .canonicalize()
            .unwrap_or_else(|_| args.file.clone());
        file.parent()
            .filter(|parent| !parent.as_os_str().is_empty())
            .map(PathBuf::from)
            .unwrap_or_else(|| PathBuf::from("."))
    });
    let mut options = ExecOptions::new(cwd);
    if let Some(shell) = &args.shell {
        options.shell = shell.clone();
    }
    options.timeout = args.timeout.map(Duration::from_secs);
    options
}

/// Decides which cells to run, as 0-based indices.
///
/// Only called once the flags have established that this is a non-interactive
/// run, so an empty result means "run nothing", not "open the TUI".
fn targets(args: &Args, session: &Session) -> std::io::Result<Vec<usize>> {
    if args.run_all {
        return Ok((0..session.len()).collect());
    }
    let mut targets = Vec::new();
    for number in &args.run {
        if *number == 0 || *number > session.len() {
            return Err(std::io::Error::other(format!(
                "no such cell: {number} (the file has {} cells)",
                session.len()
            )));
        }
        targets.push(number - 1);
    }
    Ok(targets)
}

fn print_list(session: &Session) {
    if session.is_empty() {
        println!("no runnable cells in {}", session.path().display());
        return;
    }
    for cell in &session.doc().cells {
        let out = match &cell.out_file {
            Some(path) => format!(" -> {path}"),
            None => String::new(),
        };
        println!(
            "[{}] {}{out}",
            cell.display_number(),
            first_line(&cell.command)
        );
    }
}

fn first_line(command: &str) -> String {
    let mut lines = command.lines().filter(|line| !line.trim().is_empty());
    let first = lines.next().unwrap_or("").trim().to_string();
    if lines.next().is_some() {
        return format!("{first} ...");
    }
    first
}

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

    #[test]
    fn the_exit_code_names_the_signal_that_stopped_the_run() {
        // A job runner that sent SIGTERM must not be told the run was interrupted at
        // a keyboard, so the two cannot share one code.
        assert_eq!(signal_exit_code(2), 130);
        assert_eq!(signal_exit_code(15), 143);
    }

    /// A destination that refuses everything after the first write, the way stdout
    /// does once the command it was piped into has gone.
    #[derive(Default)]
    struct ClosesAfterOneWrite {
        written: Vec<String>,
    }

    impl std::io::Write for ClosesAfterOneWrite {
        fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> {
            if !self.written.is_empty() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    "closed",
                ));
            }
            self.written
                .push(String::from_utf8_lossy(buffer).to_string());
            Ok(buffer.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    #[test]
    fn printing_gives_up_quietly_once_the_output_has_nowhere_to_go() {
        // `print!` panics when stdout is closed -- a run piped into `head`. This
        // callback is called from inside the run, so a panic would come back out of
        // it before the result could be written into the Markdown: the command would
        // have run for nothing. Once, so the rest of the run costs nothing either.
        let mut destination = ClosesAfterOneWrite::default();
        {
            let mut print = printer(&mut destination);
            print("first\n");
            print("second\n");
            print("third\n");
        }

        assert_eq!(destination.written, vec!["first\n"]);
    }

    #[test]
    fn shows_ellipsis_for_multi_line_commands() {
        assert_eq!(first_line("ls /opt\nls /tmp\n"), "ls /opt ...");
        assert_eq!(first_line("date\n"), "date");
        assert_eq!(first_line("\n\n"), "");
    }
}