Better support floating buffer
This commit is contained in:
1 parent
8955eeae28
commit
2e1226f878
5 files changed
+70
-13
No files matched your search
+22
-2
@@ -20,6 +20,7 @@ use crate::{
|
||||
core::{
|
||||
actions::{CommandAction, EditorAction},
|
||||
error::BasicErrorKind,
|
||||
pane_layout::PaneDescriptor,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -49,9 +50,12 @@ pub(crate) enum Mode {
|
||||
|
||||
pub(crate) struct EditorCore {
|
||||
pub mirrored: bool,
|
||||
|
||||
// buffer-mgmt-related stuff - TODO move this into a substructure
|
||||
buffers: HashMap<BufferId, TextBuffer>,
|
||||
pane_layout: PaneLayout,
|
||||
active_buffer: BufferId,
|
||||
|
||||
mode: Mode,
|
||||
configuration: Configuration,
|
||||
commands: Commands,
|
||||
@@ -85,10 +89,19 @@ impl EditorCore {
|
||||
pub(crate) fn mode(&self) -> Mode {
|
||||
self.mode
|
||||
}
|
||||
|
||||
pub(crate) fn pane_layout(&self) -> &PaneLayout {
|
||||
&self.pane_layout
|
||||
}
|
||||
|
||||
pub(crate) fn current_command(&self) -> Option<&str> {
|
||||
self.commands.current_command.as_ref().map(|x| x.as_str())
|
||||
}
|
||||
|
||||
pub(crate) fn get_buffer_by_id(&self, id: BufferId) -> Option<&TextBuffer> {
|
||||
self.buffers.get(&id)
|
||||
}
|
||||
|
||||
pub(crate) fn active_buffer(&self) -> &TextBuffer {
|
||||
let active = self.active_buffer;
|
||||
self.buffers.get(&active).unwrap()
|
||||
@@ -147,9 +160,9 @@ impl EditorCore {
|
||||
error!(err, "Command Output Error");
|
||||
}
|
||||
CommandOutput::WriteBuffer(path) => log_only(self.write_current_buffer(path)),
|
||||
CommandOutput::ShowHelp => {
|
||||
CommandOutput::ShowHelp(toggle) => {
|
||||
info!("showing help");
|
||||
self.handle_action(EditorAction::PopupHelp(true));
|
||||
self.handle_action(EditorAction::PopupHelp(toggle));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -200,12 +213,19 @@ impl EditorCore {
|
||||
buf.cursor_pos.move_cursor(movement, &buf.text);
|
||||
}
|
||||
PopupHelp(toggle) => {
|
||||
info!(%toggle, "invoke help");
|
||||
let keys = self.buffers.keys();
|
||||
info!(?keys, "buffers");
|
||||
|
||||
//TODO change this hardcoding of the buffer id
|
||||
let buffer_id = BufferId(1);
|
||||
if toggle {
|
||||
self.buffers.insert(buffer_id, TextBuffer::help());
|
||||
let descriptor = PaneDescriptor::Floating;
|
||||
self.pane_layout.add(buffer_id, descriptor);
|
||||
} else {
|
||||
self.buffers.remove(&buffer_id);
|
||||
self.pane_layout.remove(buffer_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ pub(crate) enum CommandOutput {
|
||||
WriteBuffer(Option<PathBuf>),
|
||||
Quit,
|
||||
Error(String),
|
||||
ShowHelp,
|
||||
ShowHelp(bool),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@@ -34,7 +34,16 @@ impl CommandAPI {
|
||||
Some(CommandOutput::OpenFile(path))
|
||||
}
|
||||
Command::Quit => Some(CommandOutput::Quit),
|
||||
Command::Help => Some(CommandOutput::ShowHelp),
|
||||
Command::Help => {
|
||||
let toggle = commands.next();
|
||||
let toggle = match toggle {
|
||||
Some("on") => true,
|
||||
None => true,
|
||||
Some("off") => false,
|
||||
_ => return None,
|
||||
};
|
||||
Some(CommandOutput::ShowHelp(toggle))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-1
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use super::buffer::BufferId;
|
||||
|
||||
pub(super) struct PaneLayout {
|
||||
pub(crate) struct PaneLayout {
|
||||
map: HashMap<BufferId, PaneDescriptor>,
|
||||
}
|
||||
|
||||
@@ -13,8 +13,26 @@ impl PaneLayout {
|
||||
|
||||
Self { map }
|
||||
}
|
||||
|
||||
pub(super) fn add(&mut self, id: BufferId, descriptor: PaneDescriptor) {
|
||||
self.map.insert(id, descriptor);
|
||||
}
|
||||
|
||||
pub(super) fn remove(&mut self, id: BufferId) {
|
||||
self.map.remove(&id);
|
||||
}
|
||||
|
||||
pub(crate) fn has_popup(&self) -> Option<BufferId> {
|
||||
// TODO this needs to be ordered
|
||||
self.map
|
||||
.iter()
|
||||
.find(|(_, item)| matches!(item, PaneDescriptor::Floating))
|
||||
.map(|(id, _)| id)
|
||||
.copied()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum PaneDescriptor {
|
||||
Main { tab: u16 },
|
||||
Floating,
|
||||
|
||||
+18
-8
@@ -90,6 +90,22 @@ impl TerminalState {
|
||||
let active_buffer = core.active_buffer();
|
||||
let terminal_buf_height = rect.height;
|
||||
|
||||
let pane_layout = core.pane_layout();
|
||||
|
||||
let text = single_buffer_lines(
|
||||
active_buffer,
|
||||
terminal_buf_height,
|
||||
cursor_style,
|
||||
line_number_style,
|
||||
);
|
||||
frame.render_widget(Paragraph::new(text).white().on_black(), rect);
|
||||
|
||||
if let Some(floating) = pane_layout.has_popup() {
|
||||
let _floating_buf = core.get_buffer_by_id(floating);
|
||||
self.render_popup(frame, rect, "yolo");
|
||||
}
|
||||
|
||||
/*
|
||||
if core.mirrored {
|
||||
let buffer_layout =
|
||||
Layout::horizontal([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)]).split(rect);
|
||||
@@ -111,15 +127,9 @@ impl TerminalState {
|
||||
);
|
||||
frame.render_widget(Paragraph::new(text).white().on_black(), buffer_layout[1]);
|
||||
} else {
|
||||
let text = single_buffer_lines(
|
||||
active_buffer,
|
||||
terminal_buf_height,
|
||||
cursor_style,
|
||||
line_number_style,
|
||||
);
|
||||
frame.render_widget(Paragraph::new(text).white().on_black(), rect);
|
||||
self.render_popup(frame, rect, "yolo");
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
fn single_buffer_lines<'a>(
|
||||
active_buffer: &TextBuffer,
|
||||
|
||||
Reference in new issue
Block a user