Skip to content

Commit

Permalink
x and y axis movenemtn
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 8, 2024
1 parent 0064758 commit 7a24782
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
58 changes: 43 additions & 15 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@ use std::error;

pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;

#[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<String>,
pub cursor_position: Position,
pub cursor_offset: Position,
}
Expand All @@ -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 },
}
Expand All @@ -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;
}
}
}
16 changes: 11 additions & 5 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand All @@ -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)
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use ratatui::{
layout::Alignment,
style::{Color, Style},
text::Line,
widgets::{Block, BorderType, Paragraph},
Frame,
};

use crate::app::App;

pub fn render(app: &mut App, frame: &mut Frame) {
let mut content_lines: Vec<Line> = 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")
Expand All @@ -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,
);
}

0 comments on commit 7a24782

Please sign in to comment.