Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::path::PathBuf;

use crate::app::messages::Message;
use crate::app::mode::Mode;

pub struct EditorContext {
pub mode: Mode,
pub command_buffer: String,
pub file_path: String,
pub file_path: Option<PathBuf>,
pub current_message: Option<Message>,
pub savedialog: bool,
pub quit_after_save: bool
}
43 changes: 33 additions & 10 deletions src/commands/executor.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
use std::io::stdout;
use std::{
io::stdout,
path::{self, Path, PathBuf},
};

use crossterm::{
execute,
terminal::{self, LeaveAlternateScreen},
};

use crate::{app::{context::{self, EditorContext}, messages::MessageLevel}, gui::status_bar::send_message, io::save::save};
use crate::{Buffer, Status};
use crate::{
app::{
context::{self, EditorContext},
messages::MessageLevel,
},
gui::status_bar::send_message,
io::save::save,
};

use super::parser::Command;

pub fn execute(
command: Command,
buffer: &Buffer,
path: &str,
path: &Option<PathBuf>,
) -> std::io::Result<Status> {
match command {
Command::Quit => Ok(Status::Quit),

Command::Write => {
save(buffer, &path.to_string())?;
Command::Write(command_path) => {
match command_path {
Some(new_path) => save(buffer, std::path::Path::new(&new_path))?,
None => {
if let Some(path) = path {
save(buffer, path)?;
}
}
}
execute!(stdout(), crossterm::cursor::Hide)?;
Ok(Status::Continue)
}

Command::WriteQuit => {
save(buffer, &path.to_string())?;
terminal::disable_raw_mode()?;
execute!(stdout(), LeaveAlternateScreen)?;
Command::WriteQuit(command_path) => {
match command_path {
Some(new_path) => save(buffer, std::path::Path::new(&new_path))?,
None => {
if let Some(path) = path {
save(buffer, path)?;
}
}
}

Ok(Status::Quit)
}

Command::Unknown => Ok(Status::Continue),
}
}
}
12 changes: 7 additions & 5 deletions src/commands/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[derive(Debug, PartialEq, Eq)]
pub enum Command {
Quit,
Write,
WriteQuit,
Write(Option<String>),
WriteQuit(Option<String>),
Unknown,
}

Expand All @@ -13,10 +13,12 @@ pub fn parse(input: &str) -> Command {
return Command::Unknown;
};

let path = parts.next().map(String::from);

match name {
"q" => Command::Quit,
"w" => Command::Write,
"wq" => Command::WriteQuit,
"q" if path.is_none() => Command::Quit,
"w" => Command::Write(path),
"wq" => Command::WriteQuit(path),
_ => Command::Unknown,
}
}
29 changes: 19 additions & 10 deletions src/editor/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fs::read_to_string;
use std::{fs::read_to_string, io::ErrorKind};

#[derive(Debug)]
pub struct Buffer {
Expand All @@ -7,24 +7,33 @@ pub struct Buffer {
pub column: usize,
}

pub fn fetch(file: &str) -> Buffer {
pub fn fetch(file: &str) -> Result<Buffer, std::io::Error> {
let mut result = Vec::new();

for line in read_to_string(file)
.unwrap()
.lines()
{
result.push(line.to_string());
let content = read_to_string(file);

match content {
Ok(content) => {
for line in content.lines() {
result.push(line.to_string());
}
}
Err(error) => {
if error.kind() == ErrorKind::NotFound {
result.push(String::new());
} else {
return Err(error);
}
}
}

if result.is_empty() {
result.push(String::new());
}

Buffer {
Ok(Buffer {
lines: result,
row: 0,
column: 0,
}
})
}

10 changes: 6 additions & 4 deletions src/editor/editing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::editor::buffer::Buffer;
use crate::app::context::EditorContext;
use crate::app::mode::Mode;
use crate::editor::buffer::Buffer;

pub fn insert_char(c: char, buffer: &mut Buffer) {
let Some(line) = buffer.lines.get_mut(buffer.column) else {
Expand All @@ -16,11 +16,14 @@ pub fn insert_char(c: char, buffer: &mut Buffer) {
}

pub fn insert_forward(buffer: &mut Buffer, context: &mut EditorContext) {
buffer.row += 1;
let line_len = buffer.lines[buffer.column].chars().count();

if line_len > 0 && buffer.row < line_len {
buffer.row += 1;
}

context.mode = Mode::Insert;
}

pub fn insert_backward(buffer: &mut Buffer, context: &mut EditorContext) {
if buffer.row == 0 {
return;
Expand All @@ -30,7 +33,6 @@ pub fn insert_backward(buffer: &mut Buffer, context: &mut EditorContext) {
context.mode = Mode::Insert;
}


pub fn remove_char(buffer: &mut Buffer) {
if buffer.row > 0 {
let Some(line) = buffer.lines.get_mut(buffer.column) else {
Expand Down
28 changes: 13 additions & 15 deletions src/gui/display.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::{
editor::buffer::Buffer,
app::context::EditorContext,
explorer::tree::Explorer,
app::mode::Mode,
app::context::EditorContext, app::mode::Mode, editor::buffer::Buffer, explorer::tree::Explorer,
};

use crossterm::{
Expand All @@ -12,7 +9,7 @@ use crossterm::{
terminal,
};

use std::io::{stdout, Write};
use std::io::{Write, stdout};

use super::{editor_view, explorer_view, status_bar};

Expand Down Expand Up @@ -84,7 +81,7 @@ impl Display {
) -> std::io::Result<()> {
let mut stdout = stdout();

execute!(stdout, Hide)?;
queue!(stdout, Hide)?;

for cell in &mut self.cells {
*cell = Cell::default();
Expand Down Expand Up @@ -119,30 +116,31 @@ impl Display {
)?;
}

stdout.flush()?;

match context.mode {
Mode::Command => {
execute!(
let x = if context.savedialog {
(context.command_buffer.chars().count() + 9) as u16
} else {
(context.command_buffer.chars().count() + 1) as u16
};
queue!(
stdout,
MoveTo(
(context.command_buffer.chars().count() + 1) as u16,
self.height.saturating_sub(1) as u16,
),
MoveTo(x, self.height.saturating_sub(1) as u16,),
Show
)?;
}
Mode::Explorer => {
execute!(stdout, Hide)?;
}
_ => {
execute!(
queue!(
stdout,
MoveTo(buffer.row as u16, buffer.column as u16),
Show
)?;
}
}
stdout.flush()?;

Ok(())
}
Expand All @@ -158,4 +156,4 @@ impl Display {

self.cells = cells;
}
}
}
8 changes: 7 additions & 1 deletion src/gui/status_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ pub(super) fn draw_status_bar(
Mode::Normal => "-- NORMAL --".to_string(),
Mode::Insert => "-- INSERT --".to_string(),
Mode::Explorer => "-- EXPLORER --".to_string(),
Mode::Command => format!(":{}", context.command_buffer),
Mode::Command => {
if context.savedialog {
format!("Save as: {}", context.command_buffer)
} else {
format!(":{}", context.command_buffer)
}
},
};

let status = match context.current_message.as_ref() {
Expand Down
4 changes: 2 additions & 2 deletions src/io/save.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::editor::buffer::Buffer;

use std::fs::write;
use std::{fs::write, path::Path};

pub fn save(buffer: &Buffer, path: &String) -> std::io::Result<()> {
pub fn save(buffer: &Buffer, path: &Path) -> std::io::Result<()> {
let content = buffer.lines.join("\n");

write(path, content)?;
Expand Down
Loading
Loading