Skip to content

Commit

Permalink
prompt part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 21, 2024
1 parent bd12b69 commit d343b20
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
56 changes: 40 additions & 16 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error, fs};
use std::{cmp::min, error, fs};

use ratatui::layout::Rect;

Expand Down Expand Up @@ -165,21 +165,30 @@ impl App {
return;
}
self.prompt.pop();
self.move_cursor(Direction { x: -1, y: 0 });
return;
}

if self.content.len() == 0 {
return;
}

if self.content[self.cursor_position.y + self.cursor_offset.y].len() == 0 {
self.content
.remove(self.cursor_position.y + self.cursor_offset.y);
let pos = self.get_cursor_positon();

if self.content[pos.y].len() == 0 {
self.content.remove(pos.y);

self.move_cursor(Direction { x: 0, y: 1 });
} else if self.cursor_position.x == 0 && self.cursor_position.y > 0 {
let lower_line = self.content.remove(pos.y);

self.cursor_position.x = self.content[pos.y - 1].len();

self.content[pos.y - 1].push_str(&lower_line);

self.move_cursor(Direction { x: 0, y: 1 });
} else {
self.content[self.cursor_position.y + self.cursor_offset.y]
.remove(self.cursor_position.x + self.cursor_offset.x - 1);
self.content[pos.y].remove(pos.x - 1);

self.move_cursor(Direction { x: -1, y: 0 });
}
Expand All @@ -197,25 +206,42 @@ impl App {
return;
}

if direction.x < 0 && self.cursor_position.x + self.cursor_offset.x > 0 {
if self.cursor_position.x == 0 {
self.cursor_offset.x -= 1;
} else {
self.cursor_position.x -= 1;
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;
}
} else if direction.x > 0 {
if let Some(line) = self.content.get(self.cursor_position.y) {
if line.len() > self.cursor_position.x + self.cursor_offset.x {
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;
}
}
}
}

if direction.y > 0 && self.cursor_position.y + self.cursor_offset.y > 0 {
if direction.y > 0 && pos.y > 0 {
if self.cursor_position.y == 0 {
self.cursor_offset.y -= 1;
} else {
Expand All @@ -226,9 +252,7 @@ impl App {
self.cursor_position.x = self.content[self.cursor_position.y].len();
self.cursor_offset.x = 0;
}
} else if direction.y < 0
&& self.cursor_position.y + self.cursor_offset.y < self.content.len().saturating_sub(1)
{
} 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;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
(1..=pos.y)
.rev()
.chain(std::iter::once(pos.y))
.chain(1..=app.content.len() - pos.y - 1)
.chain(1..=app.content.len().saturating_sub(pos.y + 1))
.collect::<Vec<_>>()
}
} else {
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
let status_line: Line = if app.is_prompt {
Line::from(format!("{:<}", app.prompt))
} else {
Line::from("Press Ctrl + C to quit").centered()
Line::from("Press Ctrl + C to quit, Ctrl + S to save.").centered()
};

let layout = Layout::default()
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn render(app: &mut App, frame: &mut Frame) {
frame.render_widget(status_line, layout[3]);

frame.set_cursor(
(app.cursor_position.x + (if app.is_prompt { 1 } else { numbers_width + 1 })) as u16,
(app.cursor_position.x + (if app.is_prompt { 0 } else { numbers_width + 1 })) as u16,
app.cursor_position.y as u16 + 1,
);
}

0 comments on commit d343b20

Please sign in to comment.