From 08081d54e206d5a50b524fa44649da8ac10b5556 Mon Sep 17 00:00:00 2001 From: Maddie <32415621+SpyHoodle@users.noreply.github.com> Date: Fri, 18 Nov 2022 14:20:49 +0000 Subject: [PATCH] added buffer types and takes in buffer name better --- src/core/buffer.rs | 30 ++++++++++++++++++++++-------- src/core/editor.rs | 7 ++++--- src/main.rs | 8 ++------ src/tui/components/statusbar.rs | 27 +++++++++++++++++---------- src/tui/ui.rs | 5 ++++- 5 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/core/buffer.rs b/src/core/buffer.rs index 805b90f..f7c277a 100644 --- a/src/core/buffer.rs +++ b/src/core/buffer.rs @@ -1,22 +1,36 @@ use std::path::PathBuf; +#[derive(PartialEq)] +pub enum BufferKind { + Scratch, + Write, + Read, +} + +impl BufferKind { + pub fn as_str(&self) -> &str { + match self { + BufferKind::Scratch => "*Scratch*", + BufferKind::Write => "Write", + BufferKind::Read => "Read", + } + } +} + pub struct Buffer<'a> { pub data: Vec, pub path: PathBuf, + pub kind: BufferKind, pub name: &'a str, } impl<'a> Buffer<'a> { - pub fn new(dir_path: PathBuf, file_name: &'a str) -> Self { - let name = if file_name.len() > 0 { - file_name - } else { - "[No Name]" - }; - + pub fn new(path: PathBuf, name: &'a str, kind: BufferKind) -> Self { + // Return a buffer Self { data: vec![String::from("")], - path: dir_path, + path, + kind, name, } } diff --git a/src/core/editor.rs b/src/core/editor.rs index 4f95161..513a88b 100644 --- a/src/core/editor.rs +++ b/src/core/editor.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; - use crate::core::buffer::Buffer; +use crate::core::buffer::BufferKind; pub struct Config<'a> { pub logo: &'a str, @@ -43,10 +43,11 @@ pub struct Editor<'a> { } impl<'a> Editor<'a> { - pub fn new(dir_path: PathBuf, file_name: &'a str) -> Self { + pub fn new(path: PathBuf, buffer_name: &'a str) -> Self { + let buffer_kind = if path.to_str().unwrap().len() > 1 { BufferKind::Write } else { BufferKind::Scratch }; Editor { config: Config::new(), - buffer: Box::new(Buffer::new(dir_path, file_name)), + buffer: Box::new(Buffer::new(path, buffer_name, buffer_kind)), cursors: Vec::from([0]), mode: Mode::Normal, } diff --git a/src/main.rs b/src/main.rs index bda93b0..004ed55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,18 +8,14 @@ fn main() { let args: Vec = env::args().collect(); // Collect the file path - let current_dir = env::current_dir().unwrap(); - let file_path = if args.len() > 1 { PathBuf::from(&args[1]) } else { current_dir.to_path_buf() }; + let file_path = if args.len() > 1 { PathBuf::from(&args[1]) } else { PathBuf::from("") }; // Collect the file name let file_name = file_path.clone(); let file_name = if args.len() > 1 { file_name.file_name().unwrap().to_str().unwrap() } else { "" }; - // Collect the directory path - let dir_path = file_path.parent().unwrap().to_path_buf(); - // Create a new editor - let lambda = core::editor::Editor::new(dir_path, file_name); + let lambda = core::editor::Editor::new(file_path, file_name); // Initalise a screen let mut screen = terminal::screen::Screen::new().unwrap(); diff --git a/src/tui/components/statusbar.rs b/src/tui/components/statusbar.rs index 74989e2..b51fdf3 100644 --- a/src/tui/components/statusbar.rs +++ b/src/tui/components/statusbar.rs @@ -13,9 +13,11 @@ pub fn draw(screen: &mut Screen, editor: &Editor) -> Result<(), ()> { let mode_string = &utils::with_spaces(editor.mode.as_str()) as &str; // Get the current open file name let file_name = &utils::with_spaces(editor.buffer.name) as &str; + // Get the current buffer kind + let buffer_kind = &utils::with_spaces(editor.buffer.kind.as_str()) as &str; // Calculate the total length of all the status bar components - let total_length = editor_logo.len() + mode_string.len() + file_name.len() + 1; + let total_length = editor_logo.len() + mode_string.len() + file_name.len() + buffer_kind.len() + 1; // If the screen isn't wide enough, panic as we can't draw the status bar if screen.size.width < total_length { @@ -36,16 +38,21 @@ pub fn draw(screen: &mut Screen, editor: &Editor) -> Result<(), ()> { Coords::from(x, status_height), ); - // Calculate where to write the file name let x = x + mode_string.len(); - // Write the current file name - screen.write_at( - file_name.magenta().bold().reverse().to_string(), - Coords::from(x, status_height), - ); - - // Draw the rest of the status bar - let x = x + file_name.len(); + // Draws the file name if it has a length, if not then it will draw the buffer type + if editor.buffer.name.len() > 0 { + screen.write_at( + file_name.magenta().bold().reverse().to_string(), + Coords::from(x, status_height), + ); + } else { + screen.write_at( + buffer_kind.blue().bold().reverse().to_string(), + Coords::from(x, status_height), + ); + } + + let x = if editor.buffer.name.len() > 0 { x + file_name.len() } else { x + buffer_kind.len() }; screen.write_at( " ".repeat(screen.size.width - x).reverse().to_string(), Coords::from(x, status_height), diff --git a/src/tui/ui.rs b/src/tui/ui.rs index cab9d22..29d9d3a 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -1,4 +1,5 @@ use crate::core::editor::Editor; +use crate::core::buffer::BufferKind; use crate::terminal::screen::Screen; use crate::tui::components; use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers}; @@ -10,7 +11,9 @@ pub fn start(screen: &mut Screen, editor: Editor) { screen.refresh().unwrap(); // Draw the welcome message - components::welcome::draw(screen, &editor); + if editor.buffer.kind == BufferKind::Scratch { + components::welcome::draw(screen, &editor); + }; // Draw the status bar components::statusbar::draw(screen, &editor).unwrap();