Comments (4)
Hey there!
I'm struggling to understand exactly what the underlying issue seems to be, maybe some sort of MRE would be helpful? Something with code that I can run.
I say this because I created a silly subclass of DirectoryTree
that changes the colour of files/folders that have an a
in the name and that custom colour doesn't go away when I click those files/folders or when I navigate the directory tree.
The recording below shows said directory tree in action where all files/folders with an a
in the name are coloured green:
Screen.Recording.2024-01-18.at.14.59.28.mov
Code
from textual.app import App, ComposeResult
from textual.widgets import DirectoryTree
class MyDictT(DirectoryTree):
def render_label(self, node, base_style, style):
text = super().render_label(node, base_style, style)
if "a" in str(node.data.path):
text.stylize("green")
return text
class MyApp(App[None]):
def compose(self) -> ComposeResult:
yield MyDictT(".")
if __name__ == "__main__":
MyApp().run()
from textual.
Thanks @rodrigogiraoserrao! Apologies, did not think to supply an MRE 🤦♂️ Please find below:
from rich.style import Style
from rich.text import Text
from textual.app import App
from textual.widgets import DirectoryTree
from textual.widgets._directory_tree import DirEntry
from textual.widgets._tree import TOGGLE_STYLE, TreeNode
ROOT_PATH = "path/to/some/folder"
FOLDERNAME_TO_HIGHLIGHT = ["dir_1", "dir_2"]
class MyDirectoryTree(DirectoryTree):
DEFAULT_CSS = """
MyDirectoryTree:focus > .tree--cursor {
background: $panel-lighten-2;
}
"""
def render_label(
self, node: TreeNode[DirEntry], base_style: Style, style: Style
) -> Text:
"""
This function is exactly the same as the parent method
except for the `style_node_color` call.
"""
node_label = node._label.copy()
node_label.stylize(style)
node_path = node.data.path
if node._allow_expand:
prefix = (
"📂 " if node.is_expanded else "📁 ",
base_style + TOGGLE_STYLE,
)
node_label.stylize_before(
self.get_component_rich_style(
"directory-tree--folder", partial=True
)
)
else:
prefix = (
"📄 ",
base_style,
)
node_label.stylize_before(
self.get_component_rich_style(
"directory-tree--file", partial=True
),
)
node_label.highlight_regex(
r"\..+$",
self.get_component_rich_style(
"directory-tree--extension", partial=True
),
)
self.style_node_color(node_label, node_path)
text = Text.assemble(prefix, node_label)
return text
def style_node_color(self, node_label, node_path):
if node_path.stem in FOLDERNAME_TO_HIGHLIGHT:
node_label.stylize_before("bright_red")
class TuiApp(App):
def compose(self):
yield MyDirectoryTree(ROOT_PATH)
if __name__ == "__main__":
TuiApp().run()
It looks long because of the override of render_label
but the only change in the overriden method is the call to style_node_color
. To highlight some directories you will need to replace ROOT_PATH
and FOLDERNAME_TO_HIGHLIGHT
with some arbitary directory path on your system, and folder name/s within it, repsectively.
I wonder why we are seeing different behaviour, maybe it is due to how we are styling the node (in the overriden render_label
). The result I get is like the below:
Thanks a lot for taking a look at this!
from textual.
You don't have to copy and paste the whole code only to add an extra function call at the end.
This would do the same thing as your code:
def render_label(
self, node: TreeNode[DirEntry], base_style: Style, style: Style
) -> Text:
text = super().render_label(node, base_style, style)
self.style_node_color(text, node.data.path)
return text
As for the overriding, notice that in my comment above I'm using Text.stylize
whereas your code is using Text.stylize_before
:
I'll close this issue as it seems to me it is resolved.
Feel free to reopen if you feel I missed something!
from textual.
Don't forget to star the repository!
Follow @textualizeio for Textual updates.
from textual.
Related Issues (20)
- Helper `tools/widget_documentation.py` script fails HOT 2
- Help panel isn't helpful for bindings without descriptions HOT 2
- Footer tooltip appears away from cursor
- Awaiting pop_screen in @on causes app to freeze HOT 3
- self.batch_update not working / Calling pop_screen multiple times causes a flickering effect / A new method to pop more screens at once HOT 1
- Investigate Screen._modal_binding_chain behaviour
- Improve cosmetics on Linux vconsole HOT 5
- Quitting the calculator app with the command palette causes escape sequences to be emitted at the prompt HOT 3
- DataTable Cell Borders? HOT 6
- Inline glitch with 1 line screen HOT 4
- Quitting inline app without padding will also clear the line above HOT 3
- `inline_no_clear` broken in v0.77.0 HOT 3
- Can't seem to bind an action to shift+space HOT 3
- Support type specification in `get_screen` HOT 2
- New widgets being ignored HOT 11
- Crash when instancing a SelectionList with disabled=True HOT 4
- Suggester docs HOT 3
- `Binding.key_display` and help panel interact poorly for comma-separated Binding definitions
- Running self.exit() in on_mount() throws exception HOT 4
- Textualize v0.80.0 Textarea tries to set cursor position before its mounted and throws error textual._context.NoActiveAppError HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from textual.