sxyazi / yazi

💥 Blazing fast terminal file manager written in Rust, based on async I/O.
https://yazi-rs.github.io
MIT License
11.19k stars 261 forks source link

Word wrapping in preview #1089

Open mskvsk opened 1 month ago

mskvsk commented 1 month ago

Please describe the problem you're trying to solve

Word wrapping in preview has been a pretty contentious issue in the discussions of at least two terminal file managers. Some people like to scroll it, some people prefer it to word wrap.

Personally, I would like some of my documents to word wrap, while the others and code to not to.

I suspect, many people have their own expectations around that which I will try to address in the solution.

Would you be willing to contribute this feature?

Describe the solution you'd like

An optional "wrap" field in the previewers array in yazi.toml

True for word wrapping, false for long lines. The default is false. Ignored if the active preview is not wrappable.

Example:

previewers = [
    { name = "*/", run = "folder", sync = true },
    # Code
    { mime = "text/*", run = "code" },
    { mime = "*/{xml,javascript,x-wine-extension-ini}", run = "code" },
    # JSON
    { mime = "application/json", run = "json", wrap = true},
]

Additional context

No response

mskvsk commented 3 weeks ago

Also, I have never written any Rust but can give it a shot if @sxyazi deems this feature good to have.

We can also downscale the scope of this feature to a single boolean (wordwrap = true/false) in the config if it seems an overkill.

All I need is my text files to word wrap as I use text preview on them daily.

sxyazi commented 3 weeks ago

Nice to have!

Would you like to raise a PR for it? Here is the method that handles code highlighting: https://github.com/sxyazi/yazi/blob/9df256f7f4abc312ae94b59c948502310ecb2c87/yazi-plugin/src/external/highlighter.rs#L59

Maybe we can change its function signature to:

pub async fn highlight(&self, skip: usize, area: Rect, wrap: bool) -> Result<Text<'static>, PeekError> {

And handle text wrapping when wrap = true.

mskvsk commented 3 weeks ago

Sure, will do. I'm half-way through the solution already.

sxyazi commented 2 weeks ago

If anyone is interested in implementing it, https://github.com/sxyazi/yazi/pull/1142 would be a great starting point.

mskvsk commented 1 week ago

I might also add that I am ready to pay a small bounty of $50 in crypto (USDC/USDT) to anyone who solves this issue without using an external crate (I don't mind if you copypaste the solution from the crate I have used in my PR).

This feature is quite nice to have to me, as I use Yazi to browse a lot of personal content that is stored as separate text files which don't look good without wrapping.

ArtyomArtamonov commented 1 week ago

Hello @mskvsk, I tried to solve this problem in #1159. Your previous commits helped me a bit, so thanks for that! You can try and build my solution, check it out on your use cases, maybe you will find some bugs. Feel free to comment and review my code!

ArtyomArtamonov commented 1 week ago

I found an issue with files that are handled by plugins, like json, toml etc. They use preview_widgets, not preview_code function. But file.lua plugin has word wrapping functionality in it, handled by Lua. Should Rust application handle the word wrapping or this is responsibility of plugins?

mskvsk commented 1 week ago

Thank you, I tried it out and have left some comments!

sxyazi commented 1 week ago

Thanks for the PR @ArtyomArtamonov! I'll review it ASAP