duat_core::text

Struct Text

Source
pub struct Text { /* private fields */ }
Expand description

The text in a given Area

Implementations§

Source§

impl Text

Source

pub fn search_fwd<R: RegexPattern>( &mut self, pat: R, at: Point, end: Option<Point>, ) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>

Source

pub fn search_rev<R: RegexPattern>( &mut self, pat: R, at: Point, start: Option<Point>, ) -> Result<impl Iterator<Item = R::Match> + '_, Box<Error>>

Returns an iterator over the reverse matches of the regex

Source§

impl Text

Source

pub fn new() -> Self

Returns a new, empty Text

Source

pub fn from_file(path: impl AsRef<Path>) -> Self

Creates a Text from a file’s path

Source

pub fn builder() -> Builder

Returns a Builder for Text

This builder can be used to iteratively create text, by assuming that the user wants no* Tag overlap, and that they want to construct the Text in Tag/content pairs.

use duat_core::text::{Tag, Text, text};
let mut builder = Text::builder();
Source

pub fn len(&self) -> Point

The Point at the end of the text

Source

pub fn is_empty(&self) -> bool

Wether or not there are any characters in the Text

§Note

This does not check for tags, so with a Tag::GhostText, there could actually be a “string” of characters on the Text, it just wouldn’t be considered real “text”.

Source

pub fn char_at(&self, point: Point) -> Option<char>

The char at the Point’s position

Source

pub fn strs(&self) -> [&str; 2]

The two &strs that compose the buffer

In order to iterate over them, I recommend using the flat_map method:

let text = Text::new();
text.strs().into_iter().flat_map(str::chars);

Do note that you should avoid iterators like str::lines, as they will separate the line that is partially owned by each &str:

let broken_up_line = [
    "This is line 1, business as usual This is line 2, but it",
    "is broken into two separate strings So 4 lines would be counted, \
     instead of 3",
];

If you want the two &strs in a range, see strs_in_range

Source

pub fn strs_in_range(&self, (p1, p2): (Point, Point)) -> [&str; 2]

This method will return two &strs at the Point range

This function treats any Points outside the range as if they where the last point in the text.

§Note

The reason why this function returns two strings is that the contents of the text are stored in a GapBuffer, which works with two strings.

If you want to iterate over them, you can do the following:

let text = Text::new();
text.strs_in_range((p1, p2))
    .into_iter()
    .flat_map(str::bytes);

Do note that you should avoid iterators like str::lines, as they will separate the line that is partially owned by each &str:

let broken_up_line = [
    "This is line 1, business as usual.\nThis is line 2, but it",
    "is broken into two separate strings.\nSo 4 lines would be counted, \
     instead of 3",
];

If you want the two full &strs, see strs

Source

pub fn point_at(&self, at: u32) -> Point

The Point corresponding to the byte position, 0 indexed

If the byte position would fall in between two characters (because the first one comprises more than one byte), the first character is chosen as the Point where the byte is located.

§Panics

Will panic if at is greater than the lenght of the text

Source

pub fn point_at_char(&self, at: u32) -> Point

The Point associated with a char position, 0 indexed

§Panics

Will panic if at is greater than the number of chars in the text.

Source

pub fn point_at_line(&self, at: u32) -> Point

The Point where the atth line starts, 0 indexed

If at == number_of_lines, returns the last point of the text.

§Panics

Will panic if the number at is greater than the number of lines on the text

Source

pub fn len_points(&self) -> (Point, Option<Point>)

The points at the end of the text

This will essentially return the last point of the text, alongside the last possible Point of any Tag::GhostText at the end of the text.

Source

pub fn last_point(&self) -> Option<Point>

The last Point associated with a char

This will give the Point of the last char of the text. The difference between this method and len is that it will return a Point one position earlier than it. If the text is completely empty, it will return None.

Source

pub fn ghost_max_points_at(&self, at: u32) -> (Point, Option<Point>)

The maximum points in the atth byte

This point is essentially the point at that byte, plus the last possible Point of any Tag::GhostTexts in that position.

Source

pub fn points_after(&self, tp: impl TwoPoints) -> Option<(Point, Option<Point>)>

Points visually after the TwoPoints

If the TwoPoints in question is concealed, treats the next visible character as the first character, and returns the points of the next visible character.

This method is useful if you want to iterater reversibly right after a certain point, thus including the character of said point.

Source

pub fn visual_line_start(&self, p: impl TwoPoints) -> (Point, Option<Point>)

