Skip to content

Commit

Permalink
scrolling part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed May 14, 2024
1 parent 5740316 commit 8b8d9da
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 36 deletions.
121 changes: 121 additions & 0 deletions ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use std::error;

use ratatui::layout::Rect;

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: usize,
pub y: usize,
}

#[derive(Debug)]
pub struct App {
pub running: bool,
pub content: Vec<String>,
pub cursor_position: Position,
pub cursor_offset: Position,
pub opened_filename: String,
pub window_size: Rect,
}

impl Default for App {
fn default() -> Self {
Self {
running: true,
content: Vec::new(),
cursor_position: Position { x: 0, y: 0 },
cursor_offset: Position { x: 0, y: 0 },
opened_filename: String::new(),
window_size: Rect::new(0, 0, 0, 0),
}
}
}

impl App {
pub fn new() -> Self {
Self::default()
}

pub fn tick(&self) {}

pub fn quit(&mut self) {
self.running = false;
}

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 + self.cursor_offset.y]
.insert(self.cursor_position.x + self.cursor_offset.x, c);
self.move_cursor(Direction { x: 1, y: 0 });
}

pub fn add_new_line(&mut self) {
self.content.insert(
self.cursor_position.y + self.cursor_offset.y + 1,
String::new(),
);
self.move_cursor(Direction { x: 0, y: -1 })
}

pub fn pop_char(&mut self) {
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: Direction) {
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;
}
} 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 self.window_size.width.saturating_sub(1) > self.cursor_position.x as u16 {
self.cursor_position.x += 1;
} else {
self.cursor_offset.x += 1;
}
}
}
}

if direction.y > 0 && self.cursor_position.y + self.cursor_offset.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();
}
} else if direction.y < 0
&& self.cursor_position.y + self.cursor_offset.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 {
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();
}
}
}
}
34 changes: 26 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,39 @@ impl App {
while self.cursor_position.y >= self.content.len() {
self.content.push(String::new());
}

self.content[self.cursor_position.y + self.cursor_offset.y]
.insert(self.cursor_position.x + self.cursor_offset.x, c);
self.cursor_position.x += 1;
self.move_cursor(Direction { x: 1, y: 0 });
}

pub fn add_new_line(&mut self) {
self.content
.insert(self.cursor_position.y + self.cursor_offset.y, String::new());
self.cursor_position.y += 1;
self.cursor_position.x = 0;
while self.cursor_position.y >= self.content.len() {
self.content.push(String::new());
}

self.content.insert(
self.cursor_position.y + self.cursor_offset.y + 1,
String::new(),
);
self.move_cursor(Direction { x: 0, y: -1 });
}

pub fn pop_char(&mut self) {
self.content[self.cursor_position.y].pop();
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);

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);

if self.cursor_position.x > 0 {
self.cursor_position.x -= 1;
self.move_cursor(Direction { x: -1, y: 0 });
}
}

Expand Down Expand Up @@ -102,6 +118,7 @@ impl App {

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;
}
} else if direction.y < 0
&& self.cursor_position.y + self.cursor_offset.y < self.content.len().saturating_sub(1)
Expand All @@ -114,6 +131,7 @@ impl App {

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;
}
}
}
Expand Down
55 changes: 29 additions & 26 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,38 @@ use crate::app::{App, AppResult, Direction};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
match key_event.code {
KeyCode::Char('c') | KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL {
match key_event.modifiers {
KeyModifiers::CONTROL => {
if key_event.code == KeyCode::Char('c') || key_event.code == KeyCode::Char('C') {
app.quit();
}
}
KeyCode::Enter => {
app.add_new_line();
}
KeyCode::Backspace => {
app.pop_char();
}
KeyCode::Left => {
app.move_cursor(Direction { x: -1, y: 0 });
}
KeyCode::Right => {
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.insert_char(c)
_ => match key_event.code {
KeyCode::Enter => {
app.add_new_line();
}
}
}
KeyCode::Backspace => {
app.pop_char();
}
KeyCode::Left => {
app.move_cursor(Direction { x: -1, y: 0 });
}
KeyCode::Right => {
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.insert_char(c)
}
}
},
};

Ok(())
}
4 changes: 2 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub fn render(app: &mut App, frame: &mut Frame) {

let cursor_position_status = Line::from(format!(
" {:>2}:{:<2} ",
app.cursor_position.y + 1,
app.cursor_position.x + 1,
app.cursor_position.y + app.cursor_offset.y + 1,
app.cursor_position.x + app.cursor_offset.x + 1,
))
.right_aligned()
.style(Style::default().bg(Color::Rgb(128, 192, 255)).bold());
Expand Down

0 comments on commit 8b8d9da

Please sign in to comment.