Skip to content
Open
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions ui-tui/src/__tests__/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
estimateTokensRough,
fmtK,
hasAnsi,
isRtl,
isToolTrailResultLine,
lastCotTrailIndex,
parseToolTrailResultLine,
Expand Down Expand Up @@ -254,3 +255,47 @@ describe('estimateRows', () => {
expect(estimateRows(snake, w)).toBe(estimateRows(plain, w))
})
})

describe('isRtl', () => {
it('returns false for empty string', () => {
expect(isRtl('')).toBe(false)
})

it('returns false for LTR English text', () => {
expect(isRtl('Hello world')).toBe(false)
})

it('returns false for LTR Chinese text', () => {
expect(isRtl('你好世界')).toBe(false)
})

it('detects Persian text', () => {
expect(isRtl('سلام دنیا')).toBe(true)
})

it('detects Arabic text', () => {
expect(isRtl('مرحبا بالعالم')).toBe(true)
})

it('detects Hebrew text', () => {
expect(isRtl('שלום עולם')).toBe(true)
})

it('detects Urdu text (Arabic script)', () => {
expect(isRtl('ہیلو دنیا')).toBe(true)
})

it('detects RTL after leading whitespace', () => {
expect(isRtl(' مرحبا')).toBe(true)
})

it('returns false for LTR text with trailing RTL', () => {
// First strong character is LTR
expect(isRtl('Hello مرحبا')).toBe(false)
})

it('strips ANSI before detection', () => {
// \x1b[31m is red color ANSI code — the first real char is Arabic
expect(isRtl('\x1b[31mمرحبا\x1b[0m')).toBe(true)
})
})
17 changes: 14 additions & 3 deletions ui-tui/src/components/messageLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
compactPreview,
hasAnsi,
isPasteBackedText,
isRtl,
sanitizeAnsiForRender,
stripAnsi
} from '../lib/text.js'
Expand Down Expand Up @@ -193,6 +194,7 @@ export const MessageLine = memo(function MessageLine({
// segments) keep a blank line on both sides so the patch doesn't butt up
// against the prose around it.
const isDiffSegment = msg.kind === 'diff'
const rtl = isRtl(msg.text)

return (
<Box
Expand Down Expand Up @@ -226,14 +228,23 @@ export const MessageLine = memo(function MessageLine({
</Box>
)}

<Box>
<NoSelect flexShrink={0} fromLeftEdge width={gutterWidth}>
<Box flexDirection={rtl ? 'row-reverse' : 'row'}>
<NoSelect
flexShrink={0}
{...(rtl ? {} : { fromLeftEdge: true })}
width={gutterWidth}
>
<Text bold={msg.role === 'user'} color={prefix}>
{glyph}{' '}
</Text>
</NoSelect>

<Box width={transcriptBodyWidth(cols, msg.role, t.brand.prompt, TERMUX_TUI_MODE)}>{content}</Box>
<Box
width={transcriptBodyWidth(cols, msg.role, t.brand.prompt, TERMUX_TUI_MODE)}
alignItems={rtl ? 'flex-end' : 'flex-start'}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner Box defaults to flexDirection: 'row', so alignItems controls its vertical cross-axis rather than horizontal text placement. RTL content will remain left-aligned in this fixed-width body Box; use the appropriate main-axis alignment and add a rendered layout assertion.

>
{content}
</Box>
</Box>
</Box>
)
Expand Down
26 changes: 26 additions & 0 deletions ui-tui/src/lib/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,29 @@ export const pick = <T>(a: T[]) => a[Math.floor(Math.random() * a.length)]!

export const isPasteBackedText = (text: string) =>
/\[\[paste:\d+(?:[^\n]*?)\]\]|\[paste #\d+ (?:attached|excerpt)(?:[^\n]*?)\]/.test(text)

/**
* First-strong-character RTL detection (matches CSS direction: auto behaviour).
* Returns true when the first non-whitespace code point falls in an RTL
* script range: Hebrew (0590–05FF), Arabic/Persian (0600–06FF),
* Arabic Supplement (0750–077F), Arabic Extended-A (08A0–08FF),
* Arabic Presentation Forms (FB50–FDFF, FE70–FEFF).
*/
export const isRtl = (text: string): boolean => {
if (!text) return false
const clean = stripAnsi(text)
for (let i = 0; i < clean.length; i++) {
const cp = clean.codePointAt(i)!
// Skip whitespace / control chars
if (cp <= 0x20 || (cp >= 0x09 && cp <= 0x0D)) continue
return (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns for the first non-whitespace character, not the first strong directional character. (مرحبا) and 1. مرحبا therefore classify as LTR. Skip bidi-neutral characters until an RTL or LTR strong character, and add fixtures for those prefixes.

(cp >= 0x0590 && cp <= 0x05FF) ||
(cp >= 0x0600 && cp <= 0x06FF) ||
(cp >= 0x0750 && cp <= 0x077F) ||
(cp >= 0x08A0 && cp <= 0x08FF) ||
(cp >= 0xFB50 && cp <= 0xFDFF) ||
(cp >= 0xFE70 && cp <= 0xFEFF)
)
}
return false
}
Loading