diff --git a/benches/main/ui.rs b/benches/main/ui.rs index 229f582..be1f46d 100644 --- a/benches/main/ui.rs +++ b/benches/main/ui.rs @@ -498,7 +498,8 @@ pub fn draw(c: &mut Criterion) { std::thread::sleep(std::time::Duration::from_millis(10)); } tv.move_cursor(Movement::Next, 10); - let _ = tv.update_preview_state(&tv.get_selected_entry()); + let selected_entry = tv.get_selected_entry(); + let _ = tv.update_preview_state(&selected_entry); let _ = tv.update(&Action::Tick); (tv, terminal) }, diff --git a/television/channels/channel.rs b/television/channels/channel.rs index 45ce513..81ea97d 100644 --- a/television/channels/channel.rs +++ b/television/channels/channel.rs @@ -107,7 +107,7 @@ impl Channel { entries } - pub fn get_result(&self, index: u32) -> Option { + pub fn get_result(&mut self, index: u32) -> Option { if let Some(item) = self.matcher.get_result(index) { let mut entry = Entry::new(item.inner.clone()) .with_display(item.matched_string) @@ -177,6 +177,8 @@ impl Channel { } } +const DEFAULT_LINE_BUFFER_SIZE: usize = 512; + #[allow(clippy::unused_async)] async fn load_candidates( source: SourceSpec, @@ -197,7 +199,7 @@ async fn load_candidates( if let Some(out) = child.stdout.take() { let mut produced_output = false; let mut reader = BufReader::new(out); - let mut buf = Vec::new(); + let mut buf = Vec::with_capacity(DEFAULT_LINE_BUFFER_SIZE); let delimiter = source .entry_delimiter diff --git a/television/channels/remote_control.rs b/television/channels/remote_control.rs index f927b16..8b6bdbf 100644 --- a/television/channels/remote_control.rs +++ b/television/channels/remote_control.rs @@ -155,7 +155,7 @@ impl RemoteControl { .collect() } - pub fn get_result(&self, index: u32) -> CableEntry { + pub fn get_result(&mut self, index: u32) -> CableEntry { let item = self.matcher.get_result(index).expect("Invalid index"); item.inner.with_match_indices(&item.match_indices) } diff --git a/television/matcher/mod.rs b/television/matcher/mod.rs index b27d2f9..ae75a73 100644 --- a/television/matcher/mod.rs +++ b/television/matcher/mod.rs @@ -226,23 +226,23 @@ where /// } /// ``` pub fn get_result( - &self, + &mut self, index: u32, ) -> Option> { let snapshot = self.inner.snapshot(); - let mut col_indices = Vec::new(); let mut matcher = lazy::MATCHER.lock(); + self.col_indices_buffer.clear(); snapshot.get_matched_item(index).map(|item| { snapshot.pattern().column_pattern(0).indices( item.matcher_columns[0].slice(..), &mut matcher, - &mut col_indices, + &mut self.col_indices_buffer, ); - col_indices.sort_unstable(); - col_indices.dedup(); + self.col_indices_buffer.sort_unstable(); + self.col_indices_buffer.dedup(); - let indices = col_indices.drain(..); + let indices = self.col_indices_buffer.drain(..); let matched_string = item.matcher_columns[0].to_string(); matched_item::MatchedItem { diff --git a/television/television.rs b/television/television.rs index 0cb3821..4809324 100644 --- a/television/television.rs +++ b/television/television.rs @@ -389,7 +389,7 @@ impl Television { } } - pub fn get_selected_entry(&self) -> Option { + pub fn get_selected_entry(&mut self) -> Option { if self.channel.result_count() == 0 { return None; } @@ -398,7 +398,7 @@ impl Television { .and_then(|entry| entry) } - pub fn get_selected_cable_entry(&self) -> Option { + pub fn get_selected_cable_entry(&mut self) -> Option { if self .remote_control .as_ref() @@ -408,7 +408,7 @@ impl Television { return None; } self.selected_index().and_then(|idx| { - self.remote_control.as_ref().map(|rc| rc.get_result(idx)) + self.remote_control.as_mut().map(|rc| rc.get_result(idx)) }) } @@ -423,7 +423,7 @@ impl Television { } #[must_use] - pub fn get_selected_entries(&self) -> Option> { + pub fn get_selected_entries(&mut self) -> Option> { // if nothing is selected, return the currently hovered entry if self.channel.selected_entries().is_empty() { return self