Skip to content

Conversation

@shaal
Copy link

@shaal shaal commented Jan 2, 2026

Summary

This PR adds BiDi (Bidirectional) text support to Textual, enabling proper display of RTL (Right-to-Left) languages like Hebrew and Arabic in terminal applications.

Key Features

  • Auto-detection: The text-direction: auto CSS property automatically detects RTL characters and applies the Unicode BiDi algorithm
  • CSS property: New text-direction property with values auto, ltr, rtl
  • Optional dependency: Uses python-bidi library (install with pip install textual[bidi])
  • Graceful fallback: Works without python-bidi installed (RTL detection still works, just no reordering)

Changes

File Description
_bidi.py New core BiDi utilities module
content.py Integrated BiDi processing in render pipeline
css/types.py Added TextDirection type
css/constants.py Added VALID_TEXT_DIRECTION
css/styles.py Added text_direction CSS property
css/_styles_builder.py Added CSS parser for text-direction
widgets/_text_area.py Added text_direction reactive property
widgets/_markdown.py Code blocks force LTR to preserve formatting
pyproject.toml Added python-bidi optional dependency
tests/test_bidi.py Comprehensive tests

Usage

CSS:

/* Auto-detect RTL text */
Static {
    text-direction: auto;
}

/* Force RTL */
Label {
    text-direction: rtl;
}

/* Force LTR (default for code) */
MarkdownFence {
    text-direction: ltr;
}

Python (TextArea):

text_area = TextArea(text="שלום עולם")
text_area.text_direction = "auto"  # or "rtl" or "ltr"

Test Examples

  • Hebrew: שלום עולם (Hello World)
  • Arabic: مرحبا بالعالم
  • Mixed: Hello שלום World
  • Numbers in RTL: מספר 123

Test plan

  • Run pytest tests/test_bidi.py - All 22 tests pass (3 skipped when python-bidi not installed)
  • Run pytest tests/test_content.py - All 74 tests pass
  • Manual testing with Hebrew/Arabic text in Static, Label, TextArea widgets
  • Verify code blocks in Markdown remain LTR

🤖 Generated with Claude Code

…directional languages

This adds BiDi (Bidirectional) text support to Textual, enabling proper display of
RTL languages like Hebrew and Arabic in terminal applications.

## Changes

### New Files
- `_bidi.py`: Core BiDi utilities with RTL detection and Unicode BiDi algorithm support
- `tests/test_bidi.py`: Comprehensive tests for BiDi functionality

### CSS System
- Added `text-direction` CSS property with values: "auto", "ltr", "rtl"
- Default is "auto" which detects RTL characters automatically

### Widgets
- **Content class**: Integrated BiDi processing in render pipeline
- **TextArea**: Added `text_direction` reactive property (defaults to "ltr" for code)
- **Markdown**: Code blocks force LTR to preserve formatting

### Dependencies
- Added `python-bidi` as optional dependency (`pip install textual[bidi]`)

## Usage

```css
/* Auto-detect RTL */
Static { text-direction: auto; }

/* Force RTL */
Label { text-direction: rtl; }
```

```python
text_area.text_direction = "auto"
```

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@willmcgugan
Copy link
Member

I will need the following before I consider merging:

  • Passing tests
  • Snapshot tests covering new code paths
  • Documentation and examples of the new styles in same format as we currently have for other styles
  • A technical document describing the changes
  • A commitment from you to support these changes for a while until bugs have been worked out

I gather this is AI generated. But I do hope you have an understanding of this update beyond what you prompted for...

https://textual.textualize.io/blog/2023/07/29/pull-requests-are-cake-or-puppies/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants