yazi-rs / yazi-rs.github.io

Documentation site for Yazi.
https://yazi-rs.github.io
MIT License
25 stars 87 forks source link

Add 'file name squeeze' in Tips #89

Closed AnirudhG07 closed 4 months ago

AnirudhG07 commented 4 months ago

Hi, I made some changes in file.lua so that long names have extension visible. This is my code.

function File:highlights(file)
    local max_length = 15 -- Maximum allowed length, ## TO BE FIXED WITH APPROPRIATE LENGTH
    local name = file.name:gsub("\r", "?", 1)

    if #name > max_length then
        local ext_pos = name:match("^.*()%.") -- Find the position of the last dot
        if ext_pos then
            local ext = name:sub(ext_pos) -- Extract the extension
            local name_part = name:sub(1, max_length - #ext - 1) -- Extract the initial part of the name
            name = name_part .. "~" .. ext -- Construct the shortened name
        else
            name = name:sub(1, max_length - 1) .. "~" -- If no extension, just shorten the name
        end
    end

    local highlights = file:highlights()
    if not highlights or #highlights == 0 then
        return { ui.Span(name) }
    end

    local spans, last = {}, 0
    for _, h in ipairs(highlights) do
        if h[1] > last then
            spans[#spans + 1] = ui.Span(name:sub(last + 1, h[1]))
        end
        spans[#spans + 1] = ui.Span(name:sub(h[1] + 1, h[2])):style(THEME.manager.find_keyword)
        last = h[2]
    end
    if last < #name then
        spans[#spans + 1] = ui.Span(name:sub(last + 1))
    end
    return spans
end

which will be added to the init.lua. One thing to do is to set max_length seperately for 3 windows. This is not worth a plugin, but can go in Tips area. Please review and let me know how I can make a PR for it.

It looks like this, where filename is iiiiiiiiiiiiiiiiiiiiaaaaaaaaaaaaaaaaannnnnnnnnnnnnnnnneeeeaaa.png hehe :)

image
sxyazi commented 4 months ago

Thanks for the PR, but :sub() can't handle CJK and Unicode characters, and I think it should be a built-in feature rather than something users have to manually patch. Closing in favor of https://github.com/sxyazi/yazi/issues/903

AnirudhG07 commented 4 months ago

oh ok. No problem. Either ways, if this feature is available, it would be great.

github-actions[bot] commented 3 months ago

I'm going to lock this issue because it has been closed for 30 days. ⏳ This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.