revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Core types for BiDi text handling

use super::helpers::detect_direction;

/// Text direction
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextDirection {
    /// Left-to-right (default for Latin, CJK, etc.)
    #[default]
    Ltr,
    /// Right-to-left (for Arabic, Hebrew, etc.)
    Rtl,
    /// Auto-detect from content
    Auto,
}

impl TextDirection {
    /// Returns true if this is RTL direction
    pub fn is_rtl(&self) -> bool {
        matches!(self, TextDirection::Rtl)
    }

    /// Returns true if this is LTR direction
    pub fn is_ltr(&self) -> bool {
        matches!(self, TextDirection::Ltr)
    }

    /// Returns true if direction should be auto-detected
    pub fn is_auto(&self) -> bool {
        matches!(self, TextDirection::Auto)
    }

    /// Resolve auto direction based on text content
    pub fn resolve(&self, text: &str) -> ResolvedDirection {
        match self {
            TextDirection::Ltr => ResolvedDirection::Ltr,
            TextDirection::Rtl => ResolvedDirection::Rtl,
            TextDirection::Auto => detect_direction(text),
        }
    }
}

/// Resolved (non-auto) text direction
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ResolvedDirection {
    /// Left-to-right
    #[default]
    Ltr,
    /// Right-to-left
    Rtl,
}

impl ResolvedDirection {
    /// Returns true if this is RTL
    pub fn is_rtl(&self) -> bool {
        matches!(self, ResolvedDirection::Rtl)
    }

    /// Returns true if this is LTR
    pub fn is_ltr(&self) -> bool {
        matches!(self, ResolvedDirection::Ltr)
    }

    /// Get the opposite direction
    pub fn opposite(&self) -> Self {
        match self {
            ResolvedDirection::Ltr => ResolvedDirection::Rtl,
            ResolvedDirection::Rtl => ResolvedDirection::Ltr,
        }
    }
}

/// BiDi character class (simplified from UAX #9)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BidiClass {
    /// Strong left-to-right (L)
    L,
    /// Strong right-to-left (R) - Hebrew
    R,
    /// Strong right-to-left Arabic (AL)
    AL,
    /// European number (EN)
    EN,
    /// European number separator (ES)
    ES,
    /// European number terminator (ET)
    ET,
    /// Arabic number (AN)
    AN,
    /// Common number separator (CS)
    CS,
    /// Nonspacing mark (NSM)
    NSM,
    /// Boundary neutral (BN)
    BN,
    /// Paragraph separator (B)
    B,
    /// Segment separator (S)
    S,
    /// Whitespace (WS)
    WS,
    /// Other neutrals (ON)
    ON,
    /// Left-to-right embedding (LRE)
    LRE,
    /// Left-to-right override (LRO)
    LRO,
    /// Right-to-left embedding (RLE)
    RLE,
    /// Right-to-left override (RLO)
    RLO,
    /// Pop directional formatting (PDF)
    PDF,
    /// Left-to-right isolate (LRI)
    LRI,
    /// Right-to-left isolate (RLI)
    RLI,
    /// First strong isolate (FSI)
    FSI,
    /// Pop directional isolate (PDI)
    PDI,
}

