added buffer types and takes in buffer name better

This commit is contained in:
Maddie 2022-11-18 14:20:49 +00:00
parent cb005ef441
commit 08081d54e2
5 changed files with 49 additions and 28 deletions

View File

@ -1,22 +1,36 @@
use std::path::PathBuf; 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 struct Buffer<'a> {
pub data: Vec<String>, pub data: Vec<String>,
pub path: PathBuf, pub path: PathBuf,
pub kind: BufferKind,
pub name: &'a str, pub name: &'a str,
} }
impl<'a> Buffer<'a> { impl<'a> Buffer<'a> {
pub fn new(dir_path: PathBuf, file_name: &'a str) -> Self { pub fn new(path: PathBuf, name: &'a str, kind: BufferKind) -> Self {
let name = if file_name.len() > 0 { // Return a buffer
file_name
} else {
"[No Name]"
};
Self { Self {
data: vec![String::from("")], data: vec![String::from("")],
path: dir_path, path,
kind,
name, name,
} }
} }

View File

@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::core::buffer::Buffer; use crate::core::buffer::Buffer;
use crate::core::buffer::BufferKind;
pub struct Config<'a> { pub struct Config<'a> {
pub logo: &'a str, pub logo: &'a str,
@ -43,10 +43,11 @@ pub struct Editor<'a> {
} }
impl<'a> 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 { Editor {
config: Config::new(), 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]), cursors: Vec::from([0]),
mode: Mode::Normal, mode: Mode::Normal,
} }

View File

@ -8,18 +8,14 @@ fn main() {
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
// Collect the file path // Collect the file path
let current_dir = env::current_dir().unwrap(); let file_path = if args.len() > 1 { PathBuf::from(&args[1]) } else { PathBuf::from("") };
let file_path = if args.len() > 1 { PathBuf::from(&args[1]) } else { current_dir.to_path_buf() };
// Collect the file name // Collect the file name
let file_name = file_path.clone(); let file_name = file_path.clone();
let file_name = if args.len() > 1 { file_name.file_name().unwrap().to_str().unwrap() } else { "" }; 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 // 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 // Initalise a screen
let mut screen = terminal::screen::Screen::new().unwrap(); let mut screen = terminal::screen::Screen::new().unwrap();

View File

@ -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; let mode_string = &utils::with_spaces(editor.mode.as_str()) as &str;
// Get the current open file name // Get the current open file name
let file_name = &utils::with_spaces(editor.buffer.name) as &str; 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 // 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 the screen isn't wide enough, panic as we can't draw the status bar
if screen.size.width < total_length { if screen.size.width < total_length {
@ -36,16 +38,21 @@ pub fn draw(screen: &mut Screen, editor: &Editor) -> Result<(), ()> {
Coords::from(x, status_height), Coords::from(x, status_height),
); );
// Calculate where to write the file name
let x = x + mode_string.len(); let x = x + mode_string.len();
// Write the current file name // 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( screen.write_at(
file_name.magenta().bold().reverse().to_string(), file_name.magenta().bold().reverse().to_string(),
Coords::from(x, status_height), Coords::from(x, status_height),
); );
} else {
screen.write_at(
buffer_kind.blue().bold().reverse().to_string(),
Coords::from(x, status_height),
);
}
// Draw the rest of the status bar let x = if editor.buffer.name.len() > 0 { x + file_name.len() } else { x + buffer_kind.len() };
let x = x + file_name.len();
screen.write_at( screen.write_at(
" ".repeat(screen.size.width - x).reverse().to_string(), " ".repeat(screen.size.width - x).reverse().to_string(),
Coords::from(x, status_height), Coords::from(x, status_height),

View File

@ -1,4 +1,5 @@
use crate::core::editor::Editor; use crate::core::editor::Editor;
use crate::core::buffer::BufferKind;
use crate::terminal::screen::Screen; use crate::terminal::screen::Screen;
use crate::tui::components; use crate::tui::components;
use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
@ -10,7 +11,9 @@ pub fn start(screen: &mut Screen, editor: Editor) {
screen.refresh().unwrap(); screen.refresh().unwrap();
// Draw the welcome message // Draw the welcome message
if editor.buffer.kind == BufferKind::Scratch {
components::welcome::draw(screen, &editor); components::welcome::draw(screen, &editor);
};
// Draw the status bar // Draw the status bar
components::statusbar::draw(screen, &editor).unwrap(); components::statusbar::draw(screen, &editor).unwrap();