1#![doc = include_str!("../readme.md")]
2#![allow(clippy::uninlined_format_args)]
3use std::error::Error;
4use std::fmt::{Debug, Display, Formatter};
5use std::ops::Range;
6
7pub mod clipboard;
8#[cfg(feature = "palette")]
9pub mod color_input;
10pub mod date_input;
11pub mod line_number;
12pub mod number_input;
13pub mod text_area;
14pub mod text_input;
15pub mod text_input_mask;
16pub mod undo_buffer;
17
18mod cache;
19mod glyph2;
20mod grapheme;
21mod range_map;
22mod text_core;
23mod text_store;
24
25pub use grapheme::Grapheme;
26
27use crate::_private::NonExhaustive;
28pub use pure_rust_locales::Locale;
29pub use rat_cursor::{HasScreenCursor, impl_screen_cursor, screen_cursor};
30use rat_scrolled::ScrollStyle;
31use ratatui::style::Style;
32use ratatui::widgets::Block;
33
34pub mod event {
35 pub use rat_event::*;
40
41 #[derive(Debug)]
43 pub struct ReadOnly;
44
45 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
47 pub enum TextOutcome {
48 Continue,
50 Unchanged,
53 Changed,
58 TextChanged,
60 }
61
62 impl ConsumedEvent for TextOutcome {
63 fn is_consumed(&self) -> bool {
64 *self != TextOutcome::Continue
65 }
66 }
67
68 impl From<bool> for TextOutcome {
70 fn from(value: bool) -> Self {
71 if value {
72 TextOutcome::Changed
73 } else {
74 TextOutcome::Unchanged
75 }
76 }
77 }
78
79 impl From<Outcome> for TextOutcome {
80 fn from(value: Outcome) -> Self {
81 match value {
82 Outcome::Continue => TextOutcome::Continue,
83 Outcome::Unchanged => TextOutcome::Unchanged,
84 Outcome::Changed => TextOutcome::Changed,
85 }
86 }
87 }
88
89 impl From<TextOutcome> for Outcome {
90 fn from(value: TextOutcome) -> Self {
91 match value {
92 TextOutcome::Continue => Outcome::Continue,
93 TextOutcome::Unchanged => Outcome::Unchanged,
94 TextOutcome::Changed => Outcome::Changed,
95 TextOutcome::TextChanged => Outcome::Changed,
96 }
97 }
98 }
99}
100
101#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
106pub enum TextFocusGained {
107 None,
109 #[default]
112 Overwrite,
113 SelectAll,
115}
116
117#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
122pub enum TextFocusLost {
123 None,
125 #[default]
128 Position0,
129}
130
131#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
136pub enum TextTab {
137 #[default]
139 MoveToNextSection,
140 MoveToNextWidget,
142}
143
144#[derive(Debug, Clone)]
146pub struct TextStyle {
147 pub style: Style,
148 pub scroll: Option<ScrollStyle>,
149 pub block: Option<Block<'static>>,
150 pub border_style: Option<Style>,
151 pub title_style: Option<Style>,
152 pub focus: Option<Style>,
153 pub select: Option<Style>,
154 pub invalid: Option<Style>,
155
156 pub on_focus_gained: Option<TextFocusGained>,
158 pub on_focus_lost: Option<TextFocusLost>,
160 pub on_tab: Option<TextTab>,
162
163 pub non_exhaustive: NonExhaustive,
164}
165
166impl Default for TextStyle {
167 fn default() -> Self {
168 Self {
169 style: Default::default(),
170 scroll: Default::default(),
171 block: Default::default(),
172 border_style: Default::default(),
173 title_style: Default::default(),
174 focus: Default::default(),
175 select: Default::default(),
176 invalid: Default::default(),
177 on_focus_gained: Default::default(),
178 on_focus_lost: Default::default(),
179 on_tab: Default::default(),
180 non_exhaustive: NonExhaustive,
181 }
182 }
183}
184
185pub mod core {
186 pub use crate::text_core::TextCore;
192 pub use crate::text_core::core_op;
193 pub use crate::text_store::SkipLine;
194 pub use crate::text_store::TextStore;
195 pub use crate::text_store::text_rope::TextRope;
196 pub use crate::text_store::text_string::TextString;
197}
198
199#[derive(Debug, PartialEq)]
200pub enum TextError {
201 InvalidText(String),
203 Clipboard,
205 TextRangeOutOfBounds(TextRange),
207 TextPositionOutOfBounds(TextPosition),
209 LineIndexOutOfBounds(upos_type, upos_type),
214 ColumnIndexOutOfBounds(upos_type, upos_type),
216 ByteIndexOutOfBounds(usize, usize),
221 CharIndexOutOfBounds(usize, usize),
226 ByteRangeOutOfBounds(Option<usize>, Option<usize>, usize),
232 CharRangeOutOfBounds(Option<usize>, Option<usize>, usize),
239 ByteIndexNotCharBoundary(usize),
243 ByteRangeNotCharBoundary(
250 Option<usize>, Option<usize>, ),
253 ByteRangeInvalid(
258 usize, usize, ),
261 CharRangeInvalid(
266 usize, usize, ),
269 InvalidSearch,
271}
272
273impl Display for TextError {
274 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
275 write!(f, "{:?}", self)
276 }
277}
278
279impl Error for TextError {}
280
281#[allow(non_camel_case_types)]
283pub type upos_type = u32;
284#[allow(non_camel_case_types)]
286pub type ipos_type = i32;
287
288#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
290pub struct TextPosition {
291 pub y: upos_type,
292 pub x: upos_type,
293}
294
295impl TextPosition {
296 pub const fn new(x: upos_type, y: upos_type) -> TextPosition {
298 Self { y, x }
299 }
300}
301
302impl Debug for TextPosition {
303 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
304 write!(f, "{}|{}", self.x, self.y)
305 }
306}
307
308impl From<(upos_type, upos_type)> for TextPosition {
309 fn from(value: (upos_type, upos_type)) -> Self {
310 Self {
311 y: value.1,
312 x: value.0,
313 }
314 }
315}
316
317impl From<TextPosition> for (upos_type, upos_type) {
318 fn from(value: TextPosition) -> Self {
319 (value.x, value.y)
320 }
321}
322
323#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
326pub struct TextRange {
327 pub start: TextPosition,
329 pub end: TextPosition,
331}
332
333impl Debug for TextRange {
334 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
335 write!(
336 f,
337 "{}|{}-{}|{}",
338 self.start.x, self.start.y, self.end.x, self.end.y
339 )
340 }
341}
342
343impl From<Range<TextPosition>> for TextRange {
344 fn from(value: Range<TextPosition>) -> Self {
345 assert!(value.start <= value.end);
346 Self {
347 start: value.start,
348 end: value.end,
349 }
350 }
351}
352
353impl From<Range<(upos_type, upos_type)>> for TextRange {
354 fn from(value: Range<(upos_type, upos_type)>) -> Self {
355 Self {
356 start: TextPosition::from(value.start),
357 end: TextPosition::from(value.end),
358 }
359 }
360}
361
362impl From<TextRange> for Range<TextPosition> {
363 fn from(value: TextRange) -> Self {
364 value.start..value.end
365 }
366}
367
368impl TextRange {
369 pub const MAX: TextRange = TextRange {
371 start: TextPosition {
372 y: upos_type::MAX,
373 x: upos_type::MAX,
374 },
375 end: TextPosition {
376 y: upos_type::MAX,
377 x: upos_type::MAX,
378 },
379 };
380
381 pub fn new(start: impl Into<TextPosition>, end: impl Into<TextPosition>) -> Self {
386 let start = start.into();
387 let end = end.into();
388
389 assert!(start <= end);
390
391 TextRange { start, end }
392 }
393
394 #[inline]
396 pub fn is_empty(&self) -> bool {
397 self.start == self.end
398 }
399
400 #[inline]
402 pub fn contains_pos(&self, pos: impl Into<TextPosition>) -> bool {
403 let pos = pos.into();
404 pos >= self.start && pos < self.end
405 }
406
407 #[inline]
409 pub fn before_pos(&self, pos: impl Into<TextPosition>) -> bool {
410 let pos = pos.into();
411 pos >= self.end
412 }
413
414 #[inline]
416 pub fn after_pos(&self, pos: impl Into<TextPosition>) -> bool {
417 let pos = pos.into();
418 pos < self.start
419 }
420
421 #[inline(always)]
423 pub fn contains(&self, other: TextRange) -> bool {
424 other.start >= self.start && other.end <= self.end
425 }
426
427 #[inline(always)]
429 pub fn before(&self, other: TextRange) -> bool {
430 other.start > self.end
431 }
432
433 #[inline(always)]
435 pub fn after(&self, other: TextRange) -> bool {
436 other.end < self.start
437 }
438
439 #[inline(always)]
441 pub fn intersects(&self, other: TextRange) -> bool {
442 other.start <= self.end && other.end >= self.start
443 }
444
445 #[inline]
448 pub fn expand(&self, range: TextRange) -> TextRange {
449 TextRange::new(self.expand_pos(range.start), self.expand_pos(range.end))
450 }
451
452 #[inline]
455 pub fn expand_pos(&self, pos: TextPosition) -> TextPosition {
456 let delta_lines = self.end.y - self.start.y;
457
458 if pos < self.start {
460 pos
461 } else if pos == self.start {
462 self.end
463 } else {
464 if pos.y > self.start.y {
465 TextPosition::new(pos.x, pos.y + delta_lines)
466 } else if pos.y == self.start.y {
467 if pos.x >= self.start.x {
468 TextPosition::new(pos.x - self.start.x + self.end.x, pos.y + delta_lines)
469 } else {
470 pos
471 }
472 } else {
473 pos
474 }
475 }
476 }
477
478 #[inline]
481 pub fn shrink(&self, range: TextRange) -> TextRange {
482 TextRange::new(self.shrink_pos(range.start), self.shrink_pos(range.end))
483 }
484
485 #[inline]
488 pub fn shrink_pos(&self, pos: TextPosition) -> TextPosition {
489 let delta_lines = self.end.y - self.start.y;
490
491 if pos < self.start {
493 pos
494 } else if pos >= self.start && pos <= self.end {
495 self.start
496 } else {
497 if pos.y > self.end.y {
499 TextPosition::new(pos.x, pos.y - delta_lines)
500 } else if pos.y == self.end.y {
501 if pos.x >= self.end.x {
502 TextPosition::new(pos.x - self.end.x + self.start.x, pos.y - delta_lines)
503 } else {
504 pos
505 }
506 } else {
507 pos
508 }
509 }
510 }
511}
512
513pub trait Cursor: Iterator {
518 fn prev(&mut self) -> Option<Self::Item>;
520
521 fn peek_next(&mut self) -> Option<Self::Item> {
523 let v = self.next();
524 self.prev();
525 v
526 }
527
528 fn peek_prev(&mut self) -> Option<Self::Item> {
530 let v = self.prev();
531 self.next();
532 v
533 }
534
535 fn text_offset(&self) -> usize;
537}
538
539mod _private {
540 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
541 pub struct NonExhaustive;
542}