Skip to content

Commit

Permalink
feat(editor): add editor struct to allow multiple window
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Sep 30, 2023
1 parent 2576c61 commit 275403c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
45 changes: 45 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{core::Size, window::Window};

pub struct Editor {
pub size: Size<u16>,
pub windows: Vec<Window>,
pub active_window: usize,
}

impl Editor {
pub fn new(size: Size<u16>) -> Self {
Self {
size,
windows: vec![],
active_window: 0,
}
}

pub fn create_new_window(&mut self) -> &mut Window {
let window = Window::new(Size {
width: self.size.width,
height: self.size.height - 2,
});

self.windows.push(window);

self.windows.last_mut().unwrap()
}

pub fn get_active_window(&self) -> &Window {
self.windows.get(self.active_window).unwrap()
}

pub fn get_active_window_mut(&mut self) -> &mut Window {
self.windows.get_mut(self.active_window).unwrap()
}

pub fn set_size(&mut self, width: u16, height: u16) {
self.size.width = width;
self.size.height = height;

for window in self.windows.iter_mut() {
window.set_size(width, height)
}
}
}
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod buffer;
pub mod commands;
pub mod core;
pub mod editor;
pub mod palette;
pub mod plugins;
pub mod terminal;
Expand All @@ -9,36 +10,37 @@ pub mod window;
use std::io::Result;

use crate::{
editor::Editor,
palette::Palette,
terminal::{Terminal, TerminalEvent},
window::Window,
};

fn main() -> Result<()> {
let terminal_size = Terminal::get_terminal_size()?;

let mut window = Window::new(terminal_size);
let mut editor = Editor::new(terminal_size);
let window = editor.create_new_window();
window.create_new_buffer();

let mut terminal = Terminal::new();

terminal.initialize()?;

let palette = Palette::new(&window);
let palette = Palette::new(&editor);
terminal.redraw(&palette)?;

while let Ok(event) = terminal.read() {
match event {
TerminalEvent::Resize(size) => window.set_size(size.width, size.height),
TerminalEvent::Resize(size) => editor.set_size(size.width, size.height),
TerminalEvent::Key(key) => {
if key.ctrl && key.code == String::from("c") {
break;
}
window.get_active_buffer_mut().handle_key(key);
editor.get_active_window_mut().get_active_buffer_mut().handle_key(key);
}
}

let palette = Palette::new(&window);
let palette = Palette::new(&editor);
terminal.redraw(&palette)?;
}

Expand Down
5 changes: 3 additions & 2 deletions src/palette.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
buffer::mode::BufferMode,
core::{Point, Size},
editor::Editor,
terminal::CursorStyle,
window::Window,
};

pub struct Cell {
Expand Down Expand Up @@ -57,7 +57,8 @@ impl Palette {
palette
}

pub fn new(window: &Window) -> Self {
pub fn new(editor: &Editor) -> Self {
let window = editor.get_active_window();
let mut palette = Palette::from(window.size.height, window.size.width);

palette.cursor.x = window.get_active_buffer_visible_x(window.get_active_buffer().cursor.x);
Expand Down

0 comments on commit 275403c

Please sign in to comment.