mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: going to next or previous occurrence is working with J and K
This commit is contained in:
parent
a29e5c9296
commit
520ff0adaf
6 changed files with 43 additions and 47 deletions
|
|
@ -29,7 +29,9 @@ impl Preview {
|
||||||
impl UserData for Preview {
|
impl UserData for Preview {
|
||||||
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
fn add_fields<F: UserDataFields<Self>>(fields: &mut F) {
|
||||||
fields.add_field_method_get("skip", |_, me| Ok(me.skip));
|
fields.add_field_method_get("skip", |_, me| Ok(me.skip));
|
||||||
|
// TODO: Make sure we need this ?
|
||||||
fields.add_field_method_get("search_idx", |_, me| Ok(me.search_idx));
|
fields.add_field_method_get("search_idx", |_, me| Ok(me.search_idx));
|
||||||
|
|
||||||
cached_field!(fields, folder, |_, me| {
|
cached_field!(fields, folder, |_, me| {
|
||||||
me.tab
|
me.tab
|
||||||
.hovered_folder()
|
.hovered_folder()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use yazi_macro::succ;
|
use yazi_macro::succ;
|
||||||
use yazi_parser::mgr::PeekForm;
|
use yazi_parser::mgr::PeekForm;
|
||||||
use yazi_shared::{data::Data, url::UrlLike};
|
use yazi_shared::data::Data;
|
||||||
|
|
||||||
use crate::{Actor, Ctx};
|
use crate::{Actor, Ctx};
|
||||||
|
|
||||||
|
|
@ -46,10 +46,8 @@ impl Actor for Peek {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tracing::debug!("peeeek boy before set stuff");
|
|
||||||
match form.search_idx {
|
match form.search_idx {
|
||||||
Some(index) => {
|
Some(index) => {
|
||||||
tracing::debug!("search index setting {}", index);
|
|
||||||
preview.search_idx = Some(index);
|
preview.search_idx = Some(index);
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
|
|
|
||||||
|
|
@ -43,10 +43,9 @@ impl Preview {
|
||||||
|
|
||||||
self.abort();
|
self.abort();
|
||||||
|
|
||||||
// let search_first_occurrence = self.get_first_search_occurrence(&file.url);
|
let job = PeekJob { previewer, file, mime, skip: self.skip, search_idx: self.search_idx };
|
||||||
let job = PeekJob { previewer, file, mime, skip: self.skip };
|
|
||||||
|
|
||||||
tracing::debug!("preview {:?}", self);
|
tracing::debug!("preview before to peek {:?}", self.search_idx);
|
||||||
|
|
||||||
self.handle = Some(tokio::spawn(async move {
|
self.handle = Some(tokio::spawn(async move {
|
||||||
let mut rx = RUNNER.peek(&job).await;
|
let mut rx = RUNNER.peek(&job).await;
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ impl From<ActionCow> for PeekForm {
|
||||||
force: a.bool("force"),
|
force: a.bool("force"),
|
||||||
only_if: a.take("only-if").ok(),
|
only_if: a.take("only-if").ok(),
|
||||||
upper_bound: a.bool("upper-bound"),
|
upper_bound: a.bool("upper-bound"),
|
||||||
search_idx: a.take("search_idx").ok(),
|
search_idx: a.take("search-idx").ok(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,35 +18,36 @@ local function get_search_occurrences(url)
|
||||||
return lines
|
return lines
|
||||||
end
|
end
|
||||||
|
|
||||||
-- local function get_next_occurrence(occurrences, current_line, direction)
|
local function get_next_occurrence_idx(search_idx, direction, occurrences_len)
|
||||||
-- local step = direction == "up" and -1 or 1
|
local index
|
||||||
-- local start = direction == "up" and #occurrences or 1
|
|
||||||
-- local stop = direction == "up" and 1 or #occurrences
|
if direction == "up" then
|
||||||
--
|
index = (search_idx or 1) - 1
|
||||||
-- for i = start, stop, step do
|
if index < 1 then
|
||||||
-- local line = occurrences[i]
|
index = occurrences_len
|
||||||
-- if go_up and line >= current_line then
|
end
|
||||||
-- return line
|
else
|
||||||
-- end
|
index = (search_idx or 1) + 1
|
||||||
-- if direction == "down" and line <= current_line then
|
if index > occurrences_len then
|
||||||
-- return line
|
index = 1
|
||||||
-- end
|
end
|
||||||
--
|
end
|
||||||
-- -- TODO: Handle return to first element or last one
|
|
||||||
-- end
|
return index
|
||||||
-- end
|
end
|
||||||
|
|
||||||
function M:peek(job)
|
function M:peek(job)
|
||||||
local search_occurrences = get_search_occurrences(job.file.url)
|
local search_occurrences = get_search_occurrences(job.file.url)
|
||||||
local search_idx = cx.active.preview.search_idx
|
local search_idx = job.search_idx
|
||||||
ya.dbg(" search index", search_idx)
|
|
||||||
|
ya.dbg("code.lua: search_occurrences:", search_occurrences)
|
||||||
|
|
||||||
local occurrence
|
local occurrence
|
||||||
if search_occurrences and search_idx then
|
if search_occurrences and search_idx then
|
||||||
occurrence = search_occurrences[search_idx]
|
occurrence = search_occurrences[search_idx]
|
||||||
end
|
end
|
||||||
|
|
||||||
ya.dgb("Peek Occurrence:", occurrence)
|
ya.dbg("code.lua: Occurrence:", occurrence)
|
||||||
|
|
||||||
-- Todo: Pass the occurrence for highlighting
|
-- Todo: Pass the occurrence for highlighting
|
||||||
local err, bound = ya.preview_code(job)
|
local err, bound = ya.preview_code(job)
|
||||||
if bound then
|
if bound then
|
||||||
|
|
@ -60,7 +61,7 @@ function M:seek(job)
|
||||||
local direction = job.units > 0 and "down" or "up"
|
local direction = job.units > 0 and "down" or "up"
|
||||||
|
|
||||||
local search_idx = cx.active.preview.search_idx
|
local search_idx = cx.active.preview.search_idx
|
||||||
ya.dbg(" search index", search_idx)
|
ya.dbg("search index before to set it in seek", search_idx)
|
||||||
|
|
||||||
local h = cx.active.current.hovered
|
local h = cx.active.current.hovered
|
||||||
if not h or h.url ~= job.file.url then
|
if not h or h.url ~= job.file.url then
|
||||||
|
|
@ -69,19 +70,13 @@ function M:seek(job)
|
||||||
|
|
||||||
local search_occurrences = get_search_occurrences(job.file.url)
|
local search_occurrences = get_search_occurrences(job.file.url)
|
||||||
if search_occurrences then
|
if search_occurrences then
|
||||||
search_idx = (search_idx or 0) + 1
|
local next_occurrence_idx = get_next_occurrence_idx(search_idx, direction, #search_occurrences)
|
||||||
-- local current_line = cx.active.preview.skip + 1
|
local occurrence = search_occurrences[next_occurrence_idx]
|
||||||
-- ya.dbg("Current line:", current_line)
|
local line = occurrence[1]
|
||||||
-- local next_occurrence = get_next_occurrence(search_occurrences, current_line, direction)
|
-- local col = occurrence[2]
|
||||||
-- for _, occurrence in ipairs(search_occurrences) do
|
|
||||||
-- local search_line = occurrence[1]
|
ya.emit("peek", { math.max(0, line - 1), only_if = job.file.url, search_idx = next_occurrence_idx })
|
||||||
-- -- local col = occurrence[2]
|
return
|
||||||
--
|
|
||||||
-- if search_line >= current_line then
|
|
||||||
-- ya.emit("peek", { math.max(0, search_line - 1), only_if = job.file.url })
|
|
||||||
-- return
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ya.emit("peek", { math.max(0, line - 1), only_if = job.file.url })
|
-- ya.emit("peek", { math.max(0, line - 1), only_if = job.file.url })
|
||||||
|
|
@ -92,7 +87,7 @@ function M:seek(job)
|
||||||
ya.emit("peek", {
|
ya.emit("peek", {
|
||||||
math.max(0, cx.active.preview.skip + step),
|
math.max(0, cx.active.preview.skip + step),
|
||||||
only_if = job.file.url,
|
only_if = job.file.url,
|
||||||
search_idx,
|
search_idx = search_idx,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,10 @@ use yazi_shared::pool::Symbol;
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct PeekJob {
|
pub struct PeekJob {
|
||||||
pub previewer: Arc<yazi_config::plugin::Previewer>,
|
pub previewer: Arc<yazi_config::plugin::Previewer>,
|
||||||
pub file: yazi_fs::File,
|
pub file: yazi_fs::File,
|
||||||
pub mime: Symbol<str>,
|
pub mime: Symbol<str>,
|
||||||
pub skip: usize,
|
pub skip: usize,
|
||||||
|
pub search_idx: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoLua for PeekJob {
|
impl IntoLua for PeekJob {
|
||||||
|
|
@ -23,6 +24,7 @@ impl IntoLua for PeekJob {
|
||||||
("file", File::new(self.file).into_lua(lua)?),
|
("file", File::new(self.file).into_lua(lua)?),
|
||||||
("mime", self.mime.into_lua(lua)?),
|
("mime", self.mime.into_lua(lua)?),
|
||||||
("skip", self.skip.into_lua(lua)?),
|
("skip", self.skip.into_lua(lua)?),
|
||||||
|
("search_idx", self.search_idx.into_lua(lua)?),
|
||||||
])?
|
])?
|
||||||
.into_lua(lua)
|
.into_lua(lua)
|
||||||
}
|
}
|
||||||
|
|
@ -31,7 +33,7 @@ impl IntoLua for PeekJob {
|
||||||
// --- Seek
|
// --- Seek
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct SeekJob {
|
pub struct SeekJob {
|
||||||
pub file: yazi_fs::File,
|
pub file: yazi_fs::File,
|
||||||
pub units: i16,
|
pub units: i16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue