Skip to content

Commit ec916b8

Browse files
committed
Refactor item access in TableView to use current_selected_item method
1 parent 2f649e9 commit ec916b8

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

src/view/table.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,7 @@ impl TableView {
472472

473473
impl TableView {
474474
fn open_item(&self) {
475-
if let Some(item) = self
476-
.items
477-
.get(self.view_indices[self.table_state.selected_row])
478-
{
475+
if let Some(item) = self.current_selected_item() {
479476
let desc = self.table_description.clone();
480477
let item = item.clone();
481478
self.tx.send(AppEvent::OpenItem(desc, item));
@@ -489,14 +486,15 @@ impl TableView {
489486

490487
fn open_expand_selected_attr(&mut self) {
491488
if let Some(col) = self.table_state.selected_col {
492-
let selected_item = &self.items[self.view_indices[self.table_state.selected_row]];
493-
let schema = &self.table_description.key_schema_type;
494-
let key = &list_attribute_keys(&self.items, schema)[col];
495-
if let Some(attr) = selected_item.attributes.get(key) {
496-
let lines = get_raw_json_attribute_lines(attr, &self.theme);
497-
let options = self.attr_scroll_lines_state.current_options();
498-
self.attr_scroll_lines_state = ScrollLinesState::new(lines, options);
499-
self.attr_expanded = true;
489+
if let Some(selected_item) = self.current_selected_item() {
490+
let schema = &self.table_description.key_schema_type;
491+
let key = &list_attribute_keys(&self.items, schema)[col];
492+
if let Some(attr) = selected_item.attributes.get(key) {
493+
let lines = get_raw_json_attribute_lines(attr, &self.theme);
494+
let options = self.attr_scroll_lines_state.current_options();
495+
self.attr_scroll_lines_state = ScrollLinesState::new(lines, options);
496+
self.attr_expanded = true;
497+
}
500498
}
501499
}
502500
}
@@ -609,28 +607,35 @@ impl TableView {
609607
.with_new_total_rows(self.view_indices.len());
610608
}
611609

610+
fn current_selected_item(&self) -> Option<&Item> {
611+
self.view_indices
612+
.get(self.table_state.selected_row)
613+
.and_then(|&idx| self.items.get(idx))
614+
}
615+
612616
fn copy_to_clipboard(&self) {
613-
let selected_item = &self.items[self.view_indices[self.table_state.selected_row]];
614-
let schema = &self.table_description.key_schema_type;
615-
616-
let (name, content) = if let Some(col) = self.table_state.selected_col {
617-
let key = &list_attribute_keys(&self.items, schema)[col];
618-
if let Some(attr) = selected_item.attributes.get(key) {
619-
if self.attr_expanded {
620-
("selected attribute", get_raw_json_attribute_string(attr))
617+
if let Some(selected_item) = self.current_selected_item() {
618+
let schema = &self.table_description.key_schema_type;
619+
620+
let (name, content) = if let Some(col) = self.table_state.selected_col {
621+
let key = &list_attribute_keys(&self.items, schema)[col];
622+
if let Some(attr) = selected_item.attributes.get(key) {
623+
if self.attr_expanded {
624+
("selected attribute", get_raw_json_attribute_string(attr))
625+
} else {
626+
("selected attribute", attr.to_simple_string())
627+
}
621628
} else {
622-
("selected attribute", attr.to_simple_string())
629+
return;
623630
}
624631
} else {
625-
return;
626-
}
627-
} else {
628-
let raw_json_string = get_raw_json_string(selected_item, schema);
629-
("selected item", raw_json_string)
630-
};
632+
let raw_json_string = get_raw_json_string(selected_item, schema);
633+
("selected item", raw_json_string)
634+
};
631635

632-
self.tx
633-
.send(AppEvent::CopyToClipboard(name.into(), content));
636+
self.tx
637+
.send(AppEvent::CopyToClipboard(name.into(), content));
638+
}
634639
}
635640

636641
fn open_help(&self) {

0 commit comments

Comments
 (0)