Skip to content

Commit

Permalink
refactor: move command to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 17, 2023
1 parent 21a200a commit 6326801
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 109 deletions.
9 changes: 1 addition & 8 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod actions;
pub mod command;
pub mod maps;
pub mod mode;
pub mod movement;
Expand All @@ -10,8 +9,6 @@ pub mod text;

use std::collections::HashMap;

use crate::core::Rectangle;
use crate::core::{editable_text::EditableText, Point};
use crate::{
buffer::{
maps::{
Expand All @@ -21,6 +18,7 @@ use crate::{
mode::BufferMode,
select::Select,
},
core::{Point, Rectangle},
editor::Editor,
};

Expand All @@ -36,7 +34,6 @@ pub struct Buffer {
pub cursor: Point<usize>,
pub lines: Vec<String>,
pub select: Select,
pub command: EditableText,
pub actions_command: ActionMap,
pub actions_insert: ActionMap,
pub actions_normal: ActionMap,
Expand All @@ -57,10 +54,6 @@ impl Buffer {
select: Select {
start: Point { x: 0, y: 0 },
},
command: EditableText {
text: String::new(),
cursor_x: 0,
},
actions_command: get_default_command_maps(),
actions_insert: get_default_insert_maps(),
actions_normal: get_default_normal_maps(),
Expand Down
14 changes: 0 additions & 14 deletions src/buffer/command.rs

This file was deleted.

29 changes: 5 additions & 24 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::collections::HashMap;

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

use super::ActionMap;

Expand Down Expand Up @@ -106,26 +103,10 @@ pub fn get_default_command_maps() -> ActionMap {
map.insert("esc", |editor| {
editor.get_active_buffer_mut().enter_normal_mode()
});
map.insert("enter", |editor| {
// TODO: Move this
let command = editor.get_active_buffer().command.text.trim();
if command == "e" {
let tab = editor.get_active_tab_mut();
let mut buffer = tab.create_new_buffer();
initialize_explorer_buffer(&mut buffer, String::from("."));
} else {
editor.get_active_buffer_mut().run_command();
}
});
map.insert("backspace", |editor| {
editor.get_active_buffer_mut().command.delete_char()
});
map.insert("left", |editor| {
editor.get_active_buffer_mut().command.move_left()
});
map.insert("right", |editor| {
editor.get_active_buffer_mut().command.move_right()
});
map.insert("enter", |editor| editor.run_command());
map.insert("backspace", |editor| editor.command.delete_char());
map.insert("left", |editor| editor.command.move_left());
map.insert("right", |editor| editor.command.move_right());
return map;
}

Expand Down
1 change: 0 additions & 1 deletion src/buffer/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ impl Buffer {

pub fn enter_command_mode(&mut self) {
self.mode = BufferMode::Command;
self.command.reset();
}
}
8 changes: 0 additions & 8 deletions src/commands.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
use std::{fs, path::PathBuf};

use crate::buffer::Buffer;
use crate::{buffer::Buffer, editor::Editor};

fn get_file_list(directory: &String) -> Vec<String> {
let paths = fs::read_dir(directory).unwrap();

let mut files: Vec<String> = vec![];

for entry in paths {
let path = entry
.unwrap()
.path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
pub struct ExplorerCommand {}

files.push(path);
impl ExplorerCommand {
pub fn run(editor: &mut Editor) {
let mut buffer = editor.get_active_tab_mut().create_new_buffer();
initialize_explorer_buffer(&mut buffer, String::from("."));
}

files.sort();

files.insert(0, String::from(".."));
files.insert(1, String::from("."));

files
}

pub fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) {
fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) {
let files = get_file_list(&base_path);

buffer.file_name = Some(base_path);
Expand Down Expand Up @@ -60,8 +43,8 @@ pub fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) {
let md = fs::metadata(&path).unwrap();

if md.is_file() {
buffer.command.text = format!("e {}", path);
buffer.run_command();
editor.command.text = format!("e {}", path);
editor.run_command();
} else if md.is_dir() {
buffer.file_name = Some(path.clone());

Expand All @@ -80,3 +63,29 @@ pub fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) {

buffer.set_size(buffer.area.clone());
}

fn get_file_list(directory: &String) -> Vec<String> {
let paths = fs::read_dir(directory).unwrap();

let mut files: Vec<String> = vec![];

for entry in paths {
let path = entry
.unwrap()
.path()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();

files.push(path);
}

files.sort();

files.insert(0, String::from(".."));
files.insert(1, String::from("."));

files
}
3 changes: 3 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod explorer;
pub mod read_file;
pub mod write_file;
32 changes: 16 additions & 16 deletions src/commands/read_file.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use std::fs::File;
use std::io::Read;

use crate::buffer::Buffer;
use crate::editor::Editor;

pub struct ReadFileCommand {}

impl ReadFileCommand {
pub fn run(buffer: &mut Buffer) {
if buffer.command.text.len() > 2 {
let file_name = &buffer.command.text[2..];
if file_name.starts_with("~/") {
let mut home_path = home::home_dir().unwrap();
home_path.push(&file_name[2..]);
buffer.file_name = Some(home_path.display().to_string());
} else {
buffer.file_name = Some(String::from(file_name));
}
let mut content = String::new();
let file_path = buffer.file_name.as_ref().unwrap();
let mut file = File::open(file_path).unwrap();
file.read_to_string(&mut content).unwrap();
buffer.set_content(content);
pub fn run(editor: &mut Editor) {
let command = editor.command.text.clone();
let buffer = editor.get_active_buffer_mut();
let file_name = &command.trim()[2..];
if file_name.starts_with("~/") {
let mut home_path = home::home_dir().unwrap();
home_path.push(&file_name[2..]);
buffer.file_name = Some(home_path.display().to_string());
} else {
buffer.file_name = Some(String::from(file_name));
}
let mut content = String::new();
let file_path = buffer.file_name.as_ref().unwrap();
let mut file = File::open(file_path).unwrap();
file.read_to_string(&mut content).unwrap();
buffer.set_content(content);
}
}
12 changes: 7 additions & 5 deletions src/commands/write_file.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use std::fs::File;
use std::io::Write;

use crate::buffer::Buffer;
use crate::editor::Editor;

pub struct WriteFileCommand {}

impl WriteFileCommand {
pub fn run(buffer: &mut Buffer) {
pub fn run(editor: &mut Editor) {
let command = editor.command.text.clone();
let buffer = editor.get_active_buffer_mut();
let path;

if buffer.command.text.trim() == "w" {
if command.trim() == "w" {
if let Some(file_name) = &buffer.file_name {
path = file_name.as_str();
} else {
return;
}
} else if buffer.command.text.trim().len() > 2 {
path = &buffer.command.text[2..];
} else if command.trim().len() > 2 {
path = &command[2..];
} else {
return;
}
Expand Down
27 changes: 25 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
buffer::{mode::BufferMode, Buffer},
core::{key::Key, Rectangle},
commands::{
explorer::ExplorerCommand, read_file::ReadFileCommand, write_file::WriteFileCommand,
},
core::{editable_text::EditableText, key::Key, Rectangle},
tab::Tab,
};

Expand All @@ -11,6 +14,7 @@ pub struct Editor {
pub status_area: Rectangle<u16>,
pub tabs: Vec<Tab>,
pub active_tab: usize,
pub command: EditableText,
}

impl Editor {
Expand All @@ -22,6 +26,10 @@ impl Editor {
status_area: Rectangle::zero(),
tabs: vec![],
active_tab: 0,
command: EditableText {
text: String::new(),
cursor_x: 0,
},
};

editor.set_size(area);
Expand Down Expand Up @@ -95,10 +103,25 @@ impl Editor {
None => {
if !key.ctrl && !key.win && !key.alt && key.code.len() == 1 {
let ch = key.code.chars().nth(0).unwrap();
buffer.command.insert_char(ch);
self.command.insert_char(ch);
}
}
},
}
}

pub fn run_command(&mut self) {
let command = self.command.text.trim();

if command == "w" || command.starts_with("w ") {
WriteFileCommand::run(self);
} else if command == "e" {
ExplorerCommand::run(self);
} else if command.starts_with("e ") {
ReadFileCommand::run(self);
}

self.command.reset();
self.get_active_buffer_mut().enter_normal_mode();
}
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub mod buffer;
pub mod commands;
pub mod core;
pub mod editor;
pub mod plugins;
pub mod screen;
pub mod tab;
pub mod terminal;
Expand Down
1 change: 0 additions & 1 deletion src/plugins/explorer/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/plugins/mod.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl Screen {
if let BufferMode::Command = buffer.mode {
let command_row = screen.size.height - 1;
screen.clear_row(command_row);
screen.print(command_row, 0, &format!(":{}", buffer.command.text));
screen.print(command_row, 0, &format!(":{}", editor.command.text));

screen.cursor.x = buffer.command.cursor_x as u16 + 1;
screen.cursor.x = editor.command.cursor_x as u16 + 1;
screen.cursor.y = command_row;
}

Expand Down

0 comments on commit 6326801

Please sign in to comment.