Skip to content

Commit

Permalink
Merge pull request #9 from todoesverso/fix_panic_deleting_container
Browse files Browse the repository at this point in the history
Fix panic when removing containers
  • Loading branch information
todoesverso committed Mar 24, 2023
2 parents 92d4290 + 39659bc commit b80f900
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 41 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,9 @@ impl<'a> App<'a> {
}
}
}
Err(TryRecvError::Disconnected) => {
Err(TryRecvError::Disconnected) | Err(TryRecvError::Empty) => {
self.stop();
}
_ => {}
}
}

Expand Down Expand Up @@ -256,30 +255,44 @@ impl<'a> App<'a> {

fn render_containers<B: Backend>(&mut self, frame: &mut Frame<'_, B>) {
let blocks = self.get_layout_blocks(frame.size());
// TODO: Review this logic
for (_, container) in self.containers.iter() {
container.render(frame, blocks[container.id as usize - 1]);
let used_ids = self.get_used_id();

for (i, id) in used_ids.into_iter().enumerate() {
let container_key = self.get_container_key_by_id(id).unwrap();
let container = self.containers.get(&container_key.clone()).unwrap();
container.render(frame, blocks[i]);
}
}

fn get_used_id(&self) -> Vec<u8> {
let mut used_ids: Vec<u8> = self.containers.iter().map(|c| c.1.id).collect();
used_ids.sort();
used_ids
}

fn update_containers(&mut self, frame_rect: Rect) {
let blocks = self.get_layout_blocks(frame_rect);
let mut area;

let used_ids = self.get_used_id();
// General containers
for (_, container) in self.containers.iter_mut() {
container.state.wrap = self.state.wrap;
if self.state.show == Views::Zoom {
area = frame_rect.height;
} else {
area = blocks[container.id as usize - 1].height;
for (i, id) in used_ids.into_iter().enumerate() {
if let Some(container_key) = self.get_container_key_by_id(id) {
let mut container = self.containers.get_mut(&container_key.clone()).unwrap();
container.state.wrap = self.state.wrap;
if self.state.show == Views::Zoom {
area = frame_rect.height;
} else {
area = blocks[i].height;
}
container.state.paused = self.state.paused;
container.state.wrap = self.state.wrap;
container.update_scroll(
area as usize,
&mut self.state.scroll_up,
&mut self.state.scroll_down,
);
}
container.state.paused = self.state.paused;
container.state.wrap = self.state.wrap;
container.update_scroll(
area as usize,
&mut self.state.scroll_up,
&mut self.state.scroll_down,
);
}
// Raw buffer
let mut container = &mut self.raw_buffer;
Expand All @@ -292,6 +305,15 @@ impl<'a> App<'a> {
);
}

fn get_container_key_by_id(&self, id: u8) -> Option<&String> {
for (key, container) in self.containers.iter() {
if container.id == id {
return Some(key);
}
}
None
}

fn render_raw<B: Backend>(&mut self, frame: &mut Frame<'_, B>) {
let container = &self.raw_buffer;
container.render(frame, frame.size());
Expand Down Expand Up @@ -319,7 +341,7 @@ impl<'a> App<'a> {

fn render_input<B: Backend>(&self, frame: &mut Frame<'_, B>) {
if self.state.show_input {
self.input.render(frame, frame.size());
self.input.render(frame);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
}
KeyCode::Char('*') => app.flip_raw_view(),
KeyCode::Char('i') => app.flip_show_input(),
KeyCode::Char('i') | KeyCode::Char('/') => app.flip_show_input(),
KeyCode::Char('h') => app.flip_help(),
KeyCode::Char('w') => app.flip_wrap(),
KeyCode::Char('p') | KeyCode::Char(' ') => app.flip_pause(),
Expand Down
2 changes: 1 addition & 1 deletion src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn render_help<B: Backend>(frame: &mut Frame<'_, B>) {
Style::default().bg(Color::Blue),
)),
Spans::from(Span::styled(
"i - input new container (Enter/Esc)",
"i|/ - input new container (Enter/Esc)",
Style::default().bg(Color::Blue),
)),
Spans::from(Span::styled(
Expand Down
8 changes: 4 additions & 4 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use ratatui::backend::Backend;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::terminal::Frame;
use ratatui::text::{Span, Spans};
use unicode_width::UnicodeWidthStr;

use crate::popup::render_popup;
use crate::popup::{centered_rect, render_popup};

#[derive(Debug, Default)]
pub struct Input {
Expand All @@ -18,8 +17,9 @@ impl Input {
Input::default()
}

pub fn render<B: Backend>(&self, frame: &mut Frame<'_, B>, area: Rect) {
pub fn render<B: Backend>(&self, frame: &mut Frame<'_, B>) {
let pos = (40, 8);
let area = centered_rect(pos.0, pos.1, frame.size());
let text = vec![Spans::from(Span::styled(
self.input.clone(),
Style::default(),
Expand Down Expand Up @@ -71,7 +71,7 @@ mod tests {
input.push('b');
let backend = TestBackend::new(20, 38);
let mut terminal = Terminal::new(backend).unwrap();
terminal.draw(|f| input.render(f, f.size())).unwrap();
terminal.draw(|f| input.render(f)).unwrap();
let expected = Buffer::with_lines(vec![
" ",
" ",
Expand Down

0 comments on commit b80f900

Please sign in to comment.