Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to give an initial pattern #7

Merged
merged 2 commits into from
Feb 9, 2024
Merged
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
BUMP_PART ?=

PYTEST_COV ?= xml
setup:
python3 -m pip install poetry

install:
poetry install

test:
poetry run pytest -ssv --cov --cov-report=xml
poetry run pytest -ssv --cov --cov-report=$(PYTEST_COV)

build:
poetry build
Expand Down
13 changes: 11 additions & 2 deletions rexi/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ def rexi_cli(
input_file: Annotated[
Optional[typer.FileText],
typer.Option(
"--input", "-i",
"--input",
"-i",
help="Input file to pass to rexi; if not provided, stdin will be used.",
),
] = None,
initial_pattern: Annotated[
Optional[str],
typer.Option(
"--pattern",
"-p",
help="Initial regex pattern to use",
),
] = None,
) -> None:
if input_file:
input_text = input_file.read()
Expand All @@ -29,5 +38,5 @@ def rexi_cli(
except OSError:
pass
sys.stdin = open("/dev/tty", "rb") # type: ignore[assignment]
app: RexiApp[int] = RexiApp(input_text)
app: RexiApp[int] = RexiApp(input_text, initial_pattern=initial_pattern)
app.run()
10 changes: 8 additions & 2 deletions rexi/rexi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ def __repr__(self) -> str:
class RexiApp(App[ReturnType]):
CSS_PATH = "rexi.tcss"

def __init__(self, input_content: str, start_mode: str = "finditer"):
def __init__(
self,
input_content: str,
start_mode: str = "finditer",
initial_pattern: Optional[str] = None,
):
super().__init__()
self.input_content: str = input_content
self.regex_modes: list[str] = ["finditer", "match"]
self.regex_current_mode: str = start_mode
self.initial_pattern = initial_pattern

def compose(self) -> ComposeResult:
with Horizontal(id="inputs"):
yield Input(placeholder="Enter regex pattern")
yield Input(value=self.initial_pattern, placeholder="Enter regex pattern")
yield Select(
zip(self.regex_modes, self.regex_modes),
id="select",
Expand Down
22 changes: 18 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rexi.cli import app


def test_stdin_cli(monkeypatch: MonkeyPatch) -> None:
def test_on_args(monkeypatch: MonkeyPatch) -> None:
"""
Couldn't find a better way to test the CLI without patching everything :(
"""
Expand All @@ -23,11 +23,11 @@ def test_stdin_cli(monkeypatch: MonkeyPatch) -> None:
monkeypatch.setattr("builtins.open", open_mock)
runner.invoke(app, input=a)
open_mock.assert_called_once_with("/dev/tty", "rb")
class_mock.assert_called_once_with(text.decode())
class_mock.assert_called_once_with(text.decode(), initial_pattern=None)
instance_mock.run.assert_called_once()


def test_file_input_cli(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
def test_file_input(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
runner = CliRunner()
text = "This iS! aTe xt2 F0r T3sT!ng"
(tmp_path / "text_file").write_text(text)
Expand All @@ -37,4 +37,18 @@ def test_file_input_cli(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
class_mock.return_value = instance_mock
monkeypatch.setattr("rexi.cli.RexiApp", class_mock)
runner.invoke(app, args=["-i", str(tmp_path / "text_file")])
class_mock.assert_called_once_with(text)
class_mock.assert_called_once_with(text, initial_pattern=None)


def test_initial_pattern(monkeypatch: MonkeyPatch, tmp_path: Path) -> None:
runner = CliRunner()
text = "This iS! aTe xt2 F0r T3sT!ng"
(tmp_path / "text_file").write_text(text)
class_mock = Mock()
instance_mock = Mock()
with monkeypatch.context():
class_mock.return_value = instance_mock
monkeypatch.setattr("rexi.cli.RexiApp", class_mock)
runner.invoke(app, args=["-i", str(tmp_path / "text_file"), "--pattern", "wtf"])
class_mock.assert_called_once_with(text, initial_pattern="wtf")

12 changes: 12 additions & 0 deletions tests/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ async def test_input_box(start_mode: str, pattern: str, expected_output: str) ->
assert result == expected_output


async def test_input_box_with_initial_pattern() -> None:
app: RexiApp[int] = RexiApp(
"This iS! aTe xt2 F0r T3sT!ng", initial_pattern="(This.*iS!)"
)
async with app.run_test():
result = str(cast(Static, app.query_one("#output")).renderable)
assert (
result
== f"{UNDERLINE}{Fore.RED}This iS!{Fore.RESET}{RESET_UNDERLINE} aTe xt2 F0r T3sT!ng"
)


async def test_switch_modes() -> None:
app: RexiApp[int] = RexiApp("This iS! aTe xt2 F0r T3sT!ng")
async with app.run_test() as pilot:
Expand Down
Loading