Skip to content

Commit

Permalink
feat(popup): add popup feature
Browse files Browse the repository at this point in the history
  • Loading branch information
metiftikci committed Oct 20, 2023
1 parent 78e3420 commit 79e747f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 75 deletions.
4 changes: 4 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct Buffer {
pub actions_insert: ActionMap,
pub actions_normal: ActionMap,
pub actions_visual: ActionMap,
pub popups: Vec<Buffer>,
pub active_popup: Option<usize>,
}

impl Buffer {
Expand All @@ -57,6 +59,8 @@ impl Buffer {
actions_insert: get_default_insert_maps(),
actions_normal: get_default_normal_maps(),
actions_visual: get_default_visual_maps(),
popups: vec![],
active_popup: None,
};

buffer.set_size(area);
Expand Down
114 changes: 73 additions & 41 deletions src/buffer/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,118 +10,136 @@ use crate::{
pub fn get_default_insert_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();
map.insert("esc", |editor| {
editor.get_active_buffer_mut().enter_normal_mode()
editor.get_active_buffer_or_popup_mut().enter_normal_mode()
});
map.insert("enter", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.split_line(buffer.cursor.y, buffer.cursor.x)
});
map.insert("backspace", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.delete_char_before(buffer.cursor.y, buffer.cursor.x)
});
map.insert("delete", |editor| {
editor.get_active_buffer_mut().delete_char()
editor.get_active_buffer_or_popup_mut().delete_char()
});
map.insert("<c-j>", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.split_line(buffer.cursor.y, buffer.cursor.x);
});
return map;
}

pub fn get_default_normal_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();
map.insert("left", |editor| editor.get_active_buffer_mut().move_left());
map.insert("down", |editor| editor.get_active_buffer_mut().move_down());
map.insert("up", |editor| editor.get_active_buffer_mut().move_up());
map.insert("left", |editor| {
editor.get_active_buffer_or_popup_mut().move_left()
});
map.insert("down", |editor| {
editor.get_active_buffer_or_popup_mut().move_down()
});
map.insert("up", |editor| {
editor.get_active_buffer_or_popup_mut().move_up()
});
map.insert("right", |editor| {
editor.get_active_buffer_mut().move_right()
editor.get_active_buffer_or_popup_mut().move_right()
});

map.insert("h", |editor| editor.get_active_buffer_mut().move_left());
map.insert("j", |editor| editor.get_active_buffer_mut().move_down());
map.insert("k", |editor| editor.get_active_buffer_mut().move_up());
map.insert("l", |editor| editor.get_active_buffer_mut().move_right());
map.insert("h", |editor| {
editor.get_active_buffer_or_popup_mut().move_left()
});
map.insert("j", |editor| {
editor.get_active_buffer_or_popup_mut().move_down()
});
map.insert("k", |editor| {
editor.get_active_buffer_or_popup_mut().move_up()
});
map.insert("l", |editor| {
editor.get_active_buffer_or_popup_mut().move_right()
});

map.insert("g", |editor| {
editor.get_active_buffer_mut().move_first_row()
editor.get_active_buffer_or_popup_mut().move_first_row()
});
map.insert("G", |editor| {
editor.get_active_buffer_or_popup_mut().move_last_row()
});
map.insert("G", |editor| editor.get_active_buffer_mut().move_last_row());
map.insert("0", |editor| {
editor.get_active_buffer_mut().move_first_column()
editor.get_active_buffer_or_popup_mut().move_first_column()
});
map.insert("$", |editor| {
editor.get_active_buffer_mut().move_last_column()
editor.get_active_buffer_or_popup_mut().move_last_column()
});

map.insert("w", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
let new_position = get_next_word_start_position(buffer);
buffer.move_cursor(new_position.y, new_position.x);
});
map.insert("e", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
let new_position = get_next_word_end_position(buffer);
buffer.move_cursor(new_position.y, new_position.x);
});
map.insert("b", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
let new_position = get_previous_word_start_position(buffer);
buffer.move_cursor(new_position.y, new_position.x);
});

map.insert("J", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
if buffer.cursor.y < buffer.get_row_count() - 1 {
buffer.join_lines(buffer.cursor.y, buffer.cursor.y + 1);
}
});

