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

View file

@ -52,7 +52,7 @@ impl Input {
}
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());
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 mut table = 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 {
let t = mem::take(&mut cur_row);
table.push(Row::new(t));
}
cur_row.push(
Cell::from(if s.len() < max_width {
s
s.to_owned()
} else {
s.split_at(max_width - 1).0.to_string() + ""
})
.style(if completion.cursor() == idx {
.style(if completion.cursor == idx {
THEME.completion.active.into()
} else {
THEME.completion.inactive.into()