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