map.insert("x", |editor| editor.get_active_buffer_mut().delete_char());
map.insert("x", |editor| {
editor.get_active_buffer_or_popup_mut().delete_char()
});

map.insert("I", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.move_first_column();
buffer.enter_insert_mode();
});
map.insert("i", |editor| {
editor.get_active_buffer_mut().enter_insert_mode()
editor.get_active_buffer_or_popup_mut().enter_insert_mode()
});
map.insert("A", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.move_last_column();
buffer.enter_insert_mode();
buffer.move_right();
});
map.insert("a", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.enter_insert_mode();
buffer.move_right();
});
map.insert("O", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.insert_newline(buffer.cursor.y);
buffer.enter_insert_mode();
});
map.insert("o", |editor| {
let buffer = editor.get_active_buffer_mut();
let buffer = editor.get_active_buffer_or_popup_mut();
buffer.insert_newline(buffer.cursor.y + 1);
buffer.enter_insert_mode();
});
map.insert(":", |editor| {
editor.get_active_buffer_mut().enter_command_mode()
editor.get_active_buffer_or_popup_mut().enter_command_mode()
});
map.insert("v", |editor| {
editor.get_active_buffer_mut().enter_visual_mode()
editor.get_active_buffer_or_popup_mut().enter_visual_mode()
});
return map;
}

