1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-01 07:46:05 +02:00

Support multiple open views.

This commit is contained in:
Blaž Hrastnik 2020-10-19 16:20:59 +09:00
parent 13cb442850
commit b2b3083a62
2 changed files with 20 additions and 11 deletions

View File

@ -354,7 +354,7 @@ pub fn new(mut args: Args) -> Result<Self, Error> {
fn render(&mut self) {
let viewport = Rect::new(OFFSET, 0, self.terminal.size.0, self.terminal.size.1 - 2); // - 2 for statusline and prompt
if let Some(view) = &mut self.editor.view {
if let Some(view) = self.editor.view_mut() {
self.terminal.render_view(view, viewport);
if let Some(prompt) = &self.prompt {
if prompt.should_close {
@ -368,11 +368,8 @@ fn render(&mut self) {
self.terminal.draw();
// TODO: drop unwrap
self.terminal.render_cursor(
self.editor.view.as_ref().unwrap(),
self.prompt.as_ref(),
viewport,
);
self.terminal
.render_cursor(self.editor.view().unwrap(), self.prompt.as_ref(), viewport);
}
pub async fn event_loop(&mut self) {
@ -392,7 +389,7 @@ pub async fn event_loop(&mut self) {
self.terminal.resize(width, height);
// TODO: simplistic ensure cursor in view for now
if let Some(view) = &mut self.editor.view {
if let Some(view) = self.editor.view_mut() {
view.size = self.terminal.size;
view.ensure_cursor_in_view()
};
@ -408,7 +405,7 @@ pub async fn event_loop(&mut self) {
.handle_input(event, &mut self.editor);
self.render();
} else if let Some(view) = &mut self.editor.view {
} else if let Some(view) = self.editor.view_mut() {
let keys = vec![event];
// TODO: sequences (`gg`)
// TODO: handle count other than 1

View File

@ -5,20 +5,32 @@
use anyhow::Error;
pub struct Editor {
pub view: Option<View>,
pub views: Vec<View>,
pub focus: usize,
pub should_close: bool,
}
impl Editor {
pub fn new() -> Self {
Self {
view: None,
views: Vec::new(),
focus: 0,
should_close: false,
}
}
pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
self.view = Some(View::open(path, size)?);
let pos = self.views.len();
self.views.push(View::open(path, size)?);
self.focus = pos;
Ok(())
}
pub fn view(&self) -> Option<&View> {
self.views.get(self.focus)
}
pub fn view_mut(&mut self) -> Option<&mut View> {
self.views.get_mut(self.focus)
}
}