Avoid unnecessary clone

This commit is contained in:
sxyazi 2023-11-01 16:28:14 +08:00
parent f214d36e5a
commit 9a67c0150e
No known key found for this signature in database
3 changed files with 17 additions and 15 deletions

View file

@ -2,20 +2,23 @@ use crate::{completion::CompletionOpt, Position};
#[derive(Default)] #[derive(Default)]
pub struct Completion { pub struct Completion {
items: Vec<String>, pub items: Vec<String>,
cursor: usize, pub cursor: usize,
pub identifier: String,
pub identifier: String,
pub visible: bool,
// TODO: remove these
pub position: Position, pub position: Position,
pub column_cnt: u8, pub column_cnt: u8,
pub max_width: u16, pub max_width: u16,
pub visible: bool,
} }
impl Completion { impl Completion {
pub fn show(&mut self, opt: CompletionOpt) { pub fn show(&mut self, opt: CompletionOpt) {
self.close(); self.close();
self.visible = true; self.items = opt.items;
self.identifier = format!( self.identifier = format!(
"{}", "{}",
std::time::SystemTime::now() std::time::SystemTime::now()
@ -23,8 +26,9 @@ impl Completion {
.unwrap_or_default() .unwrap_or_default()
.as_millis() .as_millis()
); );
self.visible = true;
self.items = opt.items; // TODO: remove these
self.position = opt.position; self.position = opt.position;
self.column_cnt = opt.column_cnt; self.column_cnt = opt.column_cnt;
self.max_width = opt.max_width; self.max_width = opt.max_width;
@ -32,6 +36,7 @@ impl Completion {
pub fn close(&mut self) -> bool { pub fn close(&mut self) -> bool {
self.cursor = 0; self.cursor = 0;
self.identifier = String::new(); self.identifier = String::new();
self.visible = false; self.visible = false;
true true
@ -56,9 +61,6 @@ impl Completion {
old != self.cursor old != self.cursor
} }
pub fn list(&self) -> Vec<String> { self.items.clone() } #[inline]
pub fn selected(&self) -> Option<&String> { self.items.get(self.cursor) }
pub fn cursor(&self) -> usize { self.cursor }
pub fn get_selection(&self) -> Option<String> { self.items.get(self.cursor).cloned() }
} }

View file

@ -52,7 +52,7 @@ impl Input {
} }
pub fn finish_completion(&mut self) -> bool { pub fn finish_completion(&mut self) -> bool {
if let (Some(val), Some(f)) = (self.completion.get_selection(), &self.finish_completion) { if let (Some(val), Some(f)) = (self.completion.selected(), &self.finish_completion) {
let final_val = f(self.snaps.current().value.as_str(), val.as_str()); let final_val = f(self.snaps.current().value.as_str(), val.as_str());
self.replace_str(final_val.as_str()); self.replace_str(final_val.as_str());
} }

View file

@ -24,18 +24,18 @@ impl<'a> Widget for Completion<'a> {
let max_width = completion.max_width as usize; let max_width = completion.max_width as usize;
let mut table = vec![]; let mut table = vec![];
let mut cur_row = vec![]; let mut cur_row = vec![];
for (idx, s) in completion.list().into_iter().enumerate() { for (idx, s) in completion.items.iter().enumerate() {
if idx != 0 && idx % completion.column_cnt as usize == 0 { if idx != 0 && idx % completion.column_cnt as usize == 0 {
let t = mem::take(&mut cur_row); let t = mem::take(&mut cur_row);
table.push(Row::new(t)); table.push(Row::new(t));
} }
cur_row.push( cur_row.push(
Cell::from(if s.len() < max_width { Cell::from(if s.len() < max_width {
s s.to_owned()
} else { } else {
s.split_at(max_width - 1).0.to_string() + "" s.split_at(max_width - 1).0.to_string() + ""
}) })
.style(if completion.cursor() == idx { .style(if completion.cursor == idx {
THEME.completion.active.into() THEME.completion.active.into()
} else { } else {
THEME.completion.inactive.into() THEME.completion.inactive.into()