From 60b2a534aed19f389c7f383163ec33259a3002a3 Mon Sep 17 00:00:00 2001 From: Integral Date: Sun, 3 Nov 2024 15:33:26 +0800 Subject: [PATCH] refactor: destructure tuples to enhance readability --- yazi-adapter/src/dimension.rs | 6 +++--- yazi-adapter/src/emulator.rs | 4 ++-- yazi-core/src/manager/commands/peek.rs | 2 +- yazi-core/src/tab/tab.rs | 4 ++-- yazi-fm/src/signals.rs | 12 ++++++------ yazi-plugin/src/elements/line.rs | 26 ++++++++++++------------- yazi-plugin/src/elements/padding.rs | 4 ++-- yazi-plugin/src/elements/span.rs | 8 ++++---- yazi-plugin/src/elements/text.rs | 16 +++++++-------- yazi-plugin/src/external/highlighter.rs | 4 ++-- 10 files changed, 43 insertions(+), 43 deletions(-) diff --git a/yazi-adapter/src/dimension.rs b/yazi-adapter/src/dimension.rs index dfa0f464d..837fc8a25 100644 --- a/yazi-adapter/src/dimension.rs +++ b/yazi-adapter/src/dimension.rs @@ -12,9 +12,9 @@ impl Dimension { } if size.rows == 0 || size.columns == 0 { - if let Ok(s) = crossterm::terminal::size() { - size.columns = s.0; - size.rows = s.1; + if let Ok((cols, rows)) = crossterm::terminal::size() { + size.columns = cols; + size.rows = rows; } } diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index 932571485..cae2e53ac 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -70,8 +70,8 @@ impl Emulator { ("VSCODE_INJECTION", Self::VSCode), ("TABBY_CONFIG_DIRECTORY", Self::Tabby), ]; - match vars.into_iter().find(|v| env_exists(v.0)) { - Some(var) => return var.1, + match vars.into_iter().find(|(env, _)| env_exists(env)) { + Some((_, emulator)) => return emulator, None => warn!("[Adapter] No special environment variables detected"), } diff --git a/yazi-core/src/manager/commands/peek.rs b/yazi-core/src/manager/commands/peek.rs index 842cea0e4..b127472b6 100644 --- a/yazi-core/src/manager/commands/peek.rs +++ b/yazi-core/src/manager/commands/peek.rs @@ -59,7 +59,7 @@ impl Manager { } if hovered.is_dir() { - self.active_mut().preview.go_folder(hovered, folder.map(|f| f.1), opt.force); + self.active_mut().preview.go_folder(hovered, folder.map(|(_, cha)| cha), opt.force); } else { self.active_mut().preview.go(hovered, mime.into(), opt.force); } diff --git a/yazi-core/src/tab/tab.rs b/yazi-core/src/tab/tab.rs index 516925a80..ccdb49167 100644 --- a/yazi-core/src/tab/tab.rs +++ b/yazi-core/src/tab/tab.rs @@ -91,7 +91,7 @@ impl Tab { Box::new(self.selected.keys()) } else { let mut vec: Vec<_> = self.selected.iter().collect(); - vec.sort_unstable_by(|a, b| a.1.cmp(b.1)); + vec.sort_unstable_by(|(_, a), (_, b)| a.cmp(b)); Box::new(vec.into_iter().map(|(k, _)| k)) } } @@ -105,7 +105,7 @@ impl Tab { Box::new([&h.url].into_iter().chain(self.selected.keys())) } else { let mut vec: Vec<_> = self.selected.iter().collect(); - vec.sort_unstable_by(|a, b| a.1.cmp(b.1)); + vec.sort_unstable_by(|(_, a), (_, b)| a.cmp(b)); Box::new([&h.url].into_iter().chain(vec.into_iter().map(|(k, _)| k))) } } diff --git a/yazi-fm/src/signals.rs b/yazi-fm/src/signals.rs index aa8dd4500..ae57df2ca 100644 --- a/yazi-fm/src/signals.rs +++ b/yazi-fm/src/signals.rs @@ -92,9 +92,9 @@ impl Signals { if let Some(t) = &mut term { select! { biased; - Some(mut s) = rx.recv() => { - term = term.filter(|_| s.0); - s.1.take().map(|cb| cb.send(())); + Some((state, mut callback)) = rx.recv() => { + term = term.filter(|_| state); + callback.take().map(|cb| cb.send(())); }, Some(n) = sys.next() => if !Self::handle_sys(n) { return }, Some(Ok(e)) = t.next() => Self::handle_term(e) @@ -102,9 +102,9 @@ impl Signals { } else { select! { biased; - Some(mut s) = rx.recv() => { - term = s.0.then(EventStream::new); - s.1.take().map(|cb| cb.send(())); + Some((state, mut callback)) = rx.recv() => { + term = state.then(EventStream::new); + callback.take().map(|cb| cb.send(())); }, Some(n) = sys.next() => if !Self::handle_sys(n) { return }, } diff --git a/yazi-plugin/src/elements/line.rs b/yazi-plugin/src/elements/line.rs index 0c91c4040..0be95c949 100644 --- a/yazi-plugin/src/elements/line.rs +++ b/yazi-plugin/src/elements/line.rs @@ -54,11 +54,11 @@ impl TryFrom> for Line { Value::Table(tb) => return Self::try_from(tb), Value::String(s) => s.to_string_lossy().into_owned().into(), Value::UserData(ud) => { - if let Ok(span) = ud.take::() { - span.0.into() - } else if let Ok(mut line) = ud.take::() { - line.0.spans.iter_mut().for_each(|s| s.style = line.0.style.patch(s.style)); - line.0 + if let Ok(Span(span)) = ud.take() { + span.into() + } else if let Ok(Line(mut line)) = ud.take() { + line.spans.iter_mut().for_each(|s| s.style = line.style.patch(s.style)); + line } else { Err(EXPECTED.into_lua_err())? } @@ -77,11 +77,11 @@ impl TryFrom> for Line { match v? { Value::String(s) => spans.push(s.to_string_lossy().into_owned().into()), Value::UserData(ud) => { - if let Ok(span) = ud.take::() { - spans.push(span.0); - } else if let Ok(mut line) = ud.take::() { - line.0.spans.iter_mut().for_each(|s| s.style = line.0.style.patch(s.style)); - spans.extend(line.0.spans); + if let Ok(Span(span)) = ud.take() { + spans.push(span); + } else if let Ok(Line(mut line)) = ud.take() { + line.spans.iter_mut().for_each(|s| s.style = line.style.patch(s.style)); + spans.extend(line.spans); } else { return Err(EXPECTED.into_lua_err()); } @@ -98,7 +98,7 @@ impl UserData for Line { crate::impl_style_method!(methods, 0.style); crate::impl_style_shorthands!(methods, 0.style); - methods.add_method("width", |_, me, ()| Ok(me.0.width())); + methods.add_method("width", |_, Line(me), ()| Ok(me.width())); methods.add_function_mut("align", |_, (ud, align): (AnyUserData, u8)| { ud.borrow_mut::()?.0.alignment = Some(match align { CENTER => ratatui::layout::Alignment::Center, @@ -107,8 +107,8 @@ impl UserData for Line { }); Ok(ud) }); - methods.add_method("visible", |_, me, ()| { - Ok(me.0.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0)) + methods.add_method("visible", |_, Line(me), ()| { + Ok(me.iter().flat_map(|s| s.content.chars()).any(|c| c.width().unwrap_or(0) > 0)) }); } } diff --git a/yazi-plugin/src/elements/padding.rs b/yazi-plugin/src/elements/padding.rs index 01a4e47a3..9e1fe90ea 100644 --- a/yazi-plugin/src/elements/padding.rs +++ b/yazi-plugin/src/elements/padding.rs @@ -13,8 +13,8 @@ impl Deref for Padding { impl Padding { pub fn install(lua: &Lua, ui: &Table) -> mlua::Result<()> { - let new = lua.create_function(|_, args: (Table, u16, u16, u16, u16)| { - Ok(Self(ratatui::widgets::Padding::new(args.1, args.2, args.3, args.4))) + let new = lua.create_function(|_, (_, left, right, top, bottom): (Table, u16, u16, u16, u16)| { + Ok(Self(ratatui::widgets::Padding::new(left, right, top, bottom))) })?; let padding = lua.create_table_from([ diff --git a/yazi-plugin/src/elements/span.rs b/yazi-plugin/src/elements/span.rs index ecdf0e1a4..5e185380b 100644 --- a/yazi-plugin/src/elements/span.rs +++ b/yazi-plugin/src/elements/span.rs @@ -19,8 +19,8 @@ impl TryFrom> for Span { Ok(Self(match value { Value::String(s) => s.to_string_lossy().into_owned().into(), Value::UserData(ud) => { - if let Ok(span) = ud.take::() { - span.0 + if let Ok(Span(span)) = ud.take() { + span } else { Err(EXPECTED.into_lua_err())? } @@ -35,8 +35,8 @@ impl UserData for Span { crate::impl_style_method!(methods, 0.style); crate::impl_style_shorthands!(methods, 0.style); - methods.add_method("visible", |_, me, ()| { - Ok(me.0.content.chars().any(|c| c.width().unwrap_or(0) > 0)) + methods.add_method("visible", |_, Span(me), ()| { + Ok(me.content.chars().any(|c| c.width().unwrap_or(0) > 0)) }); } } diff --git a/yazi-plugin/src/elements/text.rs b/yazi-plugin/src/elements/text.rs index f51bd9b93..04cb12f53 100644 --- a/yazi-plugin/src/elements/text.rs +++ b/yazi-plugin/src/elements/text.rs @@ -60,10 +60,10 @@ impl TryFrom> for Text { Value::Table(tb) => return Self::try_from(tb), Value::String(s) => s.to_string_lossy().into_owned().into(), Value::UserData(ud) => { - if let Ok(line) = ud.take::() { - line.0.into() - } else if let Ok(span) = ud.take::() { - span.0.into() + if let Ok(Line(line)) = ud.take() { + line.into() + } else if let Ok(Span(span)) = ud.take() { + span.into() } else { Err(EXPECTED.into_lua_err())? } @@ -83,10 +83,10 @@ impl TryFrom> for Text { match v? { Value::String(s) => lines.push(s.to_string_lossy().into_owned().into()), Value::UserData(ud) => { - if let Ok(span) = ud.take::() { - lines.push(span.0.into()); - } else if let Ok(line) = ud.take::() { - lines.push(line.0); + if let Ok(Span(span)) = ud.take() { + lines.push(span.into()); + } else if let Ok(Line(line)) = ud.take() { + lines.push(line); } else { return Err(EXPECTED.into_lua_err()); } diff --git a/yazi-plugin/src/external/highlighter.rs b/yazi-plugin/src/external/highlighter.rs index ef21db55d..d17e0c15b 100644 --- a/yazi-plugin/src/external/highlighter.rs +++ b/yazi-plugin/src/external/highlighter.rs @@ -34,8 +34,8 @@ impl Highlighter { .unwrap() }; - let r = SYNTECT.get_or_init(|| fut).await; - (&r.0, &r.1) + let (theme, syntaxes) = SYNTECT.get_or_init(|| fut).await; + (&theme, &syntaxes) } #[inline]