mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-09 19:00:46 +00:00
Use With trait instead of mutable variable
This commit is contained in:
parent
c10b5e9a1e
commit
eca68256ae
@ -63,7 +63,7 @@ new_default!(TextArea);
|
|||||||
impl TextArea {
|
impl TextArea {
|
||||||
/// Creates a new, empty TextArea.
|
/// Creates a new, empty TextArea.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut area = TextArea {
|
TextArea {
|
||||||
content: String::new(),
|
content: String::new(),
|
||||||
rows: Vec::new(),
|
rows: Vec::new(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
@ -71,12 +71,9 @@ impl TextArea {
|
|||||||
size_cache: None,
|
size_cache: None,
|
||||||
last_size: Vec2::zero(),
|
last_size: Vec2::zero(),
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
};
|
}
|
||||||
|
.with(|area| area.compute_rows(Vec2::new(1, 1)))
|
||||||
// Make sure we have valid rows, even for empty text.
|
// Make sure we have valid rows, even for empty text.
|
||||||
area.compute_rows(Vec2::new(1, 1));
|
|
||||||
|
|
||||||
area
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the content of the view.
|
/// Retrieves the content of the view.
|
||||||
@ -84,11 +81,14 @@ impl TextArea {
|
|||||||
&self.content
|
&self.content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ensures next layout call re-computes the rows.
|
||||||
fn invalidate(&mut self) {
|
fn invalidate(&mut self) {
|
||||||
self.size_cache = None;
|
self.size_cache = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the position of the cursor in the content string.
|
/// Returns the position of the cursor in the content string.
|
||||||
|
///
|
||||||
|
/// This is a byte index.
|
||||||
pub fn cursor(&self) -> usize {
|
pub fn cursor(&self) -> usize {
|
||||||
self.cursor
|
self.cursor
|
||||||
}
|
}
|
||||||
@ -164,26 +164,26 @@ impl TextArea {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the row containing the grapheme at the given offset
|
/// Finds the row containing the grapheme at the given offset
|
||||||
fn row_at(&self, offset: usize) -> usize {
|
fn row_at(&self, byte_offset: usize) -> usize {
|
||||||
debug!("Offset: {}", offset);
|
debug!("Offset: {}", byte_offset);
|
||||||
|
|
||||||
assert!(!self.rows.is_empty());
|
assert!(!self.rows.is_empty());
|
||||||
assert!(offset >= self.rows[0].start);
|
assert!(byte_offset >= self.rows[0].start);
|
||||||
|
|
||||||
self.rows
|
self.rows
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.take_while(|&(_, row)| row.start <= offset)
|
.take_while(|&(_, row)| row.start <= byte_offset)
|
||||||
.map(|(i, _)| i)
|
.map(|(i, _)| i)
|
||||||
.last()
|
.last()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn col_at(&self, offset: usize) -> usize {
|
fn col_at(&self, byte_offset: usize) -> usize {
|
||||||
let row_id = self.row_at(offset);
|
let row_id = self.row_at(byte_offset);
|
||||||
let row = self.rows[row_id];
|
let row = self.rows[row_id];
|
||||||
// Number of cells to the left of the cursor
|
// Number of cells to the left of the cursor
|
||||||
self.content[row.start..offset].width()
|
self.content[row.start..byte_offset].width()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the row containing the cursor
|
/// Finds the row containing the cursor
|
||||||
|
Loading…
Reference in New Issue
Block a user