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

feat: Add text input validators for numerical parameter types #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions trogon/widgets/parameter_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import click
from rich.text import Text
from textual import log, on
from textual import on
from textual.app import ComposeResult
from textual.containers import Vertical, Horizontal
from textual.css.query import NoMatches
from textual.validation import Integer, Number, Validator
from textual.widget import Widget
from textual.widgets import (
RadioButton,
Label,
Checkbox,
Input,
Expand Down Expand Up @@ -333,6 +333,20 @@ def get_control_method(
else:
return self.make_text_control

@staticmethod
def _make_text_validators(
schema: OptionSchema | ArgumentSchema,
) -> Iterable[Validator]:
# `IntParamType` goes first as `IntRange` inherits `IntParamType`.
if isinstance(schema.type, click.types.IntParamType):
yield Integer()

if isinstance(schema.type, (click.IntRange, click.FloatRange)):
yield Number(minimum=schema.type.min, maximum=schema.type.max)

if isinstance(schema.type, click.types.FloatParamType):
yield Number()

@staticmethod
def make_text_control(
default: Any,
Expand All @@ -343,6 +357,7 @@ def make_text_control(
) -> Widget:
control = Input(
classes=f"command-form-input {control_id}",
validators=ParameterControls._make_text_validators(schema),
)
yield control
return control
Expand Down