impl BidiClass {
    /// Get the BiDi class of a character
    pub fn of(c: char) -> Self {
        let code = c as u32;

        // Check for explicit directional formatting characters
        match c {
            '\u{202A}' => return BidiClass::LRE,
            '\u{202B}' => return BidiClass::RLE,
            '\u{202C}' => return BidiClass::PDF,
            '\u{202D}' => return BidiClass::LRO,
            '\u{202E}' => return BidiClass::RLO,
            '\u{2066}' => return BidiClass::LRI,
            '\u{2067}' => return BidiClass::RLI,
            '\u{2068}' => return BidiClass::FSI,
            '\u{2069}' => return BidiClass::PDI,
            _ => {}
        }

        // Arabic block (0600-06FF) and Arabic Supplement (0750-077F)
        if (0x0600..=0x06FF).contains(&code) || (0x0750..=0x077F).contains(&code) {
            // Arabic numbers
            if (0x0660..=0x0669).contains(&code) || (0x06F0..=0x06F9).contains(&code) {
                return BidiClass::AN;
            }
            return BidiClass::AL;
        }

        // Arabic Extended-A (08A0-08FF)
        if (0x08A0..=0x08FF).contains(&code) {
            return BidiClass::AL;
        }

        // Arabic Presentation Forms-A (FB50-FDFF) and B (FE70-FEFF)
        if (0xFB50..=0xFDFF).contains(&code) || (0xFE70..=0xFEFF).contains(&code) {
            return BidiClass::AL;
        }

        // Hebrew block (0590-05FF)
        if (0x0590..=0x05FF).contains(&code) {
            return BidiClass::R;
        }

        // Other RTL scripts
        if (0x07C0..=0x07FF).contains(&code)     // NKo
            || (0x0800..=0x089F).contains(&code) // Samaritan, Mandaic
            || (0x10800..=0x10FFF).contains(&code) // Phoenician, etc.
            || (0x1E800..=0x1EFFF).contains(&code)
        // Mende Kikakui, etc.
        {
            return BidiClass::R;
        }

        // European numbers (ASCII digits)
        if c.is_ascii_digit() {
            return BidiClass::EN;
        }

        // Number separators
        if matches!(c, '+' | '-') {
            return BidiClass::ES;
        }

        // Number terminators
        if matches!(c, '#' | '$' | '%' | '°' | '€' | '£' | '¥') {
            return BidiClass::ET;
        }

        // Common separators
        if matches!(c, ',' | '.' | ':') {
            return BidiClass::CS;
        }

        // Paragraph separator
        if matches!(c, '\n' | '\r' | '\u{0085}' | '\u{2029}') {
            return BidiClass::B;
        }

        // Segment separator
        if matches!(c, '\t' | '\u{001F}' | '\u{001E}' | '\u{000B}') {
            return BidiClass::S;
        }

        // Whitespace
        if c.is_whitespace() {
            return BidiClass::WS;
        }

        // Common punctuation and symbols are neutral
        if c.is_ascii_punctuation() || matches!(c, '(' | ')' | '[' | ']' | '{' | '}' | '"' | '\'') {
            return BidiClass::ON;
        }

        // Default to strong L for Latin and most other scripts
        BidiClass::L
    }

    /// Check if this is a strong type
    pub fn is_strong(&self) -> bool {
        matches!(self, BidiClass::L | BidiClass::R | BidiClass::AL)
    }

    /// Check if this is a strong RTL type
    pub fn is_strong_rtl(&self) -> bool {
        matches!(self, BidiClass::R | BidiClass::AL)
    }

    /// Check if this is a weak type
    pub fn is_weak(&self) -> bool {
        matches!(
            self,
            BidiClass::EN
                | BidiClass::ES
                | BidiClass::ET
                | BidiClass::AN
                | BidiClass::CS
                | BidiClass::NSM
                | BidiClass::BN
        )
    }

    /// Check if this is a neutral type
    pub fn is_neutral(&self) -> bool {
        matches!(
            self,
            BidiClass::B | BidiClass::S | BidiClass::WS | BidiClass::ON
        )
    }
}

/// A segment of text with a single direction
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BidiRun {
    /// The text content of this run
    pub text: String,
    /// Character range in original text
    pub range: std::ops::Range<usize>,
    /// The embedding level (even = LTR, odd = RTL)
    pub level: u8,
    /// The resolved direction of this run
    pub direction: ResolvedDirection,
}

impl BidiRun {
    /// Create a new BiDi run
    pub fn new(text: String, range: std::ops::Range<usize>, level: u8) -> Self {
        let direction = if level.is_multiple_of(2) {
            ResolvedDirection::Ltr
        } else {
            ResolvedDirection::Rtl
        };
        Self {
            text,
            range,
            level,
            direction,
        }
    }

