From e5eb78a380b1779432373040e5e8912a4313f227 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 20 Feb 2025 23:09:01 +0800 Subject: [PATCH] feat: new plugin `toggle-view.yazi`, closes #71 --- toggle-view.yazi/LICENSE | 21 ++++++++++++++ toggle-view.yazi/README.md | 57 ++++++++++++++++++++++++++++++++++++++ toggle-view.yazi/main.lua | 48 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 toggle-view.yazi/LICENSE create mode 100644 toggle-view.yazi/README.md create mode 100644 toggle-view.yazi/main.lua diff --git a/toggle-view.yazi/LICENSE b/toggle-view.yazi/LICENSE new file mode 100644 index 0000000..fb5b1d6 --- /dev/null +++ b/toggle-view.yazi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 yazi-rs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/toggle-view.yazi/README.md b/toggle-view.yazi/README.md new file mode 100644 index 0000000..87cbdd2 --- /dev/null +++ b/toggle-view.yazi/README.md @@ -0,0 +1,57 @@ +# toggle-view.yazi + +Based on the user's [`ratio` configuration](https://yazi-rs.github.io/docs/configuration/yazi#manager.ratio), toggles the minimum and maximum width for each column view. + +Assume the user's `ratio` is $$[A, B, C]$$, that is, $$\text{parent}=A, \text{current}=B, \text{preview}=C$$: + +- `min-parent`: Toggles between $$0$$ and $$A$$ - the parent is either completely hidden or showed with width $$A$$. +- `max-parent`: Toggles between $$A$$ and $$\infty$$ - the parent is either showed with width $$A$$ or fills the entire screen. +- `min-current`: Toggles between $$0$$ and $$B$$ - the current is either completely hidden or showed with width $$B$$. +- `max-current`: Toggles between $$B$$ and $$\infty$$ - the current is either showed with width $$B$$ or fills the entire screen. +- `min-preview`: Toggles between $$0$$ and $$C$$ - the preview is either completely hidden or showed with width $$C$$. +- `max-preview`: Toggles between $$C$$ and $$\infty$$ - the preview is either showed with width $$C$$ or fills the entire screen. +- `reset`: Resets to the user's configured `ratio`. + +## Installation + +```sh +ya pack -a yazi-rs/plugins:full-border +``` + +## Usage + +Hide/Show preview: + +```toml +[[manager.prepend_keymap]] +on = "T" +run = "plugin toggle-view min-preview" +desc = "Show/Hide preview" +``` + +Maximize/Restore preview: + +```toml +[[manager.prepend_keymap]] +on = "T" +run = "plugin toggle-view max-preview" +desc = "Maximize/Restore preview" +``` + +You can replace `preview` with `current` or `parent` to toggle the other columns. + +## Advanced + +In addition to triggering the plugin with a keypress, you can also trigger it in your `init.lua` file: + +```lua +if os.getenv("NVIM") then + require("toggle-view"):entry("min-preview") +end +``` + +In the example above, when it detects that you're [using Yazi in nvim](https://yazi-rs.github.io/docs/resources#vim), the preview is hidden by default — you can always press `T` (or any key you've bound) to show it again. + +## License + +This plugin is MIT-licensed. For more information, check the [LICENSE](LICENSE) file. diff --git a/toggle-view.yazi/main.lua b/toggle-view.yazi/main.lua new file mode 100644 index 0000000..d97d036 --- /dev/null +++ b/toggle-view.yazi/main.lua @@ -0,0 +1,48 @@ +--- @since 25.2.7 +--- @sync entry + +local function entry(st, job) + local R = MANAGER.ratio + st.parent = st.parent and st.parent or R.parent + st.current = st.current and st.current or R.current + st.preview = st.preview and st.preview or R.preview + + local act = type(job) == "string" and job or job.args[1] + if act == "min-parent" then + st.parent = st.parent == R.parent and 0 or R.parent + elseif act == "min-current" then + st.current = st.current == R.current and 0 or R.current + elseif act == "min-preview" then + st.preview = st.preview == R.preview and 0 or R.preview + elseif act == "max-parent" then + st.parent = st.parent == 65535 and R.parent or 65535 + elseif act == "max-current" then + st.current = st.current == 65535 and R.current or 65535 + elseif act == "max-preview" then + st.preview = st.preview == 65535 and R.preview or 65535 + end + + if not st.old then + st.old = Tab.layout + Tab.layout = function(self) + local all = st.parent + st.current + st.preview + self._chunks = ui.Layout() + :direction(ui.Layout.HORIZONTAL) + :constraints({ + ui.Constraint.Ratio(st.parent, all), + ui.Constraint.Ratio(st.current, all), + ui.Constraint.Ratio(st.preview, all), + }) + :split(self._area) + end + end + + if act == "reset" then + Tab.layout, st.old = st.old, nil + st.parent, st.current, st.preview = nil, nil, nil + end + + ya.app_emit("resize", {}) +end + +return { entry = entry }