Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer {
    pub opts: BufferOpts,
    /* private fields */
}
Expand description

The widget that is used to print and edit buffers.

Fields§

§opts: BufferOpts

The PrintOpts of this Buffer.

This object, much like PrintOpts, implements Copy, which makes it convenient for moving it around without borrowing the Buffer.

You can use this member to change the way this Buffer will be printed specifically.

Implementations§

Source§

impl Buffer

Source

pub fn moment_for(&self, ns: Ns) -> Moment

Get the latest Moment for a given namespace

This Moment contains every change that took place since the last call with the same Ns. On the first call, it is assumed that you don’t care about previous Changes, so the returned Moment will be empty.

This means that, in order to use this properly, you might want to add a hook like this:

use duat::prelude::*;
let my_plugin_ns = Ns::new();

hook::add::<BufferOpened>(move |pa, buffer| _ = buffer.read(pa).moment_for(my_plugin_ns));

This way, you “register” the Buffer, telling it “yes, I henceforth care about the future Changes”.

§Note: Why is this necessary?

You might reason that the first call to this function should return a Moment with all prior Changes.

However, one thing to note is that

Source

pub fn ranges_to_update_for(&self, ns: Ns) -> RangesToUpdate

Returns a struct that keeps track of which byte ranges need to be updated for a given Ns.

Source§

impl Buffer

Source

pub fn path(&self) -> PathBuf

The full path of the buffer.

If there is no set path, returns "*scratch buffer*#{id}".

Source

pub fn path_set(&self) -> Option<PathBuf>

The full path of the buffer.

Returns None if the path has not been set yet, i.e., if the buffer is a scratch buffer.

Source

pub fn path_txt(&self) -> Text

A Text from the full path of this PathKind

§Formatting

If the buffer’s path was set:

[buffer]{path}

If the buffer’s path was not set:

[buffer.new.scratch]*scratch buffer #{id}*
Source

pub fn name(&self) -> String

The buffer’s name.

If there is no set path, returns "*scratch buffer #{id}*".

Source

pub fn name_set(&self) -> Option<String>

The buffer’s name.

Returns None if the path has not been set yet, i.e., if the buffer is a scratch buffer.

Source

pub fn name_txt(&self) -> Text

A Text from the name of this PathKind

The name of a Buffer widget is the same as the path, but it strips away the current directory. If it can’t, it will try to strip away the home directory, replacing it with "~". If that also fails, it will just show the full path.

§Formatting

If the buffer’s name was set:

[buffer]{name}

If the buffer’s name was not set:

[buffer.new.scratch]*scratch buffer #{id}*
Source

pub fn path_kind(&self) -> PathKind

The type of PathBuf

This represents the three possible states for a Buffer’s PathBuf, as it could either represent a real Buffer, not exist, or not have been defined yet.

Source

pub fn buffer_id(&self) -> BufferId

A unique identifier for this Buffer.

This is more robust than identifying it by its path or name, or event PathKind, since those could change, but this cannot.

Source

pub fn text(&self) -> &Text

The Text of this Buffer

This is the same as Widget::text, but doesn’t need the Widget trait to be in scope.

Source

pub fn text_mut(&mut self) -> TextMut<'_>

The mutable TextMut of this Buffer

This is the same as Widget::text_mut, but doesn’t need the Widget trait to be in scope.

Source

pub fn text_parts(&mut self) -> TextParts<'_>

The parts that make up a Text.

This function is used when you want to insert/remove Tags (i.e., borrow the inner InnerTags mutably via Tags), while still being able to read from the Strs and Selections.

Source

pub fn len_bytes(&self) -> usize

The number of bytes in the buffer.

Source

pub fn len_chars(&self) -> usize

The number of chars in the buffer.

Source

pub fn len_lines(&self) -> usize

The number of lines in the buffer.

Source

pub fn selections(&self) -> &Selections

The Selections that are used on the Text.

Source

pub fn selections_mut(&mut self) -> &mut Selections

A mutable reference to the Selections.

Source

pub fn exists(&self) -> bool

Whether o not the Buffer exists or not.

Source

pub fn was_reloaded(&self) -> bool

Wether this buffer came from a previous reload cycle.

You should use this function to decide on things that persist across reload cycles (i.e. the crate::process and crate::storage modules).

Trait Implementations§

Source§

impl Widget for Buffer

Source§

fn text(&self) -> &Text

The text that this widget prints out
Source§

fn text_mut(&mut self) -> TextMut<'_>

A mutable reference to the Text that is printed
Source§

fn print_opts(&self) -> PrintOpts

The configuration for how to print Text Read more

Auto Trait Implementations§

§

impl !Freeze for Buffer

§

impl !RefUnwindSafe for Buffer

§

impl Send for Buffer

§

impl Sync for Buffer

§

impl Unpin for Buffer

§

impl UnsafeUnpin for Buffer

§

impl !UnwindSafe for Buffer

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> 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, 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.