Skip to content

Commit dce97cc

Browse files
committed
fix: Fix comment regex filtering in Names dialog
1 parent ac74eb7 commit dce97cc

1 file changed

Lines changed: 54 additions & 21 deletions

File tree

src/hex/names.rs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,44 @@ use ratatui::{
88

99
use ratatui::crossterm::event::{Event, KeyCode};
1010
use std::io::Result;
11+
use tui_input::backend::crossterm::EventHandler;
1112

12-
use crate::{app::App, commands::Commands, editor::UIState, util::center_widget};
13+
use crate::{app::App, editor::UIState, util::center_widget};
1314

1415
pub fn dialog_names_draw(app: &mut App, frame: &mut Frame) {
15-
let n = app.hex_view.comment_name_list.len();
16-
let mut items = Vec::with_capacity(n);
16+
let re = if !app.hex_view.names_regex.is_empty() {
17+
regex::RegexBuilder::new(&app.hex_view.names_regex)
18+
.case_insensitive(true)
19+
.build()
20+
.ok()
21+
} else {
22+
None
23+
};
24+
25+
let mut items = Vec::new();
26+
let mut count = 0;
1727

1828
for cmt in &app.hex_view.comment_name_list {
19-
items.push(ListItem::from(format!(
20-
"{:08X} {}",
21-
cmt.offset, cmt.comment
22-
)));
23-
}
29+
let is_match = if let Some(ref r) = re {
30+
r.is_match(&cmt.comment)
31+
} else {
32+
true
33+
};
2434

25-
let names_count = app.hex_view.comment_name_list.len();
35+
if is_match {
36+
items.push(ListItem::from(format!(
37+
"{:08X} {}",
38+
cmt.offset, cmt.comment
39+
)));
40+
count += 1;
41+
}
42+
}
2643

2744
let list = List::new(items)
2845
.style(app.config.theme.dialog)
2946
.block(
3047
Block::bordered()
31-
.title(format!(" Names ({}) ", names_count))
48+
.title(format!(" Names ({}) ", count))
3249
.title_alignment(Alignment::Center)
3350
.padding(Padding::horizontal(1)),
3451
)
@@ -82,16 +99,30 @@ pub fn dialog_names_events(app: &mut App, event: &Event) -> Result<bool> {
8299
}
83100
KeyCode::Enter => {
84101
if let Some(choice) = app.hex_view.names_list_state.selected() {
85-
if choice > app.hex_view.comment_name_list.len() {
86-
App::log(
87-
app,
88-
"wtf {choice} is greater than `app.hex_mode.comments.len()`, dunno how"
89-
.to_string(),
90-
);
91-
return Ok(true);
102+
let re = if !app.hex_view.names_regex.is_empty() {
103+
regex::RegexBuilder::new(&app.hex_view.names_regex)
104+
.case_insensitive(true)
105+
.build()
106+
.ok()
107+
} else {
108+
None
109+
};
110+
111+
let mut matched_comments = Vec::new();
112+
for cmt in &app.hex_view.comment_name_list {
113+
let is_match = if let Some(ref r) = re {
114+
r.is_match(&cmt.comment)
115+
} else {
116+
true
117+
};
118+
if is_match {
119+
matched_comments.push(cmt.clone());
120+
}
121+
}
122+
123+
if choice < matched_comments.len() {
124+
app.goto(matched_comments[choice].offset);
92125
}
93-
// Vec<Comment>
94-
app.goto(app.hex_view.comment_name_list[choice].offset);
95126
}
96127
app.state = UIState::Normal;
97128
app.dialog_renderer = None;
@@ -147,9 +178,11 @@ pub fn dialog_names_regex_events(app: &mut App, event: &Event) -> Result<bool> {
147178
app.hex_view.names_regex = String::from(app.hex_view.names_regex_input.value());
148179
app.dialog_2nd_renderer = None;
149180
app.state = UIState::DialogNames;
150-
Commands::load_strings(app, true);
181+
app.hex_view.names_list_state.select(Some(0));
182+
}
183+
_ => {
184+
app.hex_view.names_regex_input.handle_event(event);
151185
}
152-
_ => (),
153186
}
154187
}
155188
Ok(false)

0 commit comments

Comments
 (0)