From 4cae360ea5042dae7935612d24a5d2cda7503c93 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sat, 17 Feb 2024 18:59:06 +0000 Subject: [PATCH 1/4] pipe support --- src/toolong/cli.py | 52 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/toolong/cli.py b/src/toolong/cli.py index cc2018e..03e5811 100644 --- a/src/toolong/cli.py +++ b/src/toolong/cli.py @@ -1,6 +1,9 @@ from __future__ import annotations from importlib.metadata import version +import os +import sys + import click from toolong.ui import UI @@ -19,9 +22,52 @@ ) def run(files: list[str], merge: bool, output_merge: str) -> None: """View / tail / search log files.""" - if not files: + stdin_tty = sys.__stdin__.isatty() + if not files and stdin_tty: ctx = click.get_current_context() click.echo(ctx.get_help()) ctx.exit() - ui = UI(files, merge=merge, save_merge=output_merge) - ui.run() + if stdin_tty: + try: + ui = UI(files, merge=merge, save_merge=output_merge) + ui.run() + except Exception: + pass + else: + import signal + import selectors + import subprocess + import tempfile + + def request_exit(*args) -> None: + """Don't write anything when a signal forces an error.""" + sys.stderr.write("^C") + + signal.signal(signal.SIGINT, request_exit) + signal.signal(signal.SIGTERM, request_exit) + + with tempfile.NamedTemporaryFile( + mode="w+b", buffering=0, prefix="tl_" + ) as temp_file: + + with open("/dev/tty", "rb", buffering=0) as tty_stdin: + with subprocess.Popen( + [sys.argv[0], temp_file.name], + stdin=tty_stdin, + bufsize=0, + close_fds=True, + env={**os.environ, "TEXTUAL_ALLOW_SIGNALS": "1"}, + ) as process: + + selector = selectors.SelectSelector() + selector.register(sys.stdin.fileno(), selectors.EVENT_READ) + + while process.poll() is None: + for _, event in selector.select(0.1): + if process.poll() is not None: + break + if event & selectors.EVENT_READ: + if line := os.read(sys.stdin.fileno(), 1024 * 64): + temp_file.write(line) + else: + break From 039ccbabec9f206f44f705d52484bd74077a2540 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Sun, 18 Feb 2024 17:14:28 +0000 Subject: [PATCH 2/4] comments --- src/toolong/cli.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/toolong/cli.py b/src/toolong/cli.py index 03e5811..bd35e89 100644 --- a/src/toolong/cli.py +++ b/src/toolong/cli.py @@ -46,19 +46,22 @@ def request_exit(*args) -> None: signal.signal(signal.SIGINT, request_exit) signal.signal(signal.SIGTERM, request_exit) + # Write piped data to a temporary file with tempfile.NamedTemporaryFile( mode="w+b", buffering=0, prefix="tl_" ) as temp_file: + # Get input directly from /dev/tty to free up stdin with open("/dev/tty", "rb", buffering=0) as tty_stdin: + # Launch a new process to render the UI with subprocess.Popen( [sys.argv[0], temp_file.name], stdin=tty_stdin, - bufsize=0, close_fds=True, env={**os.environ, "TEXTUAL_ALLOW_SIGNALS": "1"}, ) as process: + # Current process copies from stdin to the temp file selector = selectors.SelectSelector() selector.register(sys.stdin.fileno(), selectors.EVENT_READ) From 9559796c5276023d35eb03f5bead21777dd63e70 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 19 Feb 2024 15:03:08 +0000 Subject: [PATCH 3/4] bump requirements --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3d430d7..e44495b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "toolong" -version = "1.2.2" +version = "1.2.3" description = "A terminal log file viewer / tailer / analyzer" authors = ["Will McGugan "] license = "MIT" @@ -13,7 +13,7 @@ documentation = "https://github.com/textualize/toolong" [tool.poetry.dependencies] python = "^3.8" click = "^8.1.7" -textual = "^0.50.0" +textual = "^0.51.0" typing-extensions = "^4.9.0" [tool.poetry.group.dev.dependencies] From 38754561484080232fa2c7bdd979e6442d973ff7 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 19 Feb 2024 16:22:38 +0000 Subject: [PATCH 4/4] bump --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e44495b..58d7b02 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "toolong" -version = "1.2.3" +version = "1.3.0" description = "A terminal log file viewer / tailer / analyzer" authors = ["Will McGugan "] license = "MIT" @@ -13,7 +13,7 @@ documentation = "https://github.com/textualize/toolong" [tool.poetry.dependencies] python = "^3.8" click = "^8.1.7" -textual = "^0.51.0" +textual = "^0.52.0" typing-extensions = "^4.9.0" [tool.poetry.group.dev.dependencies]