Skip to content

Commit

Permalink
refactor: refactor and fix cursor position on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 16, 2023
1 parent 758107a commit 705ef56
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ impl Buffer {
self.info_area.width = 2 + self.lines.len().to_string().len() as u16;
self.text_area.x = self.info_area.x + self.info_area.width;
self.text_area.width = area.width - self.info_area.width;
self.move_cursor(self.cursor.y, self.cursor.x);
}
}
8 changes: 3 additions & 5 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use crate::{
buffer::actions::find_next_word_position::find_next_word_position,
plugins::explorer::explorer_buffer::create_explorer_buffer,
plugins::explorer::explorer_buffer::initialize_explorer_buffer,
};

use super::ActionMap;
Expand Down Expand Up @@ -110,11 +110,9 @@ pub fn get_default_command_maps() -> ActionMap {
// TODO: Move this
let command = editor.get_active_buffer().command.text.trim();
if command == "e" {
let tab = editor.get_active_tab();
let buffer = create_explorer_buffer(String::from("."), tab.area.clone());
let tab = editor.get_active_tab_mut();
tab.buffers.push(buffer);
tab.active_buffer = tab.buffers.len() - 1;
let mut buffer = tab.create_new_buffer();
initialize_explorer_buffer(&mut buffer, String::from("."));
} else {
editor.get_active_buffer_mut().run_command();
}
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/explorer/explorer_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs, path::PathBuf};

use crate::{buffer::Buffer, core::Rectangle};
use crate::buffer::Buffer;

fn get_file_list(directory: &String) -> Vec<String> {
let paths = fs::read_dir(directory).unwrap();
Expand Down Expand Up @@ -28,13 +28,12 @@ fn get_file_list(directory: &String) -> Vec<String> {
files
}

pub fn create_explorer_buffer(base_path: String, area: Rectangle<u16>) -> Buffer {
pub fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) {
let files = get_file_list(&base_path);

let mut buffer = Buffer::new(area);
buffer.file_name = Some(base_path);

buffer.lines.remove(0);
buffer.lines.clear();
for file in files {
buffer.lines.push(file);
}
Expand Down Expand Up @@ -75,8 +74,9 @@ pub fn create_explorer_buffer(base_path: String, area: Rectangle<u16>) -> Buffer
}

buffer.move_cursor(0, 0);
buffer.set_size(buffer.area.clone());
}
});

buffer
buffer.set_size(buffer.area.clone());
}
2 changes: 1 addition & 1 deletion src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ impl Screen {
}

pub fn from(editor: &Editor) -> Self {
let tab = editor.get_active_tab();
let mut palette = Screen::new(editor.area.height, editor.area.width);

let tab = editor.get_active_tab();
let buffer = tab.get_active_buffer();

palette.cursor.x = buffer.text_area.x + (buffer.cursor.x - buffer.scroll.x) as u16;
Expand Down

0 comments on commit 705ef56

Please sign in to comment.