Skip to content

Commit 1307b36

Browse files
authored
feat: nvchad statusline integration (#45)
1 parent 263371d commit 1307b36

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,72 @@ With this plugin you can:
174174

175175
To learn more about SSH configs and how to write/style one you can read more [here](https://linuxize.com/post/using-the-ssh-config-file/)
176176

177+
## 🧩 Status-line integrations
178+
179+
`remote-sshfs.nvim` ships a tiny helper module that exposes the current
180+
connection (if any) as a **single, reusable component** – so every status-line
181+
framework can opt-in without additional boiler-plate.
182+
183+
The module returns an **empty string** when no host is mounted which makes it
184+
safe to drop into existing layouts.
185+
186+
<details>
187+
<summary><b>NvChad (built-in statusline)</b></summary>
188+
189+
NvChad exposes its UI configuration through the return table of
190+
`lua/chadrc.lua`. The snippet below shows a minimal way to **add one custom
191+
module** (named `remote`) without touching the rest of the default layout.
192+
193+
```lua
194+
-- ~/.config/nvim/lua/chadrc.lua
195+
196+
local M = {}
197+
198+
-- 1️⃣ Create a callable module for NvChad’s statusline
199+
local remote_module = require("remote-sshfs.statusline").nvchad_module {
200+
highlight = "St_gitIcons", -- highlight group (optional)
201+
}
202+
203+
-- Option A: use an *existing* highlight group by name (as above).
204+
-- Option B: provide a colour table and the plugin will create a group for you:
205+
-- local remote_module = require("remote-sshfs.statusline").nvchad_module {
206+
-- highlight = { fg = "#6A9955", bold = true },
207+
-- }
208+
209+
-- 2️⃣ Add it to `modules` *and* reference it in `order`
210+
M.ui = {
211+
statusline = {
212+
-- theme / separator_style as you already have…
213+
214+
-- insert the module name wherever you like
215+
order = { "mode", "file", "git", "%=", "lsp_msg", "%=", "diagnostics", "remote", "lsp", "cwd", "cursor" },
216+
217+
modules = {
218+
remote = remote_module,
219+
},
220+
},
221+
}
222+
223+
return M
224+
```
225+
226+
Custom icon:
227+
228+
```lua
229+
-- has to be set *before* `require("remote-sshfs")` is executed
230+
vim.g.remote_sshfs_status_icon = "" -- VS Code-style lock icon
231+
```
232+
233+
When `RemoteSSHFSConnect` succeeds your status-line reads e.g.
234+
235+
```
236+
󰀻 myserver
237+
```
238+
239+
and vanishes as soon as you disconnect.
240+
241+
</details>
242+
177243
## 🤝 Contributing
178244

179245
If you find a bug or have a suggestion for how to improve remote-sshfs.nvim or additional functionality, please feel free to submit an issue or a pull request. We welcome contributions from the community and are committed to making remote-sshfs.nvim as useful as possible for everyone who uses it.
@@ -182,8 +248,8 @@ If you find a bug or have a suggestion for how to improve remote-sshfs.nvim or a
182248

183249
This repository provides two test suites:
184250

185-
* **Unit tests** – pure-Lua logic, run via [Busted](https://olivinelabs.com/busted/)
186-
* **Integration tests** – Neovim + [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) harness
251+
- **Unit tests** – pure-Lua logic, run via [Busted](https://olivinelabs.com/busted/)
252+
- **Integration tests** – Neovim + [plenary.nvim](https://github.com/nvim-lua/plenary.nvim) harness
187253

188254
### Unit tests
189255

lua/remote-sshfs/statusline.lua

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)