diff --git a/src/commands/explorer.rs b/src/commands/explorer.rs index f5bfb1e..bc9fb50 100644 --- a/src/commands/explorer.rs +++ b/src/commands/explorer.rs @@ -1,24 +1,55 @@ use std::{fs, path::PathBuf}; -use crate::{buffer::Buffer, editor::Editor}; +use crate::{ + buffer::{highlight::Highlight, Buffer}, + core::Style, + editor::Editor, + theme::{GREEN, SILVER}, +}; pub struct ExplorerCommand {} impl ExplorerCommand { - pub fn run(editor: &mut Editor) { + pub fn run(editor: &mut Editor, path: &str) { let mut buffer = editor.get_active_tab_mut().create_new_buffer(); - initialize_explorer_buffer(&mut buffer, String::from(".")); + initialize_explorer_buffer(&mut buffer, path); } } -fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) { - let files = get_file_list(&base_path); +const FOLDER_STYLE: &str = "ExplorerFolder"; - buffer.file_name = Some(base_path); +fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: &str) { + buffer.styles.insert( + FOLDER_STYLE, + Style { + fg: GREEN, + bg: SILVER, + bold: false, + italic: false, + underline: false, + }, + ); + let files = get_file_list(base_path); + + buffer.file_name = Some(base_path.to_owned()); buffer.lines.clear(); - for file in files { - buffer.lines.push(file); + buffer.highlights.clear(); + + for (i, file) in files.iter().enumerate() { + let mut file_path = PathBuf::from(base_path); + file_path.push(file); + + if file != "." && file != ".." && fs::metadata(file_path).unwrap().is_dir() { + buffer.highlights.push(Highlight { + name: FOLDER_STYLE, + row: i, + start: 0, + end: file.len() - 1, + }); + } + + buffer.lines.push(file.to_owned()); } buffer.actions_normal.insert("enter", |editor| { @@ -40,31 +71,14 @@ fn initialize_explorer_buffer(buffer: &mut Buffer, base_path: String) { path = String::from("."); } - let md = fs::metadata(&path).unwrap(); - - if md.is_file() { - editor.command.text = format!("e {}", path); - editor.run_command(); - } else if md.is_dir() { - buffer.file_name = Some(path.clone()); - - let files = get_file_list(&path); - - buffer.lines.clear(); - - for file in files { - buffer.lines.push(file); - } - - buffer.move_cursor(0, 0); - buffer.set_size(buffer.area.clone()); - } + editor.command.text = format!("e {}", path); + editor.run_command(); }); buffer.set_size(buffer.area.clone()); } -fn get_file_list(directory: &String) -> Vec { +fn get_file_list(directory: &str) -> Vec { let paths = fs::read_dir(directory).unwrap(); let mut files: Vec = vec![]; diff --git a/src/commands/read_file.rs b/src/commands/read_file.rs index 48cf9c1..ae4ceaa 100644 --- a/src/commands/read_file.rs +++ b/src/commands/read_file.rs @@ -1,7 +1,7 @@ -use std::fs::File; +use std::fs::{self, File}; use std::io::Read; -use crate::editor::Editor; +use crate::{commands::explorer::ExplorerCommand, editor::Editor}; pub struct ReadFileCommand {} @@ -19,8 +19,13 @@ impl ReadFileCommand { } 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); + if fs::metadata(&file_path).unwrap().is_dir() { + let path = file_path.clone(); + ExplorerCommand::run(editor, &path); + } else { + let mut file = File::open(file_path).unwrap(); + file.read_to_string(&mut content).unwrap(); + buffer.set_content(content); + } } } diff --git a/src/editor.rs b/src/editor.rs index 84028a6..03d24b3 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -139,7 +139,7 @@ impl Editor { if command == "w" || command.starts_with("w ") { WriteFileCommand::run(self); } else if command == "e" { - ExplorerCommand::run(self); + ExplorerCommand::run(self, "."); } else if command.starts_with("e ") { ReadFileCommand::run(self); } else if command == "f" {