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;
#[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<String>,
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,
}
}

View File

@ -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,
}

View File

@ -8,18 +8,14 @@ fn main() {
let args: Vec<String> = 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();

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;
// 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),

View File

@ -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();