on master help sign in
Support multiple open views.
commitb2b3083a623ec3dc5e2d1ea9c6ba35970efe19a3 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 19 Oct 2020, 16:20 (6 years ago) parent13cb442
2 files changed+20−11
helix-term/src/application.rs+5−8 helix-view/src/editor.rs+15−3
helix-term/src/application.rs+5−8
@@ -354,7 +354,7 @@ impl Application {
354354 fn render(&mut self) {
355355 let viewport = Rect::new(OFFSET, 0, self.terminal.size.0, self.terminal.size.1 - 2); // - 2 for statusline and prompt
356356
357− if let Some(view) = &mut self.editor.view {
357+ if let Some(view) = self.editor.view_mut() {
358358 self.terminal.render_view(view, viewport);
359359 if let Some(prompt) = &self.prompt {
360360 if prompt.should_close {
@@ -368,11 +368,8 @@ impl Application {
368368 self.terminal.draw();
369369
370370 // TODO: drop unwrap
371− self.terminal.render_cursor(
372− self.editor.view.as_ref().unwrap(),
373− self.prompt.as_ref(),
374− viewport,
375− );
371+ self.terminal
372+ .render_cursor(self.editor.view().unwrap(), self.prompt.as_ref(), viewport);
376373 }
377374
378375 pub async fn event_loop(&mut self) {
@@ -392,7 +389,7 @@ impl Application {
392389 self.terminal.resize(width, height);
393390
394391 // TODO: simplistic ensure cursor in view for now
395− if let Some(view) = &mut self.editor.view {
392+ if let Some(view) = self.editor.view_mut() {
396393 view.size = self.terminal.size;
397394 view.ensure_cursor_in_view()
398395 };
@@ -408,7 +405,7 @@ impl Application {
408405 .handle_input(event, &mut self.editor);
409406
410407 self.render();
411− } else if let Some(view) = &mut self.editor.view {
408+ } else if let Some(view) = self.editor.view_mut() {
412409 let keys = vec![event];
413410 // TODO: sequences (`gg`)
414411 // TODO: handle count other than 1
helix-view/src/editor.rs+15−3
@@ -5,20 +5,32 @@ use std::path::PathBuf;
55use anyhow::Error;
66
77pub struct Editor {
8− pub view: Option<View>,
8+ pub views: Vec<View>,
9+ pub focus: usize,
910 pub should_close: bool,
1011}
1112
1213impl Editor {
1314 pub fn new() -> Self {
1415 Self {
15− view: None,
16+ views: Vec::new(),
17+ focus: 0,
1618 should_close: false,
1719 }
1820 }
1921
2022 pub fn open(&mut self, path: PathBuf, size: (u16, u16)) -> Result<(), Error> {
21− self.view = Some(View::open(path, size)?);
23+ let pos = self.views.len();
24+ self.views.push(View::open(path, size)?);
25+ self.focus = pos;
2226 Ok(())
2327 }
28+
29+ pub fn view(&self) -> Option<&View> {
30+ self.views.get(self.focus)
31+ }
32+
33+ pub fn view_mut(&mut self) -> Option<&mut View> {
34+ self.views.get_mut(self.focus)
35+ }
2436}