Skip to content

Commit 8404bd8

Browse files
authored
Merge pull request #23 from sirfuzzalot/feat/syntax-highlighting
feat: one-line syntax highlighting
2 parents fb31b5f + 28a4171 commit 8404bd8

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

README.md

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,23 @@ Textual Inputs is a collection of input widgets for the [Textual](https://github
1111
> Textual Inputs is pre-alpha please pin your projects to the minor release
1212
> number to avoid breaking changes. For example: textual-inputs=0.2.\*
1313
14-
---
15-
1614
## News
1715

18-
### v0.2.4
16+
### v0.2.5
1917

20-
Adds support for customizing the message handler names for on change and
21-
on focus events emitted by the inputs. Under the hood this will generate
22-
a `Message` class with the appropriate name for Textual to send it to
23-
the handler name provided. You'll then want add the handler to the input's
24-
parent or the App instance. If you opt not to customize these handlers,
25-
their values will be the default `handle_input_on_change` and `handle_input_on_focus`.
26-
See `examples/simple_form.py` for a working example.
18+
Adds support for syntax highlighting. To add syntax highlighting to your
19+
input text set the `syntax` argument to a language supported by
20+
`pygments`. Currently this is set to the default theme.
2721

2822
```python
29-
email = TextInput(name="email", title="Email")
30-
email.on_change_handler_name = "handle_email_on_change"
31-
email.on_focus_handler_name = "handle_email_on_focus"
23+
TextInput(
24+
name="code",
25+
placeholder="enter some python code...",
26+
title="Code",
27+
syntax="python",
28+
)
3229
```
3330

34-
---
35-
3631
## Quick Start
3732

3833
Installation
@@ -58,8 +53,6 @@ python -m pip install -e .
5853
python examples/simple_form.py
5954
```
6055

61-
---
62-
6356
## Widgets
6457

6558
### TextInput 🔡
@@ -68,6 +61,7 @@ python examples/simple_form.py
6861
- one line of text
6962
- placeholder and title support
7063
- password mode to hide input
64+
- syntax mode to highlight code
7165
- support for Unicode characters
7266
- controls: arrow right/left, home, end, delete, backspace/ctrl+h, escape
7367
- emits - InputOnChange, InputOnFocus messages
@@ -80,7 +74,45 @@ python examples/simple_form.py
8074
- controls: arrow right/left, home, end, delete, backspace/ctrl+h, escape
8175
- emits - InputOnChange, InputOnFocus messages
8276

83-
---
77+
## Features
78+
79+
### One-Line Syntax Highlighting
80+
81+
Textual Inputs takes advantage of `rich`'s built-in Syntax feature. To
82+
add highlighting to your input text set the `syntax` argument to a language
83+
supported by `pygments`. Currently this is set to the default theme.
84+
85+
**⚠️ THIS FEATURE IS LIMITED TO ONE LINE OF TEXT**
86+
87+
```python
88+
TextInput(
89+
name="code",
90+
placeholder="enter some python code...",
91+
title="Code",
92+
syntax="python",
93+
)
94+
```
95+
96+
### Event Handlers
97+
98+
Textual Inputs helps make the event handler process easier by providing
99+
the following convenient properties for inputs.
100+
101+
- on_change_handler_name
102+
- on_focus_handler_name
103+
104+
```python
105+
email = TextInput(name="email", title="Email")
106+
email.on_change_handler_name = "handle_email_on_change"
107+
email.on_focus_handler_name = "handle_email_on_focus"
108+
```
109+
110+
Under the hood setting this attribute this will generate a `Message` class
111+
with the appropriate name for Textual to send it to the handler name provided.
112+
You'll then want add the handler to the input's parent or the App instance.
113+
If you opt not to customize these handlers, their values will be the
114+
default `handle_input_on_change` and `handle_input_on_focus`. See
115+
`examples/simple_form.py` for a working example.
84116

85117
## API Reference
86118

@@ -101,12 +133,14 @@ class TextInput(Widget):
101133
of the widget's border.
102134
password (bool, optional): Defaults to False. Hides the text
103135
input, replacing it with bullets.
136+
syntax (Optional[str]): the name of the language for syntax highlighting.
104137
105138
Attributes:
106139
value (str): the value of the text field
107140
placeholder (str): The placeholder message.
108141
title (str): The displayed title of the widget.
109142
has_password (bool): True if the text field masks the input.
143+
syntax (Optional[str]): the name of the language for syntax highlighting.
110144
has_focus (bool): True if the widget is focused.
111145
cursor (Tuple[str, Style]): The character used for the cursor
112146
and a rich Style object defining its appearance.

examples/simple_form.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,23 @@ async def on_mount(self) -> None:
109109
title="Age",
110110
)
111111
self.age.on_change_handler_name = "handle_age_on_change"
112-
112+
113113
self.code = TextInput(
114114
name="code",
115115
placeholder="enter some python code...",
116116
title="Code",
117117
syntax="python",
118118
)
119-
self.code.on_change_handler_name = "handle_code_on_change"
120119

121120
self.output = Static(
122121
renderable=Panel(
123122
"", title="Report", border_style="blue", box=rich.box.SQUARE
124123
)
125124
)
126125
await self.view.dock(self.output, edge="left", size=40)
127-
await self.view.dock(self.username, self.password, self.age, self.code, edge="top")
126+
await self.view.dock(
127+
self.username, self.password, self.age, self.code, edge="top"
128+
)
128129

129130
async def action_next_tab_index(self) -> None:
130131
"""Changes the focus to the next form field"""
@@ -134,7 +135,6 @@ async def action_next_tab_index(self) -> None:
134135

135136
async def action_previous_tab_index(self) -> None:
136137
"""Changes the focus to the previous form field"""
137-
self.log(f"PREVIOUS {self.current_index}")
138138
if self.current_index > 0:
139139
self.current_index -= 1
140140
await getattr(self, self.tab_index[self.current_index]).focus()
@@ -165,4 +165,4 @@ async def handle_input_on_focus(self, message: Message) -> None:
165165

166166

167167
if __name__ == "__main__":
168-
SimpleForm.run(title="Textual-Inputs Demo", log="textual.log")
168+
SimpleForm.run(title="Textual-Inputs Simple Form", log="textual.log")

src/textual_inputs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .integer_input import IntegerInput
22
from .text_input import TextInput
33

4-
__version__ = "0.2.4"
4+
__version__ = "0.2.5"
55

66
__all__ = ["IntegerInput", "TextInput"]

0 commit comments

Comments
 (0)