Skip to content

Commit

Permalink
perf: new event system (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Jan 22, 2024
1 parent acb8b47 commit 56ede51
Show file tree
Hide file tree
Showing 93 changed files with 353 additions and 328 deletions.
15 changes: 3 additions & 12 deletions yazi-config/src/keymap/control.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::borrow::Cow;
use std::{borrow::Cow, collections::VecDeque};

use serde::Deserialize;
use yazi_shared::event::Exec;
Expand All @@ -14,17 +14,8 @@ pub struct Control {
}

impl Control {
pub fn to_call(&self) -> Vec<Exec> {
self
.exec
.iter()
.map(|e| Exec {
cmd: e.cmd.clone(),
args: e.args.clone(),
named: e.named.clone(),
..Default::default()
})
.collect()
pub fn to_seq(&self) -> VecDeque<Exec> {
self.exec.iter().map(|e| e.clone_without_data()).collect()
}
}

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/completion/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub struct Opt {
step: isize,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self {
Self { step: e.args.first().and_then(|s| s.parse().ok()).unwrap_or(0) }
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self { step: e.take_first().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}

Expand Down
11 changes: 6 additions & 5 deletions yazi-core/src/completion/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ pub struct Opt {
submit: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { submit: e.named.contains_key("submit") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { submit: e.named.contains_key("submit") } }
}

impl Completion {
#[inline]
pub fn _close() {
emit!(Call(Exec::call("close", vec![]).vec(), Layer::Completion));
emit!(Call(Exec::call("close", vec![]), Layer::Completion));
}

pub fn close(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if opt.submit {
Input::_complete(self.selected(), self.ticket);

if let Some(s) = self.selected().filter(|_| opt.submit) {
Input::_complete(s, self.ticket);
}

self.caches.clear();
Expand Down
26 changes: 13 additions & 13 deletions yazi-core/src/completion/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ use crate::completion::Completion;

const LIMIT: usize = 30;

pub struct Opt<'a> {
cache: &'a Vec<String>,
cache_name: &'a str,
word: &'a str,
pub struct Opt {
cache: Vec<String>,
cache_name: String,
word: String,
ticket: usize,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self {
cache: &e.args,
cache_name: e.named.get("cache-name").map(|n| n.as_str()).unwrap_or_default(),
word: e.named.get("word").map(|w| w.as_str()).unwrap_or_default(),
ticket: e.named.get("ticket").and_then(|v| v.parse().ok()).unwrap_or(0),
cache: mem::take(&mut e.args),
cache_name: e.take_name("cache-name").unwrap_or_default(),
word: e.take_name("word").unwrap_or_default(),
ticket: e.take_name("ticket").and_then(|v| v.parse().ok()).unwrap_or(0),
}
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Completion {
prefixed.into_iter().map(ToOwned::to_owned).collect()
}

pub fn show<'a>(&mut self, opt: impl Into<Opt<'a>>) {
pub fn show(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if self.ticket != opt.ticket {
return;
Expand All @@ -63,12 +63,12 @@ impl Completion {
if !opt.cache.is_empty() {
self.caches.insert(opt.cache_name.to_owned(), opt.cache.clone());
}
let Some(cache) = self.caches.get(opt.cache_name) else {
let Some(cache) = self.caches.get(&opt.cache_name) else {
return;
};

self.ticket = opt.ticket;
self.cands = Self::match_candidates(opt.word, cache);
self.cands = Self::match_candidates(&opt.word, cache);
if self.cands.is_empty() {
return render!(mem::replace(&mut self.visible, false));
}
Expand Down
23 changes: 11 additions & 12 deletions yazi-core/src/completion/commands/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use yazi_shared::{emit, event::Exec, render, Layer};

use crate::completion::Completion;

pub struct Opt<'a> {
word: &'a str,
pub struct Opt {
word: String,
ticket: usize,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self {
word: e.args.first().map(|s| s.as_str()).unwrap_or_default(),
ticket: e.named.get("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
word: e.take_first().unwrap_or_default(),
ticket: e.take_name("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
}
}
}
Expand All @@ -23,23 +23,23 @@ impl Completion {
#[inline]
pub fn _trigger(word: &str, ticket: usize) {
emit!(Call(
Exec::call("trigger", vec![word.to_owned()]).with("ticket", ticket).vec(),
Exec::call("trigger", vec![word.to_owned()]).with("ticket", ticket),
Layer::Completion
));
}

pub fn trigger<'a>(&mut self, opt: impl Into<Opt<'a>>) {
pub fn trigger(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if opt.ticket < self.ticket {
return;
}

self.ticket = opt.ticket;
let (parent, child) = Self::split_path(opt.word);
let (parent, child) = Self::split_path(&opt.word);

if self.caches.contains_key(&parent) {
return self.show(
&Exec::call("show", vec![])
Exec::call("show", vec![])
.with("cache-name", parent)
.with("word", child)
.with("ticket", opt.ticket),
Expand Down Expand Up @@ -67,8 +67,7 @@ impl Completion {
Exec::call("show", cache)
.with("cache-name", parent)
.with("word", child)
.with("ticket", ticket)
.vec(),
.with("ticket", ticket),
Layer::Completion
));
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/completion/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Completion {
pub fn limit(&self) -> usize { self.cands.len().min(10) }

#[inline]
pub fn selected(&self) -> &String { &self.cands[self.cursor] }
pub fn selected(&self) -> Option<&String> { self.cands.get(self.cursor) }

// --- Cursor
#[inline]
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/help/commands/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub struct Opt {
step: isize,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self {
Self { step: e.args.first().and_then(|s| s.parse().ok()).unwrap_or(0) }
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self { step: e.take_first().and_then(|s| s.parse().ok()).unwrap_or(0) }
}
}
impl From<isize> for Opt {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/help/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use yazi_shared::{event::Exec, render};
use crate::help::Help;

impl Help {
pub fn escape(&mut self, _: &Exec) {
pub fn escape(&mut self, _: Exec) {
if self.in_filter.is_none() {
return self.toggle(self.layer);
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/help/commands/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use yazi_shared::{event::Exec, render};
use crate::{help::Help, input::Input};

impl Help {
pub fn filter(&mut self, _: &Exec) {
pub fn filter(&mut self, _: Exec) {
let mut input = Input::default();
input.position = Position::new(Origin::BottomLeft, Offset::line());

Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/backspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Opt {
under: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { under: e.named.contains_key("under") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { under: e.named.contains_key("under") } }
}
impl From<bool> for Opt {
fn from(under: bool) -> Self { Self { under } }
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/backward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use yazi_shared::{event::Exec, CharKind};
use crate::input::Input;

impl Input {
pub fn backward(&mut self, _: &Exec) {
pub fn backward(&mut self, _: Exec) {
let snap = self.snap();
if snap.cursor == 0 {
return self.move_(0);
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Opt {
submit: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { submit: e.named.contains_key("submit") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { submit: e.named.contains_key("submit") } }
}
impl From<bool> for Opt {
fn from(submit: bool) -> Self { Self { submit } }
Expand Down
19 changes: 8 additions & 11 deletions yazi-core/src/input/commands/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@ use yazi_shared::{emit, event::Exec, render, Layer};

use crate::input::Input;

pub struct Opt<'a> {
word: &'a str,
pub struct Opt {
word: String,
ticket: usize,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self {
word: e.args.first().map(|w| w.as_str()).unwrap_or_default(),
ticket: e.named.get("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
word: e.take_first().unwrap_or_default(),
ticket: e.take_name("ticket").and_then(|s| s.parse().ok()).unwrap_or(0),
}
}
}

impl Input {
#[inline]
pub fn _complete(word: &str, ticket: usize) {
emit!(Call(
Exec::call("complete", vec![word.to_owned()]).with("ticket", ticket).vec(),
Layer::Input
));
emit!(Call(Exec::call("complete", vec![word.to_owned()]).with("ticket", ticket), Layer::Input));
}

pub fn complete<'a>(&mut self, opt: impl Into<Opt<'a>>) {
pub fn complete(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
if self.ticket != opt.ticket {
return;
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct Opt {
insert: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self {
impl From<Exec> for Opt {
fn from(e: Exec) -> Self {
Self { cut: e.named.contains_key("cut"), insert: e.named.contains_key("insert") }
}
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{completion::Completion, input::{op::InputOp, Input, InputMode}};

pub struct Opt;

impl From<&Exec> for Opt {
fn from(_: &Exec) -> Self { Self }
impl From<Exec> for Opt {
fn from(_: Exec) -> Self { Self }
}
impl From<()> for Opt {
fn from(_: ()) -> Self { Self }
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Opt {
end_of_word: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { end_of_word: e.named.contains_key("end-of-word") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { end_of_word: e.named.contains_key("end-of-word") } }
}

impl Input {
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Opt {
append: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { append: e.named.contains_key("append") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { append: e.named.contains_key("append") } }
}
impl From<bool> for Opt {
fn from(append: bool) -> Self { Self { append } }
Expand Down
12 changes: 5 additions & 7 deletions yazi-core/src/input/commands/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use yazi_shared::{event::Exec, render, CharKind};

use crate::input::Input;

pub struct Opt<'a> {
kind: &'a str,
pub struct Opt {
kind: String,
}

impl<'a> From<&'a Exec> for Opt<'a> {
fn from(e: &'a Exec) -> Self {
Self { kind: e.args.first().map(|s| s.as_str()).unwrap_or_default() }
}
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self { Self { kind: e.take_first().unwrap_or_default() } }
}

impl Input {
Expand Down Expand Up @@ -67,7 +65,7 @@ impl Input {
input.take(n).fold(0, |acc, c| acc + c.len_utf8())
}

pub fn kill<'a>(&mut self, opt: impl Into<Opt<'a>>) {
pub fn kill(&mut self, opt: impl Into<Opt>) {
let opt = opt.into() as Opt;
let snap = self.snap_mut();

Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/input/commands/move_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ pub struct Opt {
in_operating: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self {
impl From<Exec> for Opt {
fn from(mut e: Exec) -> Self {
Self {
step: e.args.first().and_then(|s| s.parse().ok()).unwrap_or(0),
step: e.take_first().and_then(|s| s.parse().ok()).unwrap_or(0),
in_operating: e.named.contains_key("in-operating"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions yazi-core/src/input/commands/paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub struct Opt {
before: bool,
}

impl From<&Exec> for Opt {
fn from(e: &Exec) -> Self { Self { before: e.named.contains_key("before") } }
impl From<Exec> for Opt {
fn from(e: Exec) -> Self { Self { before: e.named.contains_key("before") } }
}

impl Input {
Expand Down
2 changes: 1 addition & 1 deletion yazi-core/src/input/commands/redo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use yazi_shared::{event::Exec, render};
use crate::input::Input;

impl Input {
pub fn redo(&mut self, _: &Exec) {
pub fn redo(&mut self, _: Exec) {
render!(self.snaps.redo());
}
}
Loading

0 comments on commit 56ede51

Please sign in to comment.