The visual start of the line

This point is defined not by where the line actually begins, but by where the last ‘\n’ was located. For example, if Tags create ghost text or ommit text from multiple different lines, this point may differ from where in the Text the physical line actually begins.

Source

pub fn replace_range(&mut self, range: (Point, Point), edit: impl ToString)

Source

pub fn undo(&mut self, area: &impl Area, cursors: &mut Cursors, cfg: PrintCfg)

Undoes the last moment, if there was one

Source

pub fn redo(&mut self, area: &impl Area, cursors: &mut Cursors, cfg: PrintCfg)

Redoes the last moment in the history, if there is one

Source

pub fn new_moment(&mut self)

Finishes the current moment and adds a new one to the history

Source

pub fn to_string(&self) -> String

Clones the inner GapBuffer as a String

This function will also cut out a final ‘\n’ from the string.

Source

pub fn write_to(&self, writer: impl Write) -> Result<usize>

Writes the contents of this Text to a writer

Source

pub fn insert_tag(&mut self, at: u32, tag: Tag, key: Key)

Inserts a Tag at the given position

Source

pub fn remove_tags_on(&mut self, at: u32, keys: impl Keys)

Removes all the Tags from a position related to a key

Source

pub fn remove_tags_of(&mut self, keys: impl Keys)

Removes the Tags of a key from the whole Text

§Caution

While it is fine to do this on your own widgets, you should refrain from using this function in a Files Text, as it must iterate over all tags in the file, so if there are a lot of other tags, this operation may be slow.

Source

pub fn clear_tags(&mut self)

Removes all Tags

Refrain from using this function on Files, as there may be other Tag providers, and you should avoid messing with their tags.

Source

pub fn iter_fwd(&self, at: impl TwoPoints) -> Iter<'_>

A forward iterator of the chars and tags of the Text

Source

pub fn iter_rev(&self, at: impl TwoPoints) -> RevIter<'_>

A reverse iterator of the chars and tags of the Text

Source

pub fn chars_fwd(&self, p: Point) -> impl Iterator<Item = (Point, char)> + '_

A forward iterator of the chars of the Text

Each char will be accompanied by a Point, which is the position where said character starts, e.g. Point::default() for the first character

Source

pub fn chars_rev(&self, p: Point) -> impl Iterator<Item = (Point, char)> + '_

A reverse iterator of the chars of the Text

Each char will be accompanied by a Point, which is the position where said character starts, e.g. Point::default() for the first character

Source

pub fn tags_fwd( &self, at: u32, ) -> Peekable<impl Iterator<Item = (u32, RawTag)> + Clone + 'a>

An forward iterator over the Tags of the Text

§Note

Duat works fine with Tags in the middle of a codepoint, but external utilizers may not, so keep that in mind.

Source

pub fn tags_rev( &self, at: u32, ) -> Peekable<impl Iterator<Item = (u32, RawTag)> + Clone + 'a>

An reverse iterator over the Tags of the Text

§Note

Duat works fine with Tags in the middle of a codepoint, but external utilizers may not, so keep that in mind.

Trait Implementations§

Source§

impl Clone for Text

Source§

fn clone(&self) -> Text

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Text

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Text

Source§

fn default() -> Text

Returns the “default value” for a type. Read more
Source§

impl From<&str> for Text

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Arc<str>> for Text

Source§

fn from(value: Arc<str>) -> Self

Converts to this type from the input type.
Source§

impl From<Box<str>> for Text

Source§

fn from(value: Box<str>) -> Self

Converts to this type from the input type.
Source§

impl<E> From<E> for Text
where E: DuatError,

Source§

fn from(value: E) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Text

Source§

fn from(value: Error) -> Self

Converts to this type from the input type.
Source§

impl From<Rc<str>> for Text

Source§

fn from(value: Rc<str>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Text

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl<U: Ui> From<Text> for State<(), Text, U>

Source§

fn from(value: Text) -> Self

Converts to this type from the input type.
Source§

impl From<char> for Text

Source§

fn from(value: char) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Text

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Text

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Text

Source§

fn from(value: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Text

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Text

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Text

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Text

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Text

Source§

fn from(value: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Text

Source§

fn from(value: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Text

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Text

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Text

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Text

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Text

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Text

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Text

Auto Trait Implementations§

§

impl Freeze for Text

§

impl !RefUnwindSafe for Text

§

impl Send for Text

§

impl Sync for Text

§

impl Unpin for Text

§

impl !UnwindSafe for Text

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.