pub fn get_default_command_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();
map.insert("esc", |editor| {
editor.get_active_buffer_mut().enter_normal_mode()
editor.get_active_buffer_or_popup_mut().enter_normal_mode()
});
map.insert("enter", |editor| editor.run_command());
map.insert("backspace", |editor| editor.command.delete_char());
Expand All @@ -133,23 +151,37 @@ pub fn get_default_command_maps() -> ActionMap {
pub fn get_default_visual_maps() -> ActionMap {
let mut map: ActionMap = HashMap::new();
map.insert("esc", |editor| {
editor.get_active_buffer_mut().enter_normal_mode()
editor.get_active_buffer_or_popup_mut().enter_normal_mode()
});

map.insert("left", |editor| editor.get_active_buffer_mut().move_left());
map.insert("down", |editor| editor.get_active_buffer_mut().move_down());
map.insert("up", |editor| editor.get_active_buffer_mut().move_up());
map.insert("left", |editor| {
editor.get_active_buffer_or_popup_mut().move_left()
});
map.insert("down", |editor| {
editor.get_active_buffer_or_popup_mut().move_down()
});
map.insert("up", |editor| {
editor.get_active_buffer_or_popup_mut().move_up()
});
map.insert("right", |editor| {
editor.get_active_buffer_mut().move_right()
editor.get_active_buffer_or_popup_mut().move_right()
});

map.insert("h", |editor| editor.get_active_buffer_mut().move_left());
map.insert("j", |editor| editor.get_active_buffer_mut().move_down());
map.insert("k", |editor| editor.get_active_buffer_mut().move_up());
map.insert("l", |editor| editor.get_active_buffer_mut().move_right());
map.insert("h", |editor| {
editor.get_active_buffer_or_popup_mut().move_left()
});
map.insert("j", |editor| {
editor.get_active_buffer_or_popup_mut().move_down()
});
map.insert("k", |editor| {
editor.get_active_buffer_or_popup_mut().move_up()
});
map.insert("l", |editor| {
editor.get_active_buffer_or_popup_mut().move_right()
});

map.insert("o", |editor| {
editor.get_active_buffer_mut().reverse_selection()
editor.get_active_buffer_or_popup_mut().reverse_selection()
});

return map;
Expand Down
28 changes: 25 additions & 3 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ impl Editor {
self.get_active_tab_mut().get_active_buffer_mut()
}

pub fn get_active_buffer_or_popup(&self) -> &Buffer {
let buffer = self.get_active_tab().get_active_buffer();

match buffer.active_popup {
Some(i) => buffer.popups.get(i).unwrap(),
None => buffer,
}
}

pub fn get_active_buffer_or_popup_mut(&mut self) -> &mut Buffer {
let buffer = self.get_active_tab_mut().get_active_buffer_mut();

match buffer.active_popup {
Some(i) => buffer.popups.get_mut(i).unwrap(),
None => buffer,
}
}

pub fn set_size(&mut self, area: Rectangle<u16>) {
self.area = area.clone();
self.tabs_area = area.clone();
Expand All @@ -78,7 +96,11 @@ impl Editor {
}

pub fn handle_key(&mut self, key: Key) {
let buffer = self.get_active_buffer_mut();
let main_buffer = self.get_active_buffer_mut();
let buffer = match main_buffer.active_popup {
Some(active_popup_index) => main_buffer.popups.get_mut(active_popup_index).unwrap(),
None => main_buffer,
};

match buffer.mode {
BufferMode::Normal => match buffer.actions_normal.get(&key.to_string().as_str()) {
Expand All @@ -94,7 +116,7 @@ impl Editor {
None => {
if !key.ctrl && !key.win && !key.alt && key.code.len() == 1 {
let ch = key.code.chars().nth(0).unwrap();
self.get_active_buffer_mut().insert_char(ch);
buffer.insert_char(ch);
}
}
},
Expand All @@ -103,7 +125,7 @@ impl Editor {
None => {
if !key.ctrl && !key.win && !key.alt && key.code.len() == 1 {
let ch = key.code.chars().nth(0).unwrap();
self.command.insert_char(ch);
buffer.insert_char(ch);
}
}
},
Expand Down
77 changes: 46 additions & 31 deletions src/screen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
buffer::mode::BufferMode,
buffer::{mode::BufferMode, Buffer},
core::{Point, Size},
editor::Editor,
terminal::CursorStyle,
Expand Down Expand Up @@ -67,17 +67,18 @@ impl Screen {
let width = screen.size.width;
let height = screen.size.height;

let tab = editor.get_active_tab();
let buffer = tab.get_active_buffer();
screen.cursor = editor.get_active_buffer_or_popup().get_cursor_screen_pos();

screen.cursor = buffer.get_cursor_screen_pos();

match buffer.mode {
match editor.get_active_buffer_or_popup().mode {
BufferMode::Normal => screen.cursor_style = CursorStyle::BlinkingBlock,
BufferMode::Visual => screen.cursor_style = CursorStyle::BlinkingBlock,
BufferMode::Insert => screen.cursor_style = CursorStyle::BlinkingBar,
BufferMode::Command => screen.cursor_style = CursorStyle::BlinkingBar,
}

let tab = editor.get_active_tab();
let buffer = tab.get_active_buffer();

screen.paint_range(
Point { y: 0, x: 0 },
Point { y: 0, x: width - 1 },
Expand All @@ -102,31 +103,10 @@ impl Screen {
None => screen.print(0, 0, "[No Name]", T.tab_selected_fg, T.tab_selected_bg),
}

for y in 0..buffer.area.height {
let row_index = buffer.scroll.y + y as usize;
match buffer.get_line_visible_text(row_index) {
Some(text) => {
screen.print(
y + 1,
buffer.area.x,
&format!(
" {:>1$} ",
row_index + 1,
buffer.info_area.width as usize - 2
),
T.info_column_fg,
T.info_column_bg,
);
screen.print(
y + 1,
buffer.area.x + buffer.info_area.width,
&text,
T.text_fg,
T.text_bg,
);
}
None => screen.print(y + 1, 0, "~", T.info_column_fg, T.bg),
}
screen.print_buffer(&buffer);

for popup in buffer.popups.iter() {
screen.print_buffer(popup);
}

match buffer.mode {
Expand Down Expand Up @@ -196,6 +176,41 @@ impl Screen {

None
}

pub fn print_buffer(&mut self, buffer: &Buffer) {
for y in 0..buffer.area.height {
let row_index = buffer.scroll.y + y as usize;
match buffer.get_line_visible_text(row_index) {
Some(text) => {
self.print(
buffer.area.y + y,
buffer.area.x,
&format!(
" {:>1$} ",
row_index + 1,
buffer.info_area.width as usize - 2
),
T.info_column_fg,
T.info_column_bg,
);
self.print(
buffer.area.y + y,
buffer.area.x + buffer.info_area.width,
&text,
T.text_fg,
T.text_bg,
);
}
None => self.print(
buffer.area.y + y,
buffer.area.x,
"~",
T.info_column_fg,
T.bg,
),
}
}
}
}

impl Screen {
Expand Down

0 comments on commit 79e747f

Please sign in to comment.