    /// Get the length of this run in characters
    pub fn char_count(&self) -> usize {
        self.text.chars().count()
    }
}

/// Text alignment
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlign {
    /// Align to start (left for LTR, right for RTL)
    #[default]
    Start,
    /// Align to end (right for LTR, left for RTL)
    End,
    /// Align to left
    Left,
    /// Align to right
    Right,
    /// Center alignment
    Center,
}

/// Result of BiDi analysis
#[derive(Clone, Debug)]
pub struct BidiInfo {
    /// The original text
    pub text: String,
    /// The base/paragraph direction
    pub base_direction: ResolvedDirection,
    /// The BiDi runs (segments with uniform direction)
    pub runs: Vec<BidiRun>,
    /// Visual ordering of runs (indices into runs)
    pub visual_order: Vec<usize>,
}

impl BidiInfo {
    /// Analyze text for BiDi properties
    pub fn new(text: &str, direction: TextDirection) -> Self {
        let base_direction = direction.resolve(text);
        let runs = Vec::new(); // Simplified - would compute runs
        let visual_order = Vec::new(); // Simplified

        Self {
            text: text.to_string(),
            base_direction,
            runs,
            visual_order,
        }
    }

    /// Get the text reordered for visual display
    pub fn visual_text(&self) -> String {
        self.text.clone() // Simplified
    }

    /// Check if the text contains any RTL characters
    pub fn has_rtl(&self) -> bool {
        self.base_direction.is_rtl()
    }

    /// Check if the text is pure LTR
    pub fn is_pure_ltr(&self) -> bool {
        !self.has_rtl()
    }

    /// Check if the text is pure RTL
    pub fn is_pure_rtl(&self) -> bool {
        self.runs.iter().all(|r| r.direction.is_rtl())
    }
}

/// Configuration for BiDi text handling
#[derive(Clone, Debug)]
pub struct BidiConfig {
    /// Default text direction
    pub default_direction: TextDirection,
    /// Whether to support directional override characters
    pub enable_overrides: bool,
    /// Whether to mirror characters in RTL context
    pub enable_mirroring: bool,
}

impl Default for BidiConfig {
    fn default() -> Self {
        Self {
            default_direction: TextDirection::Auto,
            enable_overrides: true,
            enable_mirroring: true,
        }
    }
}

/// Layout helper for RTL text in a fixed-width area
#[derive(Clone, Debug)]
pub struct RtlLayout {
    /// The available width
    pub width: usize,
    /// Text alignment
    pub align: TextAlign,
    /// Base direction
    pub direction: ResolvedDirection,
}

impl RtlLayout {
    /// Create a new RTL layout helper
    pub fn new(width: usize, direction: ResolvedDirection) -> Self {
        Self {
            width,
            align: TextAlign::Start,
            direction,
        }
    }

    /// Set text alignment
    pub fn align(mut self, align: TextAlign) -> Self {
        self.align = align;
        self
    }

    /// Calculate the X position for text of given width
    pub fn position(&self, text_width: usize) -> usize {
        if text_width >= self.width {
            return 0;
        }

        let padding = self.width - text_width;

        match self.align {
            TextAlign::Start => {
                if self.direction.is_rtl() {
                    padding
                } else {
                    0
                }
            }
            TextAlign::End => {
                if self.direction.is_rtl() {
                    0
                } else {
                    padding
                }
            }
            TextAlign::Left => 0,
            TextAlign::Right => padding,
            TextAlign::Center => padding / 2,
        }
    }

    /// Pad text to fill width according to alignment
    pub fn pad(&self, text: &str, text_width: usize) -> String {
        if text_width >= self.width {
            return text.to_string();
        }

        let padding = self.width - text_width;
        let pos = self.position(text_width);
        let left_pad = pos;
        let right_pad = padding - left_pad;

        format!("{}{}{}", " ".repeat(left_pad), text, " ".repeat(right_pad))
    }
}