Skip to content

Commit

Permalink
feat(explorer): add folder highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 22, 2023
1 parent 735c747 commit 653617c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
70 changes: 42 additions & 28 deletions src/commands/explorer.rs
Original file line number Diff line number Diff line change
@@ -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| {
Expand All @@ -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<String> {
fn get_file_list(directory: &str) -> Vec<String> {
let paths = fs::read_dir(directory).unwrap();

let mut files: Vec<String> = vec![];
Expand Down
15 changes: 10 additions & 5 deletions src/commands/read_file.rs
Original file line number Diff line number Diff line change
@@ -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 {}

Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 653617c

Please sign in to comment.