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: boolState flag for keeping track of unsaved (dirty) changes
position: (usize, usize)Cursor position within the buffer
vtop: usizeThe y coordinate of the top of the view
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn new(file: Option<String>, contents: String) -> Self
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 pathcontents: 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);Sourcepub async fn from_file(file: Option<String>) -> Result<Self>
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
Sourcepub fn name(&self) -> &str
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]”
Sourcepub fn is_dirty(&self) -> bool
pub fn is_dirty(&self) -> bool
Check if the buffer is dirty, i.e., has unsaved changes’
§Returns
trueif the buffer is dirty,falseotherwise
Sourcepub fn len(&self) -> usize
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
Sourcepub fn is_empty(&self) -> bool
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
Sourcepub fn insert(&mut self, x: usize, y: usize, c: char)
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
Sourcepub fn insert_line(&mut self, line: usize, content: &str)
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 insertedcontent: The string to insert at the specified row
Sourcepub fn remove(&mut self, x: usize, y: usize)
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)
Sourcepub fn replace_line(&mut self, line: usize, new_line: &str)
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 replacednew_line: The String to be written to the line index