From fc4a6e86932299d9ca776ddf261db55b8e758df5 Mon Sep 17 00:00:00 2001 From: Nguyen Duc Toan Date: Sat, 30 Sep 2023 14:38:37 +0700 Subject: [PATCH] move as_bytes to inner scope --- app/src/manager/folder.rs | 24 ++++++++++++------------ core/src/manager/finder.rs | 21 +++++++++++++-------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/app/src/manager/folder.rs b/app/src/manager/folder.rs index 99fda5eb..e9a3683e 100644 --- a/app/src/manager/folder.rs +++ b/app/src/manager/folder.rs @@ -1,17 +1,23 @@ use core::files::File; use config::{MANAGER, THEME}; -use ratatui::{buffer::Buffer, layout::Rect, style::{Color, Modifier, Style}, text::{Line, Span}, widgets::{List, ListItem, Widget}}; +use ratatui::{ + buffer::Buffer, + layout::Rect, + style::{Color, Modifier, Style}, + text::{Line, Span}, + widgets::{List, ListItem, Widget}, +}; use shared::short_path; use crate::Ctx; pub(super) struct Folder<'a> { - cx: &'a Ctx, - folder: &'a core::manager::Folder, - is_preview: bool, + cx: &'a Ctx, + folder: &'a core::manager::Folder, + is_preview: bool, is_selection: bool, - is_find: bool, + is_find: bool, } impl<'a> Folder<'a> { @@ -65,13 +71,7 @@ impl<'a> Folder<'a> { let v = self.is_find.then_some(()).and_then(|_| { let finder = self.cx.manager.active().finder()?; - #[cfg(target_os = "windows")] - let (head, body, tail) = finder.explode(short.name.to_string_lossy().as_bytes())?; - #[cfg(not(target_os = "windows"))] - let (head, body, tail) = { - use std::os::unix::ffi::OsStrExt; - finder.explode(short.name.as_bytes())? - }; + let (head, body, tail) = finder.explode(short.name.to_string_lossy())?; // TODO: to be configured by THEME? let style = Style::new().fg(Color::Rgb(255, 255, 50)).add_modifier(Modifier::ITALIC); diff --git a/core/src/manager/finder.rs b/core/src/manager/finder.rs index 7d5e065f..f2f9e3a7 100644 --- a/core/src/manager/finder.rs +++ b/core/src/manager/finder.rs @@ -7,7 +7,7 @@ use shared::Url; use crate::files::Files; pub struct Finder { - query: Regex, + query: Regex, matched: BTreeMap, version: u64, } @@ -111,22 +111,27 @@ impl Finder { /// Explode the name into three parts: head, body, tail. #[inline] - pub fn explode<'a>(&self, name: &[u8]) -> Option<(String, String, String)> { - let range = self.query.find(name).map(|m| m.range())?; + pub fn explode<'a>(&self, name: Cow<'a, str>) -> Option<(String, String, String)> { + let b = name.as_bytes(); + let range = self.query.find(b).map(|m| m.range())?; Some(( - String::from_utf8_lossy(&name[..range.start]).to_string(), - String::from_utf8_lossy(&name[range.start..range.end]).to_string(), - String::from_utf8_lossy(&name[range.end..]).to_string(), + String::from_utf8_lossy(&b[..range.start]).to_string(), + String::from_utf8_lossy(&b[range.start..range.end]).to_string(), + String::from_utf8_lossy(&b[range.end..]).to_string(), )) } } impl Finder { #[inline] - pub fn matched(&self) -> &BTreeMap { &self.matched } + pub fn matched(&self) -> &BTreeMap { + &self.matched + } #[inline] - pub fn has_matched(&self) -> bool { !self.matched.is_empty() } + pub fn has_matched(&self) -> bool { + !self.matched.is_empty() + } #[inline] pub fn matched_idx(&self, url: &Url) -> Option {