|
| 1 | +local M = {} |
| 2 | + |
| 3 | +-- Default icon displayed when a connection is active. Nerd-font compatible. |
| 4 | +-- Users can override this by setting `vim.g.remote_sshfs_status_icon` before |
| 5 | +-- the plugin is loaded or by changing `M.icon` afterwards. |
| 6 | +M.icon = vim.g.remote_sshfs_status_icon or "" -- nf-mdi-server |
| 7 | + |
| 8 | +-- Return a short human-readable string that represents the current connection |
| 9 | +-- state. If no connection is active an empty string is returned so that the |
| 10 | +-- statusline stays unchanged. |
| 11 | +-- |
| 12 | +-- Examples: |
| 13 | +-- "" – when not connected |
| 14 | +-- " myserver" – when connected to host *myserver* |
| 15 | +function M.status() |
| 16 | + local ok, conn = pcall(require, "remote-sshfs.connections") |
| 17 | + if not ok or type(conn) ~= "table" then |
| 18 | + return "" |
| 19 | + end |
| 20 | + |
| 21 | + if not conn.is_connected or not conn.is_connected() then |
| 22 | + return "" |
| 23 | + end |
| 24 | + |
| 25 | + local host_tbl = conn.get_current_host and conn.get_current_host() or nil |
| 26 | + local name = "remote" |
| 27 | + if host_tbl and type(host_tbl) == "table" then |
| 28 | + -- Prefer the explicit entries we create while parsing the ssh-config. |
| 29 | + name = host_tbl.Name or host_tbl.Host or host_tbl.host or name |
| 30 | + end |
| 31 | + |
| 32 | + return string.format("%s %s", M.icon, name) |
| 33 | +end |
| 34 | + |
| 35 | +------------------------------------------------------------------------------- |
| 36 | +-- READY-MADE COMPONENTS ------------------------------------------------------- |
| 37 | +------------------------------------------------------------------------------- |
| 38 | + |
| 39 | +-- NvChad (Heirline) component factory. |
| 40 | +-- |
| 41 | +-- Usage inside `custom/chadrc.lua` (NvChad > v2.*): |
| 42 | +-- |
| 43 | +-- local remote = require("remote-sshfs.statusline").nvchad_component() |
| 44 | +-- table.insert(M.active_components, remote) -- wherever you like |
| 45 | +-- |
| 46 | +-- The returned table follows Heirline's component specification used by |
| 47 | +-- NvChad, i.e. `provider`, `condition` and `hl` keys. |
| 48 | +-- |
| 49 | +-- `opts` (all optional): |
| 50 | +-- highlight (table) – Highlight table passed as-is to Heirline. |
| 51 | +function M.nvchad_component(opts) |
| 52 | + opts = opts or {} |
| 53 | + |
| 54 | + -- Lazily require within the closures because the statusline component is |
| 55 | + -- evaluated *after* the plugin init code has run. |
| 56 | + return { |
| 57 | + condition = function() |
| 58 | + local ok, conn = pcall(require, "remote-sshfs.connections") |
| 59 | + return ok and conn.is_connected and conn.is_connected() |
| 60 | + end, |
| 61 | + provider = function() |
| 62 | + return M.status() |
| 63 | + end, |
| 64 | + hl = opts.highlight or { fg = "green" }, |
| 65 | + } |
| 66 | +end |
| 67 | + |
| 68 | +------------------------------------------------------------------------------- |
| 69 | +-- NvChad (classic v3 statusline) module helper -------------------------------- |
| 70 | +------------------------------------------------------------------------------- |
| 71 | + |
| 72 | +-- NvChad’s in-house statusline (documented under `:h nvui.statusline`) expects |
| 73 | +-- plain strings or Lua callables in the `modules` table. This helper returns |
| 74 | +-- such a callable, so users can simply do |
| 75 | +-- |
| 76 | +-- M.ui = { |
| 77 | +-- statusline = { |
| 78 | +-- modules = { |
| 79 | +-- remote = require("remote-sshfs.statusline").nvchad_module(), |
| 80 | +-- } |
| 81 | +-- } |
| 82 | +-- } |
| 83 | +-- |
| 84 | +-- `opts.highlight` – optional highlight group name, e.g. "St_gitIcons". |
| 85 | +function M.nvchad_module(opts) |
| 86 | + opts = opts or {} |
| 87 | + |
| 88 | + -- Determine highlight behaviour. |
| 89 | + -- 1) string → assume existing highlight group name |
| 90 | + -- 2) table → dynamically create a group once and use it |
| 91 | + -- 3) nil → no colour decorations |
| 92 | + local hl_begin, hl_end = "", "" |
| 93 | + |
| 94 | + if opts.highlight then |
| 95 | + local group_name |
| 96 | + |
| 97 | + if type(opts.highlight) == "string" then |
| 98 | + group_name = opts.highlight |
| 99 | + elseif type(opts.highlight) == "table" then |
| 100 | + group_name = "RemoteSSHFSStl" |
| 101 | + -- Only define once per session. |
| 102 | + if vim.fn.hlexists(group_name) == 0 then |
| 103 | + vim.api.nvim_set_hl(0, group_name, opts.highlight) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + if group_name then |
| 108 | + hl_begin = "%#" .. group_name .. "#" |
| 109 | + hl_end = "%*" |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + return function() |
| 114 | + local s = M.status() |
| 115 | + if s == "" then |
| 116 | + return "" |
| 117 | + end |
| 118 | + return hl_begin .. s .. hl_end |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | +------------------------------------------------------------------------------- |
| 123 | +-- Fall-back plain string for easy integration in classic statuslines --------- |
| 124 | +------------------------------------------------------------------------------- |
| 125 | + |
| 126 | +-- For simple `statusline` settings ("set statusline=%!v:lua..."), return a Lua |
| 127 | +-- callable that expands to the status string. Example: |
| 128 | +-- vim.o.statusline = "%!v:lua.require('remote-sshfs.statusline').status()" |
| 129 | +function M.vim_statusline() |
| 130 | + return M.status() |
| 131 | +end |
| 132 | + |
| 133 | +return M |
0 commit comments