Skip to content

Commit

Permalink
move cursor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 22, 2024
1 parent d343b20 commit df3ea92
Showing 1 changed file with 60 additions and 44 deletions.
104 changes: 60 additions & 44 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,60 +209,76 @@ impl App {
let pos = self.get_cursor_positon();

if direction.x < 0 {
if pos.x > 0 {
if self.cursor_position.x == 0 {
self.cursor_offset.x -= 1;
} else {
self.cursor_position.x -= 1;
}
} else if self.cursor_position.y > 0 {
self.cursor_position.y -= 1;
let len = self.content[self.cursor_position.y].len();
self.cursor_position.x = min(len, self.window_size.width.into());
self.cursor_offset.x = len - self.cursor_position.x;
}
self.move_cursor_left(pos);
} else if direction.x > 0 {
if let Some(line) = self.content.get(self.cursor_position.y) {
if line.len() > pos.x {
if self.window_size.width.saturating_sub(1) > self.cursor_position.x as u16 {
self.cursor_position.x += 1;
} else {
self.cursor_offset.x += 1;
}
} else if line.len() == pos.x && pos.y < self.content.len() - 1 {
self.cursor_offset.x = 0;
self.cursor_position.x = 0;
if self.cursor_position.y + 1 > self.window_size.height.into() {
self.cursor_offset.y += 1;
} else {
self.cursor_position.y += 1;
}
}
}
self.move_cursor_right(pos);
}

if direction.y > 0 && pos.y > 0 {
if self.cursor_position.y == 0 {
self.cursor_offset.y -= 1;
} else {
self.cursor_position.y -= 1;
}

if self.cursor_position.x > self.content[self.cursor_position.y].len() {
self.cursor_position.x = self.content[self.cursor_position.y].len();
self.cursor_offset.x = 0;
}
self.move_cursor_up();
} else if direction.y < 0 && pos.y < self.content.len().saturating_sub(1) {
if self.window_size.height.saturating_sub(4) > self.cursor_position.y as u16 {
self.cursor_position.y += 1;
self.move_cursor_down();
}
}

fn move_cursor_left(&mut self, pos: Position) {
if pos.x > 0 {
if self.cursor_position.x == 0 {
self.cursor_offset.x -= 1;
} else {
self.cursor_offset.y += 1;
self.cursor_position.x -= 1;
}
} else if self.cursor_position.y > 0 {
self.cursor_position.y -= 1;
let len = self.content[self.cursor_position.y].len();
self.cursor_position.x = min(len, self.window_size.width.into());
self.cursor_offset.x = len - self.cursor_position.x;
}
}

if self.cursor_position.x > self.content[self.cursor_position.y].len() {
self.cursor_position.x = self.content[self.cursor_position.y].len();
fn move_cursor_right(&mut self, pos: Position) {
if let Some(line) = self.content.get(self.cursor_position.y) {
if line.len() > pos.x {
if self.window_size.width.saturating_sub(1) > self.cursor_position.x as u16 {
self.cursor_position.x += 1;
} else {
self.cursor_offset.x += 1;
}
} else if line.len() == pos.x && pos.y < self.content.len() - 1 {
self.cursor_offset.x = 0;
self.cursor_position.x = 0;
if self.cursor_position.y + 1 > self.window_size.height.into() {
self.cursor_offset.y += 1;
} else {
self.cursor_position.y += 1;
}
}
}
}

fn move_cursor_up(&mut self) {
if self.cursor_position.y == 0 {
self.cursor_offset.y -= 1;
} else {
self.cursor_position.y -= 1;
}

if self.cursor_position.x > self.content[self.cursor_position.y].len() {
self.cursor_position.x = self.content[self.cursor_position.y].len();
self.cursor_offset.x = 0;
}
}

fn move_cursor_down(&mut self) {
if self.window_size.height.saturating_sub(4) > self.cursor_position.y as u16 {
self.cursor_position.y += 1;
} else {
self.cursor_offset.y += 1;
}

if self.cursor_position.x > self.content[self.cursor_position.y].len() {
self.cursor_position.x = self.content[self.cursor_position.y].len();
self.cursor_offset.x = 0;
}
}
}

0 comments on commit df3ea92

Please sign in to comment.