Skip to main content

Buffer

Struct Buffer 

Source
pub struct Buffer {
    pub file: Option<String>,
    pub dirty: bool,
    pub position: (usize, usize),
    pub vtop: usize,
    /* private fields */
}
Expand description

The internal representation of an opened file

This entity contains the contents of a file, whether that file exists or is to be written, and maintains state information about the file, such as whether it is “dirty” (changes have been made that have not been saved yet), etc.

Fields§

§file: Option<String>

The path to the file; optional because we can open empty buffers that can be written to files using Action::SaveAs

§dirty: bool

State flag for keeping track of unsaved (dirty) changes

§position: (usize, usize)

Cursor position within the buffer

§vtop: usize

The y coordinate of the top of the view

Implementations§

Source§

impl Buffer

Source

pub fn new(file: Option<String>, contents: String) -> Self

Create a new buffer using a file path and some file contents

§Arguments
  • file: Optional file path
  • contents: String representation of the contents of the associated file
§Examples
use tsu_editor::buffer::Buffer;

let file = Some("some_file.rs".to_string());
let contents = "";

let buffer = Buffer::new(file.clone(), contents.to_string());

assert_eq!(buffer.file, file);
Source

pub async fn from_file(file: Option<String>) -> Result<Self>

Create a new buffer by opening a file and retrieving its contents

§Arguments
  • file: An optional file path
§Examples
use tsu_editor::buffer::Buffer;


let file_path_str = Some(file_path.clone().into_os_string().into_string().unwrap());
let buffer = Buffer::from_file(file_path_str.clone()).await.unwrap();
assert_eq!(buffer.file, file_path_str);
§Errors

Can return an IoError if the file cannot be read from

Source

pub fn contents(&self) -> String

Returns the entirety of the content as a String

§Returns
  • The contents of self as a String
Source

pub fn save(&mut self) -> Result<String>

Writes the contents of self to file

§Returns
  • A message to print to the command line of the editor
§Errors

Can return an IoError if it fails to write the contents to the provided file

Source

pub fn save_as(&mut self, new_file_name: &str) -> Result<String>

Writes the contents of self to a new file

§Arguments
  • new_file_name: The path to the file to be written
§Returns
  • A message to print to the command line of the editor
§Errors

Can return an IoError if it fails to write the contents to the provided file

Source

pub fn name(&self) -> &str

Retrieves the name of the buffer which corresponds to the file path it was created from

§Returns
  • The name of the buffer; if the buffer was not created from a file, returns “[No name]”
Source

pub fn is_dirty(&self) -> bool

Check if the buffer is dirty, i.e., has unsaved changes’

§Returns
  • true if the buffer is dirty, false otherwise
Source

pub fn get(&self, line: usize) -> Option<String>

Retrieves the contents at a specific line in the buffer

§Arguments
  • line: The line number
§Returns
  • The contents of the line if the line exists, i.e., is within the bounds of the buffer
Source

pub fn len(&self) -> usize

Get the length of the buffer, i.e., the number of lines

§Returns
  • The size of the buffer expressed as the number of lines
Source

pub fn is_empty(&self) -> bool

Check if the buffer is empty, i.e., the number of lines is 0

§Returns
  • True if the buffer is empty, false otherwise
Source

pub fn insert(&mut self, x: usize, y: usize, c: char)

Put a character at a coordinate position within the buffer

§Arguments
  • x: The coordinate along the x axis (column)
  • y: The coordinate along the y axis (row)
  • c: Character to insert at the specified coordinates
Source

pub fn insert_line(&mut self, line: usize, content: &str)

Insert a line at the specified line index (row)

§Arguments
  • line: The line index where the contents will be inserted
  • content: The string to insert at the specified row
Source

pub fn remove(&mut self, x: usize, y: usize)

Remove a character at the specified coordinates

§Arguments
  • x: The coordinate along the x axis (column)
  • y: The coordinate along the y axis (row)
Source

pub fn replace_line(&mut self, line: usize, new_line: &str)

Replaces the line at a given line index with a new String

§Arguments
  • line: The index of the line being replaced
  • new_line: The String to be written to the line index
Source

pub fn remove_line(&mut self, line: usize)

Removes a line at the given line index

§Arguments
  • line: The index of the line being removed
Source

pub fn view(&self, vtop: usize, vheight: usize) -> String

Calculates the view inside the buffer using a specified view top and view height

§Arguments
  • vtop: The top of the view corresponding to a line index
  • vheight: The height of the view corresponding to the number of lines to display
§Returns
  • The contents within the calculated view as a String
Source

pub fn column_to_char_index(&self, column: usize, y: usize) -> usize

Converts a column index to a character index

§Arguments
  • column: The column index which is to be converted to a character index
  • y: The y coordinate which corresponds to the line number
§Returns
  • The character index as calculated from the column index and the specified line number

Trait Implementations§

Source§

impl Debug for Buffer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.