-
Notifications
You must be signed in to change notification settings - Fork 814
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from Textualize/text-input-cursor-to-click
Text input improvements
- v1.0.0
- v0.89.1
- v0.89.0
- v0.88.1
- v0.88.0
- v0.87.1
- v0.87.0
- v0.86.3
- v0.86.2
- v0.86.1
- v0.86.0
- v0.85.2
- v0.85.1
- v0.85.0
- v0.84.0
- v0.83.0
- v0.82.0
- v0.81.0
- v0.80.1
- v0.80.0
- v0.79.1
- v0.79.0
- v0.78.0
- v0.77.0
- v0.76.0
- v0.75.1
- v0.75.0
- v0.74.0
- v0.73.0
- v0.72.0
- v0.71.1
- v0.70.0
- v0.69.0
- v0.68.0
- v0.67.1
- v0.67.0
- v0.66.0
- v0.65.2
- v0.65.1
- v0.65.0
- v0.64.0
- v0.63.6
- v0.63.5
- v0.63.4
- v0.63.3
- v0.63.2
- v0.63.1
- v0.63.0
- v0.62.0
- v0.61.1
- v0.61.0
- v0.60.1
- v0.60.0
- v0.59.0
- v0.58.1
- v0.58.0
- v0.57.1
- v0.57.0
- v0.56.4
- v0.56.3
- v0.56.2
- v0.56.1
- v0.56.0
- v0.55.1
- v0.55.0
- v0.54.0
- v0.53.1
- v0.53.0
- v0.52.1
- v0.52.0
- v0.51.0
- v0.50.1
- v0.50.0
- v0.49.0
- v0.48.2
- v0.48.1
- v0.48.0
- v0.47.1
- v0.47.0
- v0.46.0
- v0.45.1
- v0.45.0
- v0.44.1
- v0.44.0
- v0.43.2
- v0.43.1
- v0.43.0
- v0.42.0
- v0.41.0
- v0.40.0
- v0.39.0
- v0.38.1
- v0.38.0
- v0.37.1
- v0.37.0
- v0.36.0
- v0.35.1
- v0.35.0
- v0.34.0
- v0.33.0
- v0.32.0
- v0.31.0
- v0.30.0
- v0.29.0
- v0.28.1
- v0.28.0
- v0.27.0
- v0.26.0
- v0.25.0
- v0.24.1
- v0.24.0
- v0.23.0
- v0.22.3
- v0.22.2
- v0.22.1
- v0.22.0
- v0.21.0
- v0.20.1
- v0.20.0
- v0.19.1
- v0.19.0
- v0.18.0
- v0.17.3
- v0.17.2
- v0.17.1
- v0.17.0
- v0.16.0
- v0.15.1
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.1
- v0.12.0
- v0.11.1
- v0.11.0
- v0.10.1
- v0.10.0
- v0.9.1
- v0.9.0
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.0
- v0.4.0
- v0.3.0
- v0.2.1
- v0.2.0
Showing
6 changed files
with
344 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
from typing import Iterable | ||
|
||
from rich.console import RenderableType | ||
from rich.style import Style | ||
from rich.table import Table | ||
from rich.text import Text | ||
|
||
from textual.app import App | ||
from textual.geometry import Size | ||
from textual.reactive import Reactive | ||
from textual.widget import Widget | ||
from textual.widgets.text_input import TextInput, TextWidgetBase | ||
|
||
|
||
def get_files() -> list[Path]: | ||
files = list(Path.cwd().iterdir()) | ||
return files | ||
|
||
|
||
class FileTable(Widget): | ||
filter = Reactive("", layout=True) | ||
|
||
def __init__(self, *args, files: Iterable[Path] | None = None, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.files = files if files is not None else [] | ||
|
||
@property | ||
def filtered_files(self) -> list[Path]: | ||
return [ | ||
file | ||
for file in self.files | ||
if self.filter == "" or (self.filter and self.filter in file.name) | ||
] | ||
|
||
def get_content_height(self, container: Size, viewport: Size, width: int) -> int: | ||
return len(self.filtered_files) | ||
|
||
def render(self, style: Style) -> RenderableType: | ||
grid = Table.grid() | ||
grid.add_column() | ||
for file in self.filtered_files: | ||
file_text = Text(f" {file.name}") | ||
if self.filter: | ||
file_text.highlight_regex(self.filter, "black on yellow") | ||
grid.add_row(file_text) | ||
return grid | ||
|
||
|
||
class FileSearchApp(App): | ||
dark = True | ||
|
||
def on_mount(self) -> None: | ||
self.file_table = FileTable(id="file_table", files=list(Path.cwd().iterdir())) | ||
self.search_bar = TextInput(placeholder="Search for files...") | ||
self.search_bar.focus() | ||
self.mount(file_table_wrapper=Widget(self.file_table)) | ||
self.mount(search_bar=self.search_bar) | ||
|
||
def handle_changed(self, event: TextWidgetBase.Changed) -> None: | ||
self.file_table.filter = event.value | ||
|
||
|
||
app = FileSearchApp( | ||
log_path="textual.log", css_path="file_search.scss", watch_css=True, log_verbosity=2 | ||
) | ||
|
||
if __name__ == "__main__": | ||
result = app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Screen { | ||
layout: dock; | ||
docks: top=top bottom=bottom; | ||
} | ||
|
||
#file_table_wrapper { | ||
dock: bottom; | ||
height: auto; | ||
overflow: auto auto; | ||
scrollbar-color: $accent-darken-1; | ||
} | ||
|
||
#file_table { | ||
height: auto; | ||
} | ||
|
||
#search_bar { | ||
dock: bottom; | ||
background: $accent; | ||
height: 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters