Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,22 @@ readme = "README.md"
requires-python = ">= 3.9"
license = "MIT"

[dependency-groups]
dev = [
"httpx",
# required to run the dictionary example
"textual-dev>=1.5.1",
]

[project.urls]
Homepage = "https://github.com/Textualize/textual-serve"


[build-system]
requires = ["hatchling>=1.26.1"]
build-backend = "hatchling.build"

[tool.hatch.metadata]
allow-direct-references = true

[tool.rye]
managed = true
dev-dependencies = [
"httpx",
# required to run the dictionary example
"textual-dev>=1.5.1",
]

[tool.hatch.build.targets.wheel]
packages = ["src/textual_serve"]
79 changes: 0 additions & 79 deletions requirements-dev.lock

This file was deleted.

56 changes: 0 additions & 56 deletions requirements.lock

This file was deleted.

47 changes: 41 additions & 6 deletions src/textual_serve/app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io
import json
import os
from typing import Awaitable, Callable, Literal
from typing import Awaitable, Callable, Literal, Optional
from asyncio.subprocess import Process
import logging

Expand Down Expand Up @@ -60,12 +60,20 @@ def stdin(self) -> asyncio.StreamWriter:
assert self._stdin is not None
return self._stdin

def _build_environment(self, width: int = 80, height: int = 24) -> dict[str, str]:
def _build_environment(
self,
width: int = 80,
height: int = 24,
cell_width: Optional[int] = None,
cell_height: Optional[int] = None,
) -> dict[str, str]:
"""Build an environment dict for the App subprocess.

Args:
width: Initial width.
height: Initial height.
cell_width: Width of a terminal cell in pixels.
cell_height: Height of a terminal cell in pixels.

Returns:
A environment dict.
Expand All @@ -78,19 +86,35 @@ def _build_environment(self, width: int = 80, height: int = 24) -> dict[str, str
environment["TERM_PROGRAM_VERSION"] = version("textual-serve")
environment["COLUMNS"] = str(width)
environment["ROWS"] = str(height)
if cell_width and cell_height:
environment["TEXTUAL_CELL_WIDTH"] = str(cell_width)
environment["TEXTUAL_CELL_HEIGHT"] = str(cell_height)
if self.debug:
environment["TEXTUAL"] = "debug,devtools"
environment["TEXTUAL_LOG"] = "textual.log"
return environment

async def _open_app_process(self, width: int = 80, height: int = 24) -> Process:
async def _open_app_process(
self,
width: int = 80,
height: int = 24,
cell_width: Optional[int] = None,
cell_height: Optional[int] = None,
) -> Process:
"""Open a process to run the app.

Args:
width: Width of the terminal.
height: height of the terminal.
cell_width: Width of a terminal cell in pixels.
cell_height: Height of a terminal cell in pixels.
"""
environment = self._build_environment(width=width, height=height)
environment = self._build_environment(
width=width,
height=height,
cell_width=cell_width,
cell_height=cell_height,
)
self._process = process = await asyncio.create_subprocess_shell(
self.command,
stdin=asyncio.subprocess.PIPE,
Expand Down Expand Up @@ -180,8 +204,19 @@ async def focus(self) -> None:
"""Send an (app) focus to the process."""
await self.send_meta({"type": "focus"})

async def start(self, width: int, height: int) -> None:
await self._open_app_process(width, height)
async def start(
self,
width: int,
height: int,
cell_width: Optional[int] = None,
cell_height: Optional[int] = None,
) -> None:
await self._open_app_process(
width,
height,
cell_width=cell_width,
cell_height=cell_height,
)
self._task = asyncio.create_task(self.run())

async def stop(self) -> None:
Expand Down
9 changes: 8 additions & 1 deletion src/textual_serve/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ async def handle_websocket(self, request: web.Request) -> web.WebSocketResponse:

width = to_int(request.query.get("width", "80"), 80)
height = to_int(request.query.get("height", "24"), 24)
cell_width = to_int(request.query.get("cellWidth", "0"), 0)
cell_height = to_int(request.query.get("cellHeight", "0"), 0)

app_service: AppService | None = None
try:
Expand All @@ -334,7 +336,12 @@ async def handle_websocket(self, request: web.Request) -> web.WebSocketResponse:
download_manager=self.download_manager,
debug=self.debug,
)
await app_service.start(width, height)
await app_service.start(
width,
height,
cell_width=cell_width,
cell_height=cell_height,
)
try:
await self._process_messages(websocket, app_service)
finally:
Expand Down
Loading