|
21 | 21 | use_expanded_output = False |
22 | 22 | PAGER_ENABLED = True |
23 | 23 | tee_file = None |
24 | | -once_file = once_file_args = written_to_once_file = None |
| 24 | +once_file = None |
| 25 | +written_to_once_file = None |
| 26 | +pipe_once_process = None |
| 27 | +written_to_pipe_once_process = False |
25 | 28 | favoritequeries = FavoriteQueries(ConfigObj()) |
26 | 29 |
|
27 | 30 |
|
@@ -376,36 +379,79 @@ def write_tee(output): |
376 | 379 | aliases=("\\o", "\\once"), |
377 | 380 | ) |
378 | 381 | def set_once(arg, **_): |
379 | | - global once_file_args |
380 | | - |
381 | | - once_file_args = parseargfile(arg) |
| 382 | + global once_file, written_to_once_file |
| 383 | + try: |
| 384 | + once_file = open(**parseargfile(arg)) |
| 385 | + except (IOError, OSError) as e: |
| 386 | + raise OSError("Cannot write to file '{}': {}".format(e.filename, e.strerror)) |
| 387 | + written_to_once_file = False |
382 | 388 |
|
383 | 389 | return [(None, None, None, "")] |
384 | 390 |
|
385 | 391 |
|
386 | 392 | @export |
387 | 393 | def write_once(output): |
388 | 394 | global once_file, written_to_once_file |
389 | | - if output and once_file_args: |
390 | | - if once_file is None: |
391 | | - try: |
392 | | - once_file = open(**once_file_args) |
393 | | - except (IOError, OSError) as e: |
394 | | - once_file = None |
395 | | - raise OSError("Cannot write to file '{}': {}".format(e.filename, e.strerror)) |
396 | | - |
| 395 | + if output and once_file: |
397 | 396 | click.echo(output, file=once_file, nl=False) |
398 | 397 | click.echo("\n", file=once_file, nl=False) |
| 398 | + once_file.flush() |
399 | 399 | written_to_once_file = True |
400 | 400 |
|
401 | 401 |
|
402 | 402 | @export |
403 | 403 | def unset_once_if_written(): |
404 | 404 | """Unset the once file, if it has been written to.""" |
405 | | - global once_file, once_file_args, written_to_once_file |
| 405 | + global once_file, written_to_once_file |
406 | 406 | if once_file and written_to_once_file: |
407 | 407 | once_file.close() |
408 | | - once_file = once_file_args = written_to_once_file = None |
| 408 | + once_file = written_to_once_file = None |
| 409 | + |
| 410 | + |
| 411 | +@special_command("\\pipe_once", "\\| command", "Send next result to a subprocess.", aliases=("\\|",)) |
| 412 | +def set_pipe_once(arg, **_): |
| 413 | + global pipe_once_process, written_to_pipe_once_process |
| 414 | + pipe_once_cmd = shlex.split(arg) |
| 415 | + if len(pipe_once_cmd) == 0: |
| 416 | + raise OSError("pipe_once requires a command") |
| 417 | + written_to_pipe_once_process = False |
| 418 | + pipe_once_process = subprocess.Popen( |
| 419 | + pipe_once_cmd, |
| 420 | + stdin=subprocess.PIPE, |
| 421 | + stdout=subprocess.PIPE, |
| 422 | + stderr=subprocess.PIPE, |
| 423 | + bufsize=1, |
| 424 | + encoding="UTF-8", |
| 425 | + universal_newlines=True, |
| 426 | + ) |
| 427 | + return [(None, None, None, "")] |
| 428 | + |
| 429 | + |
| 430 | +@export |
| 431 | +def write_pipe_once(output): |
| 432 | + global pipe_once_process, written_to_pipe_once_process |
| 433 | + if output and pipe_once_process: |
| 434 | + try: |
| 435 | + click.echo(output, file=pipe_once_process.stdin, nl=False) |
| 436 | + click.echo("\n", file=pipe_once_process.stdin, nl=False) |
| 437 | + except (IOError, OSError) as e: |
| 438 | + pipe_once_process.terminate() |
| 439 | + raise OSError("Failed writing to pipe_once subprocess: {}".format(e.strerror)) |
| 440 | + written_to_pipe_once_process = True |
| 441 | + |
| 442 | + |
| 443 | +@export |
| 444 | +def unset_pipe_once_if_written(): |
| 445 | + """Unset the pipe_once cmd, if it has been written to.""" |
| 446 | + global pipe_once_process, written_to_pipe_once_process |
| 447 | + if written_to_pipe_once_process: |
| 448 | + (stdout_data, stderr_data) = pipe_once_process.communicate() |
| 449 | + if len(stdout_data) > 0: |
| 450 | + print(stdout_data.rstrip("\n")) |
| 451 | + if len(stderr_data) > 0: |
| 452 | + print(stderr_data.rstrip("\n")) |
| 453 | + pipe_once_process = None |
| 454 | + written_to_pipe_once_process = False |
409 | 455 |
|
410 | 456 |
|
411 | 457 | @special_command( |
|
0 commit comments