Skip to content

Commit

Permalink
perf: new image_delay option debounces image previews to avoid lag …
Browse files Browse the repository at this point in the history
…caused by terminal image decoding during fast scrolling (#1512)
  • Loading branch information
sxyazi committed Aug 18, 2024
1 parent 99a3b3a commit 0f106b5
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions yazi-config/preset/yazi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tab_size = 2
max_width = 600
max_height = 900
cache_dir = ""
image_delay = 30
image_filter = "triangle"
image_quality = 75
sixel_fraction = 15
Expand Down
4 changes: 4 additions & 0 deletions yazi-config/src/preview/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Preview {

pub cache_dir: PathBuf,

pub image_delay: u8,
pub image_filter: String,
pub image_quality: u8,
pub sixel_fraction: u8,
Expand Down Expand Up @@ -63,6 +64,8 @@ impl FromStr for Preview {

cache_dir: Option<String>,

#[validate(range(min = 0, max = 100))]
image_delay: u8,
image_filter: String,
#[validate(range(min = 50, max = 90))]
image_quality: u8,
Expand All @@ -87,6 +90,7 @@ impl FromStr for Preview {

cache_dir,

image_delay: preview.image_delay,
image_filter: preview.image_filter,
image_quality: preview.image_quality,
sixel_fraction: preview.sixel_fraction,
Expand Down
11 changes: 5 additions & 6 deletions yazi-plugin/preset/plugins/font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ local TEXT = "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n123456
local M = {}

function M:peek()
local cache = ya.file_cache(self)
if not cache then
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
return
end

if self:preload() == 1 then
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end
ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end

function M:seek() end
Expand Down
3 changes: 2 additions & 1 deletion yazi-plugin/preset/plugins/image.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local M = {}

function M:peek()
local url = ya.file_cache(self)
local start, url = os.clock(), ya.file_cache(self)
if not url or not fs.cha(url) then
url = self.file.url
end

ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(url, self.area)
ya.preview_widgets(self, {})
end
Expand Down
11 changes: 5 additions & 6 deletions yazi-plugin/preset/plugins/magick.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
local M = {}

function M:peek()
local cache = ya.file_cache(self)
if not cache then
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
return
end

if self:preload() == 1 then
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end
ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end

function M:seek() end
Expand Down
11 changes: 5 additions & 6 deletions yazi-plugin/preset/plugins/pdf.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
local M = {}

function M:peek()
local cache = ya.file_cache(self)
if not cache then
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
return
end

if self:preload() == 1 then
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end
ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end

function M:seek(units)
Expand Down
11 changes: 5 additions & 6 deletions yazi-plugin/preset/plugins/video.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
local M = {}

function M:peek()
local cache = ya.file_cache(self)
if not cache then
local start, cache = os.clock(), ya.file_cache(self)
if not cache or self:preload() ~= 1 then
return
end

if self:preload() == 1 then
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end
ya.sleep(math.max(0, PREVIEW.image_delay / 1000 + start - os.clock()))
ya.image_show(cache, self.area)
ya.preview_widgets(self, {})
end

function M:seek(units)
Expand Down

0 comments on commit 0f106b5

Please sign in to comment.