๐Ÿ”’ Backup for my config files
dotfiles
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add starship prompt for yazi

kacaii 3ec2aef9 7ca5011f

+393
+9
.config/yazi/init.lua
··· 1 1 require("full-border"):setup() 2 + 3 + require("starship"):setup({ 4 + hide_flags = false, 5 + flags_after_prompt = true, 6 + config_file = "~/.config/starship.toml", 7 + show_right_prompt = false, 8 + hide_count = false, 9 + count_separator = " ", 10 + })
.config/yazi/keymaps.toml

This is a binary file and will not be displayed.

+5
.config/yazi/package.toml
··· 3 3 rev = "e07bf41" 4 4 hash = "3996fc74044bc44144b323686f887e1" 5 5 6 + [[plugin.deps]] 7 + use = "Rolv-Apneseth/starship" 8 + rev = "eca1861" 9 + hash = "e519d894e94ded741e06aae4d4775981" 10 + 6 11 [[flavor.deps]] 7 12 use = "yazi-rs/flavors:catppuccin-mocha" 8 13 rev = "ca61658"
+21
.config/yazi/plugins/starship.yazi/LICENSE
··· 1 + MIT License 2 + 3 + Copyright (c) 2024 Rolv Apneseth 4 + 5 + Permission is hereby granted, free of charge, to any person obtaining a copy 6 + of this software and associated documentation files (the "Software"), to deal 7 + in the Software without restriction, including without limitation the rights 8 + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 + copies of the Software, and to permit persons to whom the Software is 10 + furnished to do so, subject to the following conditions: 11 + 12 + The above copyright notice and this permission notice shall be included in all 13 + copies or substantial portions of the Software. 14 + 15 + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 + SOFTWARE.
+116
.config/yazi/plugins/starship.yazi/README.md
··· 1 + # starship.yazi 2 + 3 + Starship prompt plugin for [Yazi](https://github.com/sxyazi/yazi) 4 + 5 + <https://github.com/Rolv-Apneseth/starship.yazi/assets/69486699/f7314687-5cb1-4d66-8d9d-cca960ba6716> 6 + 7 + ## Requirements 8 + 9 + - [Yazi](https://github.com/sxyazi/yazi) (v25.4.8+) 10 + - [starship](https://github.com/starship/starship) 11 + 12 + ## Installation 13 + 14 + ```bash 15 + ya pkg add Rolv-Apneseth/starship 16 + ``` 17 + 18 + ### Manual 19 + 20 + ```sh 21 + # Linux / MacOS 22 + git clone https://github.com/Rolv-Apneseth/starship.yazi.git ~/.config/yazi/plugins/starship.yazi 23 + # Windows 24 + git clone https://github.com/Rolv-Apneseth/starship.yazi.git %AppData%\yazi\config\plugins\starship.yazi 25 + ``` 26 + 27 + ## Usage 28 + 29 + Add this to `~/.config/yazi/init.lua`: 30 + 31 + ```lua 32 + require("starship"):setup() 33 + ``` 34 + 35 + Make sure you have [starship](https://github.com/starship/starship) installed and in your `PATH`. 36 + 37 + ## Config 38 + 39 + Here is an example with all available config options: 40 + 41 + ```lua 42 + require("starship"):setup({ 43 + -- Hide flags (such as filter, find and search). This can be beneficial for starship themes 44 + -- which are intended to go across the entire width of the terminal. 45 + hide_flags = false, 46 + -- Whether to place flags after the starship prompt. False means the flags will be placed before the prompt. 47 + flags_after_prompt = true, 48 + -- Custom starship configuration file to use 49 + config_file = "~/.config/starship_full.toml", -- Default: nil 50 + -- Whether to enable support for starship's right prompt (i.e. `starship prompt --right`). 51 + show_right_prompt = false, 52 + -- Whether to hide the count widget, in case you want only your right prompt to show up. Only has 53 + -- an effect when `show_right_prompt = true` 54 + hide_count = false, 55 + -- Separator to place between the right prompt and the count widget. Use `count_separator = ""` 56 + -- to have no space between the widgets. 57 + count_separator = " ", 58 + }) 59 + ``` 60 + 61 + ## Extra 62 + 63 + If you use a `starship` theme with a background colour, it might look a bit to cramped on just the one line `Yazi` gives the header by default. To fix this, you can add this to your `init.lua`: 64 + 65 + <details> 66 + <summary>Click to expand</summary> 67 + 68 + ```lua 69 + local old_build = Tab.build 70 + 71 + Tab.build = function(self, ...) 72 + local bar = function(c, x, y) 73 + if x <= 0 or x == self._area.w - 1 then 74 + return ui.Bar(ui.Bar.TOP):area(ui.Rect.default) 75 + end 76 + 77 + return ui.Bar(ui.Bar.TOP) 78 + :area(ui.Rect({ 79 + x = x, 80 + y = math.max(0, y), 81 + w = ya.clamp(0, self._area.w - x, 1), 82 + h = math.min(1, self._area.h), 83 + })) 84 + :symbol(c) 85 + end 86 + 87 + local c = self._chunks 88 + self._chunks = { 89 + c[1]:pad(ui.Pad.y(1)), 90 + c[2]:pad(ui.Pad(1, c[3].w > 0 and 0 or 1, 1, c[1].w > 0 and 0 or 1)), 91 + c[3]:pad(ui.Pad.y(1)), 92 + } 93 + 94 + local style = th.mgr.border_style 95 + self._base = ya.list_merge(self._base or {}, { 96 + ui.Bar(ui.Bar.RIGHT):area(self._chunks[1]):style(style), 97 + ui.Bar(ui.Bar.LEFT):area(self._chunks[1]):style(style), 98 + 99 + bar("โ”ฌ", c[1].right - 1, c[1].y), 100 + bar("โ”ด", c[1].right - 1, c[1].bottom - 1), 101 + bar("โ”ฌ", c[2].right, c[2].y), 102 + bar("โ”ด", c[2].right, c[2].bottom - 1), 103 + }) 104 + 105 + old_build(self, ...) 106 + end 107 + ``` 108 + 109 + </details> 110 + 111 + > [!NOTE] 112 + > This works by overriding your `Tab.build` function so make sure this is the only place you're doing that in your config. For example, this would be incompatible with the [full-border plugin](https://github.com/yazi-rs/plugins/tree/main/full-border.yazi) 113 + 114 + ## Thanks 115 + 116 + - [sxyazi](https://github.com/sxyazi) for providing the code for this plugin and the demo video [in this comment](https://github.com/sxyazi/yazi/issues/767#issuecomment-1977082834)
+242
.config/yazi/plugins/starship.yazi/main.lua
··· 1 + --- @since 25.4.8 2 + 3 + -- For development 4 + --[[ local function notify(message) ]] 5 + --[[ ya.notify({ title = "Starship", content = message, timeout = 3 }) ]] 6 + --[[ end ]] 7 + 8 + local save = ya.sync(function(st, outputs) 9 + st.output_left = outputs.left 10 + st.output_right = outputs.right 11 + 12 + -- Support for versions later than v25.5.31 (not yet a full release as of writing this comment) 13 + local render = ui.render or ya.render 14 + render() 15 + end) 16 + 17 + -- Helper function for accessing the `config_file` state variable 18 + ---@return string 19 + local get_config_file = ya.sync(function(st) 20 + return st.config_file 21 + end) 22 + 23 + --- Helper function for accessing `show_right_prompt` state variable 24 + ---@return boolean 25 + local should_show_right_prompt = ya.sync(function(st) 26 + return st.show_right_prompt 27 + end) 28 + 29 + return { 30 + ---User arguments for setup method 31 + ---@class SetupArgs 32 + ---@field config_file string Absolute path to a starship config file. 33 + ---@field hide_flags boolean Whether to hide all flags (such as filter and search). Default value is false. 34 + ---@field flags_after_prompt boolean Whether to place flags (such as filter and search) after the starship prompt. Default value is true. 35 + ---@field show_right_prompt boolean Whether to enable starship right prompt support. Default value is false. 36 + ---@field hide_count boolean Whether to hide the count widget. Only has an effect when show_right_prompt is true. Default value is false. 37 + ---@field count_separator string Set a custom separator between the count widget and the right prompt. Default value is " ", set to "" for no space. 38 + 39 + --- Setup plugin 40 + --- @param st table State 41 + --- @param args SetupArgs|nil 42 + setup = function(st, args) 43 + local hide_flags = false 44 + local flags_after_prompt = true 45 + local hide_count = false 46 + local count_separator = " " 47 + 48 + -- Check setup args 49 + if args ~= nil then 50 + if args.config_file ~= nil then 51 + local url = Url(args.config_file) 52 + if url.is_regular then 53 + local config_file = args.config_file 54 + 55 + -- Manually replace '~' and '$HOME' at the start of the path with the OS environment variable 56 + local home = os.getenv("HOME") 57 + if home then 58 + home = tostring(home) 59 + config_file = config_file:gsub("^~", home):gsub("^$HOME", home) 60 + end 61 + 62 + st.config_file = config_file 63 + end 64 + end 65 + 66 + if args.show_right_prompt ~= nil then 67 + -- Save directly to the plugin state so it can be accessed 68 + -- read from the entry function 69 + st.show_right_prompt = args.show_right_prompt 70 + end 71 + 72 + if args.hide_count ~= nil then 73 + hide_count = args.hide_count 74 + end 75 + 76 + if args.count_separator ~= nil then 77 + count_separator = args.count_separator 78 + end 79 + 80 + if args.hide_flags ~= nil then 81 + hide_flags = args.hide_flags 82 + end 83 + 84 + if args.flags_after_prompt ~= nil then 85 + flags_after_prompt = args.flags_after_prompt 86 + end 87 + end 88 + 89 + -- Replace default left header widget 90 + Header:children_remove(1, Header.LEFT) 91 + Header:children_add(function(self) 92 + if hide_flags or not st.output_left then 93 + return ui.Line.parse(st.output_left or "") 94 + end 95 + 96 + -- Split `st.output` at the first line break (or keep as is if none was found) 97 + local output = st.output_left:match("([^\n]*)\n?") or st.output_left 98 + 99 + local flags = self:flags() 100 + if flags_after_prompt then 101 + output = output .. " " .. flags 102 + else 103 + output = flags .. " " .. output 104 + end 105 + 106 + local line = ui.Line.parse(output) 107 + if line:width() > self._area.w then 108 + return "" 109 + end 110 + 111 + return line 112 + end, 1000, Header.LEFT) 113 + 114 + -- Support for right prompt, if enabled 115 + if st.show_right_prompt then 116 + -- Remove the default count widget 117 + Header:children_remove(1, Header.RIGHT) 118 + -- Replace with a custom widget combining the right prompt and the count widget 119 + Header:children_add(function(self) 120 + if not st.output_right then 121 + st.output_right = "" 122 + end 123 + local output = st.output_right:match("([^\n]*)\n?") or st.output_right 124 + local line = ui.Line.parse(output) 125 + 126 + -- Custom count widget so that we can measure the combined width 127 + if not hide_count then 128 + local yanked = #cx.yanked 129 + local count, style 130 + if yanked == 0 then 131 + count = #self._tab.selected 132 + style = th.mgr.count_selected 133 + elseif cx.yanked.is_cut then 134 + count = yanked 135 + style = th.mgr.count_cut 136 + else 137 + count = yanked 138 + style = th.mgr.count_copied 139 + end 140 + 141 + -- Append custom count widget 142 + if count ~= 0 then 143 + line = ui.Line({ 144 + line, 145 + ui.Span(count_separator), 146 + ui.Span(string.format(" %d ", count)):style(style), 147 + }) 148 + end 149 + end 150 + 151 + -- Give precedence to the left header widget(s), hiding this component entirely if 152 + -- there is no room for both 153 + local right_width = line:width() 154 + if self._left_width then 155 + local max = self._area.w - self._left_width 156 + if max < right_width then 157 + return "" 158 + end 159 + end 160 + 161 + return line 162 + end, 1000, Header.RIGHT) 163 + 164 + -- Override the header's redraw method, since we want the left side of the header to have 165 + -- precedence over the right, unlike the default behaviour of hiding the left side if 166 + -- there isn't room for both. 167 + function Header:redraw() 168 + local left = self:children_redraw(self.LEFT) 169 + self._left_width = left:width() 170 + 171 + local right = self:children_redraw(self.RIGHT) 172 + 173 + return { 174 + ui.Line(left):area(self._area), 175 + ui.Line(right):area(self._area):align(ui.Align.RIGHT), 176 + } 177 + end 178 + end 179 + 180 + -- Pass current working directory and custom config path (if specified) to the plugin's entry point 181 + ---Callback for subscribers to update the prompt 182 + local callback = function() 183 + local cwd = cx.active.current.cwd 184 + if st.cwd ~= cwd then 185 + st.cwd = cwd 186 + 187 + -- `ya.emit` as of 25.5.28 188 + local emit = ya.emit or ya.manager_emit 189 + 190 + emit("plugin", { 191 + st._id, 192 + ya.quote(tostring(cwd), true), 193 + }) 194 + end 195 + end 196 + 197 + -- Subscribe to events 198 + ps.sub("cd", callback) 199 + ps.sub("tab", callback) 200 + end, 201 + 202 + entry = function(_, job) 203 + local args = job.args 204 + 205 + -- Setup commands for left and right prompts 206 + local function base_command() 207 + return Command("starship") 208 + :arg("prompt") 209 + :stdin(Command.INHERIT) 210 + :cwd(args[1]) 211 + :env("STARSHIP_SHELL", "") 212 + :env("PWD", args[1]) 213 + end 214 + local command_left = base_command() 215 + local command_right = base_command():arg("--right") 216 + 217 + -- Point to custom starship config for both commands 218 + local config_file = get_config_file() 219 + if config_file then 220 + command_left = command_left:env("STARSHIP_CONFIG", config_file) 221 + command_right = command_right:env("STARSHIP_CONFIG", config_file) 222 + end 223 + 224 + -- Execute left prompt command and save output 225 + local outputs = { left = "", right = "" } 226 + local output_left = command_left:output() 227 + if output_left then 228 + outputs.left = output_left.stdout:gsub("^%s+", "") 229 + end 230 + 231 + -- If support for right prompt is enabled, execute right prompt command and save output 232 + local show_right_prompt = should_show_right_prompt() 233 + if show_right_prompt then 234 + local output_right = command_right:output() 235 + if output_right then 236 + outputs.right = output_right.stdout:gsub("^%s+", "") 237 + end 238 + end 239 + 240 + save(outputs) 241 + end, 242 + }