[][src]Struct rucline::Buffer

pub struct Buffer { /* fields omitted */ }

A String that also keeps track of its cursor position.

When presenting the context of the line reader to an action or a completion, the Buffer is used as a read-only view into the line reader's buffer state.

Basic example:

use rucline::actions::Direction::Backward;
use rucline::actions::Range::Word;
use rucline::actions::Scope::WholeWord;
use rucline::Buffer;

let mut buffer: Buffer = "my new buffer".into();
assert_eq!(buffer.as_str(), "my new buffer");
assert_eq!(buffer.cursor(), "my new buffer".len());

buffer.move_cursor(Word, Backward);
assert_eq!(buffer.as_str(), "my new buffer");
assert_eq!(buffer.cursor(), "my new ".len());

buffer.delete(WholeWord);
assert_eq!(buffer.as_str(), "my new ");
assert_eq!(buffer.cursor(), "my new ".len());

Implementations

impl Buffer[src]

#[must_use]pub fn new() -> Self[src]

Creates an empty buffer.

pub fn new_with_cursor<S: AsRef<str>>(
    string: S,
    cursor: usize
) -> Result<Self, InvalidIndex>
[src]

Creates a new Buffer from string with a given cursor position.

This is a short-hand for:

let mut buffer = Buffer::from(string);
buffer.set_cursor(cursor);

Errors

  • If the curosr position does not fall into a character boundary.

#[must_use]pub fn as_str(&self) -> &str[src]

Returns the current buffer string.

#[must_use]pub fn cursor(&self) -> usize[src]

Returns the current position of the cursor.

pub fn set_cursor(&mut self, cursor: usize) -> Result<(), InvalidIndex>[src]

Sets the cursor position in the buffer.

Errors

  • If the curosr position does not fall into a character boundary.

pub fn go_to_end(&mut self)[src]

Puts the cursor at the end of the buffer.

This is short-hand for move_cursor(Range::Line, Direction::Forward)

pub fn clear(&mut self)[src]

Clears the buffer and sets the cursor back to zero.

pub fn write(&mut self, c: char)[src]

Inserts a single character to the buffer at the cursor position and increments the cursor by one.

Arguments

  • c - The character to insert.

pub fn write_str(&mut self, string: &str)[src]

Inserts a string to the buffer at the cursor position and increments the cursor by the length of string.

Arguments

  • string - The string to insert.

pub fn write_range(&mut self, string: &str, range: Range)[src]

Inserts a range of a string to the buffer at the cursor position and increments the cursor by the length of the range.

Arguments

  • string - The string to insert.
  • range - The range from string to insert.

pub fn delete(&mut self, scope: Scope)[src]

Deletes the given scope from this buffer and updates the cursor accordingly.

Arguments

  • scope - The scope of the deletion.

pub fn move_cursor(&mut self, range: Range, direction: Direction)[src]

Moves the cursor by range.

Arguments

  • range - The range of the movement.
  • direction - The direction of the movement.

Trait Implementations

impl Clone for Buffer[src]

impl Debug for Buffer[src]

impl Default for Buffer[src]

impl Deref for Buffer[src]

type Target = str

The resulting type after dereferencing.

impl Display for Buffer[src]

impl Eq for Buffer[src]

impl<S> From<S> for Buffer where
    S: AsRef<str>, 
[src]

impl PartialEq<Buffer> for Buffer[src]

impl StructuralEq for Buffer[src]

impl StructuralPartialEq for Buffer[src]

Auto Trait Implementations

impl RefUnwindSafe for Buffer

impl Send for Buffer

impl Sync for Buffer

impl Unpin for Buffer

impl UnwindSafe for Buffer

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.