cleanup
This commit is contained in:
parent
8a68f0192c
commit
14796e2929
@ -51,7 +51,7 @@ impl Document {
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, at: &Position, c: char) {
|
||||
if at.y > self.len() {
|
||||
if at.y > self.rows.len() {
|
||||
return;
|
||||
}
|
||||
self.dirty = true;
|
||||
@ -61,39 +61,47 @@ impl Document {
|
||||
return;
|
||||
}
|
||||
|
||||
if at.y == self.len() {
|
||||
if at.y == self.rows.len() {
|
||||
let mut row = Row::default();
|
||||
row.insert(0, c);
|
||||
self.rows.push(row);
|
||||
} else {
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
let row = &mut self.rows[at.y];
|
||||
row.insert(at.x, c);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_newline(&mut self, at: &Position) {
|
||||
if at.y == self.len() {
|
||||
if at.y > self.rows.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
if at.y == self.rows.len() {
|
||||
self.rows.push(Row::default());
|
||||
return;
|
||||
}
|
||||
|
||||
let new_row = self.rows.get_mut(at.y).unwrap().split(at.x);
|
||||
#[allow(clippy::indexing_slicing)]
|
||||
let new_row = self.rows[at.y].split(at.x);
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
self.rows.insert(at.y + 1, new_row);
|
||||
}
|
||||
|
||||
#[allow(clippy::integer_arithmetic, clippy::indexing_slicing)]
|
||||
pub fn delete(&mut self, at: &Position) {
|
||||
let len = self.len();
|
||||
let len = self.rows.len();
|
||||
if at.y >= len {
|
||||
return;
|
||||
}
|
||||
self.dirty = true;
|
||||
|
||||
if at.x == self.rows.get_mut(at.y).unwrap().len() && at.y < len - 1 {
|
||||
if at.x == self.rows[at.y].len() && at.y + 1 < len {
|
||||
let next_row = self.rows.remove(at.y + 1);
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
let row = &mut self.rows[at.y];
|
||||
row.append(&next_row);
|
||||
} else {
|
||||
let row = self.rows.get_mut(at.y).unwrap();
|
||||
let row = &mut self.rows[at.y];
|
||||
row.delete(at.x);
|
||||
}
|
||||
}
|
||||
|
@ -187,14 +187,14 @@ impl Editor {
|
||||
}
|
||||
Key::PageUp => {
|
||||
y = if y > terminal_height {
|
||||
y - terminal_height
|
||||
y.saturating_sub(terminal_height)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
Key::PageDown => {
|
||||
y = if y.saturating_add(terminal_height) < height {
|
||||
y + terminal_height as usize
|
||||
y.saturating_add(terminal_height)
|
||||
} else {
|
||||
height
|
||||
}
|
||||
@ -241,6 +241,7 @@ impl Editor {
|
||||
let mut welcome_message = format!("Hecto Editor -- Version {}\r", VERSION);
|
||||
let width = self.terminal.size().width as usize;
|
||||
let len = welcome_message.len();
|
||||
#[allow(clippy::integer_arithmetic, clippy::integer_division)]
|
||||
let padding = width.saturating_sub(len) / 2;
|
||||
let spaces = " ".repeat(padding.saturating_sub(1));
|
||||
welcome_message = format!("~{}{}", spaces, welcome_message);
|
||||
@ -251,16 +252,20 @@ impl Editor {
|
||||
fn draw_row(&self, row: &Row) {
|
||||
let width = self.terminal.size().width as usize;
|
||||
let start = self.offset.x;
|
||||
let end = self.offset.x + width;
|
||||
let end = self.offset.x.saturating_add(width);
|
||||
let row = row.render(start, end);
|
||||
println!("{}\r", row)
|
||||
}
|
||||
|
||||
#[allow(clippy::integer_division, clippy::integer_arithmetic)]
|
||||
fn draw_rows(&self) {
|
||||
let height = self.terminal.size().height;
|
||||
for terminal_row in 0..height {
|
||||
Terminal::clear_current_line();
|
||||
if let Some(row) = self.document.row(terminal_row as usize + self.offset.y) {
|
||||
if let Some(row) = self
|
||||
.document
|
||||
.row(self.offset.y.saturating_add(terminal_row as usize))
|
||||
{
|
||||
self.draw_row(row);
|
||||
} else if self.document.is_empty() && terminal_row == height / 3 {
|
||||
self.draw_welcome_message()
|
||||
@ -273,11 +278,10 @@ impl Editor {
|
||||
pub fn default() -> Self {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let mut initial_status = String::from("HELP: CTRL-S = save | CTRL-Q = quit");
|
||||
let document = if args.len() > 1 {
|
||||
let file_name = &args[1];
|
||||
let document = if let Some(file_name) = args.get(1) {
|
||||
let doc = Document::open(&file_name);
|
||||
if doc.is_ok() {
|
||||
doc.unwrap()
|
||||
if let Ok(doc) = doc {
|
||||
doc
|
||||
} else {
|
||||
initial_status = format!("ERR: Could not open file: {}", file_name);
|
||||
Document::default()
|
||||
@ -322,9 +326,7 @@ impl Editor {
|
||||
self.document.len(),
|
||||
);
|
||||
let len = status.len() + line_indicator.len();
|
||||
if width > len {
|
||||
status.push_str(&" ".repeat(width - len));
|
||||
}
|
||||
status.push_str(&" ".repeat(width.saturating_sub(len)));
|
||||
status = format!("{}{}", status, line_indicator);
|
||||
status.truncate(width);
|
||||
|
||||
@ -351,11 +353,7 @@ impl Editor {
|
||||
self.status_message = StatusMessage::from(format!("{}{}", prompt, result));
|
||||
self.refresh_screen()?;
|
||||
match Terminal::read_key()? {
|
||||
Key::Backspace => {
|
||||
if !result.is_empty() {
|
||||
result.truncate(result.len() - 1);
|
||||
}
|
||||
}
|
||||
Key::Backspace => result.truncate(result.len().saturating_sub(1)),
|
||||
Key::Char('\n') => break,
|
||||
Key::Char(c) => {
|
||||
if !c.is_control() {
|
||||
|
10
src/main.rs
10
src/main.rs
@ -1,4 +1,12 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
#![warn(clippy::all, clippy::pedantic, clippy::restriction)]
|
||||
#![allow(
|
||||
clippy::missing_docs_in_private_items,
|
||||
clippy::implicit_return,
|
||||
clippy::shadow_reuse,
|
||||
clippy::print_stdout,
|
||||
clippy::wildcard_enum_match_arm,
|
||||
clippy::else_if_without_else
|
||||
)]
|
||||
|
||||
mod document;
|
||||
mod editor;
|
||||
|
83
src/row.rs
83
src/row.rs
@ -1,8 +1,6 @@
|
||||
use std::cmp;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use crate::Position;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Row {
|
||||
string: String,
|
||||
@ -11,12 +9,10 @@ pub struct Row {
|
||||
|
||||
impl From<&str> for Row {
|
||||
fn from(slice: &str) -> Self {
|
||||
let mut row = Self {
|
||||
Self {
|
||||
string: String::from(slice),
|
||||
len: 0,
|
||||
};
|
||||
row.update_len();
|
||||
row
|
||||
len: slice.graphemes(true).count(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,6 +21,7 @@ impl Row {
|
||||
let end = cmp::min(end, self.string.len());
|
||||
let start = cmp::min(start, end);
|
||||
let mut result = String::new();
|
||||
#[allow(clippy::integer_arithmetic)]
|
||||
for grapheme in self.string[..]
|
||||
.graphemes(true)
|
||||
.skip(start)
|
||||
@ -47,46 +44,72 @@ impl Row {
|
||||
self.len == 0
|
||||
}
|
||||
|
||||
pub fn update_len(&mut self) {
|
||||
self.len = self.string[..].graphemes(true).count();
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, at: usize, c: char) {
|
||||
if at >= self.len() {
|
||||
self.string.push(c);
|
||||
} else {
|
||||
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
||||
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
||||
result.push(c);
|
||||
result.push_str(&remainder);
|
||||
self.string = result;
|
||||
self.len += 1;
|
||||
return;
|
||||
}
|
||||
self.update_len();
|
||||
|
||||
let mut result: String = String::new();
|
||||
let mut length = 0;
|
||||
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||
length += 1;
|
||||
if index == at {
|
||||
length += 1;
|
||||
result.push(c);
|
||||
}
|
||||
result.push_str(grapheme);
|
||||
}
|
||||
self.len = length;
|
||||
self.string = result;
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, at: usize) {
|
||||
if at >= self.len() {
|
||||
return;
|
||||
} else {
|
||||
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
||||
let remainder: String = self.string[..].graphemes(true).skip(at + 1).collect();
|
||||
result.push_str(&remainder);
|
||||
self.string = result;
|
||||
}
|
||||
self.update_len();
|
||||
|
||||
let mut result: String = String::new();
|
||||
let mut length = 0;
|
||||
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||
if index != at {
|
||||
length += 1;
|
||||
result.push_str(grapheme);
|
||||
}
|
||||
}
|
||||
self.len = length;
|
||||
self.string = result;
|
||||
}
|
||||
|
||||
pub fn append(&mut self, new: &Self) {
|
||||
self.string = format!("{}{}", self.string, new.string);
|
||||
self.update_len();
|
||||
self.len += new.len;
|
||||
}
|
||||
|
||||
pub fn split(&mut self, at: usize) -> Self {
|
||||
let beginning: String = self.string[..].graphemes(true).take(at).collect();
|
||||
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
||||
self.string = beginning;
|
||||
self.update_len();
|
||||
Self::from(&remainder[..])
|
||||
let mut row: String = String::new();
|
||||
let mut length = 0;
|
||||
|
||||
let mut split_row: String = String::new();
|
||||
let mut split_length = 0;
|
||||
|
||||
for (index, grapheme) in self.string[..].graphemes(true).enumerate() {
|
||||
if index < at {
|
||||
length += 1;
|
||||
row.push_str(grapheme);
|
||||
} else {
|
||||
split_length += 1;
|
||||
split_row.push_str(grapheme);
|
||||
}
|
||||
}
|
||||
|
||||
self.string = row;
|
||||
self.len = length;
|
||||
Self {
|
||||
string: split_row,
|
||||
len: split_length,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_bytes(&self) -> &[u8] {
|
||||
|
Loading…
x
Reference in New Issue
Block a user