From 7a2478218130fdf1528981ed1cdea94d227070b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Wed, 8 May 2024 21:51:38 +0200 Subject: [PATCH] x and y axis movenemtn --- src/app.rs | 58 +++++++++++++++++++++++++++++++++++++------------- src/handler.rs | 16 +++++++++----- src/ui.rs | 13 +++++++++-- 3 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/app.rs b/src/app.rs index 418e057..b78cdee 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,16 +2,22 @@ use std::error; pub type AppResult = std::result::Result>; +#[derive(Debug)] +pub struct Direction { + pub x: i8, + pub y: i8, +} + #[derive(Debug)] pub struct Position { - pub x: u16, - pub y: u16, + pub x: usize, + pub y: usize, } #[derive(Debug)] pub struct App { pub running: bool, - pub content: String, + pub content: Vec, pub cursor_position: Position, pub cursor_offset: Position, } @@ -20,7 +26,7 @@ impl Default for App { fn default() -> Self { Self { running: true, - content: String::new(), + content: Vec::new(), cursor_position: Position { x: 0, y: 0 }, cursor_offset: Position { x: 0, y: 0 }, } @@ -38,25 +44,47 @@ impl App { self.running = false; } - pub fn append_char(&mut self, c: char) { - self.content.push(c); + pub fn insert_char(&mut self, c: char) { + while self.cursor_position.y >= self.content.len() { + self.content.push(String::new()); + } + self.content[self.cursor_position.y].insert(self.cursor_position.x, c); self.cursor_position.x += 1; } + pub fn add_new_line(&mut self) { + self.cursor_position.y += 1; + self.cursor_position.x = 0; + } + pub fn pop_char(&mut self) { - self.content.pop(); - if let Some(previous_char) = self.cursor_position.x.checked_sub(1) { - self.cursor_position.x = previous_char; + self.content[self.cursor_position.y].pop(); + + if self.cursor_position.x > 0 { + self.cursor_position.x -= 1; } } - pub fn move_cursor(&mut self, direction: i8) { - if direction < 0 { - if let Some(previous_char) = self.cursor_position.x.checked_sub(1) { - self.cursor_position.x = previous_char; + pub fn move_cursor(&mut self, direction: Direction) { + if direction.x < 0 && self.cursor_position.x > 0 { + self.cursor_position.x -= 1; + } 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_position.x += 1; + } + } + } + if direction.y > 0 && self.cursor_position.y > 0 { + 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(); + } + } else if direction.y < 0 && self.cursor_position.y < self.content.len() - 1 { + 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(); } - } else { - self.cursor_position.x += 1; } } } diff --git a/src/handler.rs b/src/handler.rs index ac1c73d..39c0195 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,4 +1,4 @@ -use crate::app::{App, AppResult}; +use crate::app::{App, AppResult, Direction}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { @@ -9,20 +9,26 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { } } KeyCode::Enter => { - app.append_char('\n'); + app.add_new_line(); } KeyCode::Backspace => { app.pop_char(); } KeyCode::Left => { - app.move_cursor(-1); + app.move_cursor(Direction { x: -1, y: 0 }); } KeyCode::Right => { - app.move_cursor(1); + app.move_cursor(Direction { x: 1, y: 0 }); + } + KeyCode::Up => { + app.move_cursor(Direction { x: 0, y: 1 }); + } + KeyCode::Down => { + app.move_cursor(Direction { x: 0, y: -1 }); } _ => { if let KeyCode::Char(c) = key_event.code { - app.append_char(c) + app.insert_char(c) } } } diff --git a/src/ui.rs b/src/ui.rs index 0e5afbe..414d2ea 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,6 +1,7 @@ use ratatui::{ layout::Alignment, style::{Color, Style}, + text::Line, widgets::{Block, BorderType, Paragraph}, Frame, }; @@ -8,8 +9,13 @@ use ratatui::{ use crate::app::App; pub fn render(app: &mut App, frame: &mut Frame) { + let mut content_lines: Vec = app.content.iter().map(|s| s.as_str().into()).collect(); + + content_lines.push(Line::from(format!("{}", app.cursor_position.x))); + content_lines.push(Line::from(format!("{}", app.cursor_position.y))); + frame.render_widget( - Paragraph::new(format!("{}", app.content)) + Paragraph::new(content_lines) .block( Block::bordered() .title("RustEdit") @@ -19,5 +25,8 @@ pub fn render(app: &mut App, frame: &mut Frame) { .style(Style::default().fg(Color::LightBlue).bg(Color::Black)), frame.size(), ); - frame.set_cursor(app.cursor_position.x + 1, app.cursor_position.y + 1); + frame.set_cursor( + app.cursor_position.x as u16 + 1, + app.cursor_position.y as u16 + 1, + ); }