Skip to content

ProcessShell

Base classes

Name Children Inherits
Shell
acptui.shell
Runs the user's commands one at a time; subclasses decide how.

⋔ Inheritance diagram

graph TD
  4263840580["shell.ProcessShell"]
  227581314["shell.Shell"]
  2751680898["builtins.object"]
  227581314 --> 4263840580
  2751680898 --> 227581314

🛈 DocStrings

Bases: Shell

One process per command (Windows, or no bash / zsh): nothing carries over.

Source code in src/acptui/shell.py
class ProcessShell(Shell):
    """One process per command (Windows, or no bash / zsh): nothing carries over."""

    def __init__(self, cwd: str | Path, on_output: OutputCallback | None) -> None:
        super().__init__(cwd, on_output)
        self._process: asyncio.subprocess.Process | None = None

    async def run(self, command: str) -> ShellCommand:
        if self.busy:
            msg = "A command is still running"
            raise RuntimeError(msg)
        entry = ShellCommand(command, self.cwd)
        self.current = entry
        try:
            process = await asyncio.create_subprocess_shell(
                command,
                stdin=asyncio.subprocess.PIPE,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.STDOUT,
                cwd=self.cwd,
                env={**os.environ, **SHELL_ENV},
            )
        except OSError as exc:
            entry.append(f"{exc}\n".encode())
            self._finish(entry, -1)
            return entry
        self._process = process
        asyncio.get_running_loop().create_task(self._pump(entry, process))
        return entry

    async def _pump(self, entry: ShellCommand, process: asyncio.subprocess.Process) -> None:
        assert process.stdout is not None
        while chunk := await process.stdout.read(64 * 1024):
            entry.append(chunk)
            self._notify(entry)
        code = await process.wait()
        self._process = None
        self._finish(entry, code)

    async def send_input(self, text: str) -> None:
        process = self._process
        if process is not None and process.stdin is not None:
            with contextlib.suppress(OSError):
                process.stdin.write(text.rstrip("\n").encode() + b"\n")
                await process.stdin.drain()

    async def interrupt(self) -> None:
        process = self._process
        if process is not None and process.returncode is None:
            with contextlib.suppress(ProcessLookupError):
                process.terminate()

    def close(self) -> None:
        process = self._process
        if process is not None and process.returncode is None:
            with contextlib.suppress(ProcessLookupError):
                process.kill()
        self._process = None

:fa-brands-github: Show source on GitHub