Skip to content

Commit 27c802b

Browse files
authored
feat: Determine server info (#52)
Part of what's holding back Windows report support is that nvrh assumes a bash/unix like environment. Most of it isn't so bad, it expects `/tmp` to exist, but the main command to start the remote neovim instance relies on a fairly bash-compatiable shell, since it uses `$SHELL -i -c '...'` This PR adds some groundwork for determining some server info (namely the shell and temp directory), but it's a chicken an egg problem, since it relies on starting the remote neovim instance before it can begin doing that. I _think_ there's maybe some path forward where we can just run `nvim -u NONE --headless -listen :SOME_PORT` - ports because they work on both Windows and Unix - `-u NONE` because we're just going to run some lua But this expects `nvim` to exist in the users `PATH`, which is why I've been using `$SHELL -i` because ... guess who has two thumbs and `nvim` isn't in my `PATH` until fully loading an interactive shell ... Closes #49 Closes #11
1 parent 92e4e92 commit 27c802b

14 files changed

Lines changed: 452 additions & 169 deletions

File tree

script/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ WORKING_HOST="$1"
55

66
fresh-nvrh() {
77
NVRH_CLIENT_DEBUG=false \
8-
NVRH_CLIENT_NVIM_CMD="nvim,-u,NONE" \
8+
NVRH_CLIENT_NVIM_CMD="/home/linuxbrew/.linuxbrew/bin/nvim,-u,NONE" \
99
env \
1010
-u NVRH_CLIENT_SSH_ARG \
1111
-u NVRH_CLIENT_SERVER_ENV \
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
-- https://github.com/neovim/neovim/blob/c12701d4e1404a67fef6da01a8a9d7e2d48d78d6/runtime/lua/uv/_meta.lua#L4297-L4301
2+
--- @class uv.os_uname.info
3+
--- @field sysname string
4+
--- @field release string
5+
--- @field version string
6+
--- @field machine string
7+
8+
-- https://github.com/neovim/neovim/blob/c12701d4e1404a67fef6da01a8a9d7e2d48d78d6/runtime/lua/uv/_meta.lua#L4367-L4372
9+
--- @class uv.os_get_passwd.passwd
10+
--- @field username string
11+
--- @field uid integer?
12+
--- @field gid integer?
13+
--- @field shell string?
14+
--- @field homedir string
15+
16+
---@type uv.os_uname.info
17+
local uname = vim.uv.os_uname()
18+
---@type uv.os_get_passwd.passwd
19+
local passwd = vim.uv.os_get_passwd()
20+
21+
local function get_os()
22+
local os = uname.sysname:lower()
23+
24+
if os == 'linux' then
25+
return 'linux'
26+
elseif os == 'darwin' then
27+
return 'macos'
28+
elseif os == 'windows_nt' then
29+
return 'windows'
30+
elseif os:match('^cygwin') or os:match('^mingw') or os:match('^msys') then
31+
return 'windows'
32+
else
33+
return 'unknown'
34+
end
35+
end
36+
37+
local function get_arch()
38+
local arch = uname.machine:lower()
39+
40+
if arch == 'x86_64' or arch == 'amd64' then
41+
return 'amd64'
42+
elseif arch == 'aarch64' or arch == 'arm64' then
43+
return 'arm64'
44+
elseif arch:match('^armv8') then
45+
return 'arm64'
46+
elseif arch:match('^armv7') or arch:match('^armv6') then
47+
return 'arm'
48+
elseif
49+
arch == 'i386'
50+
or arch == 'i486'
51+
or arch == 'i586'
52+
or arch == 'i686'
53+
or arch == 'i786'
54+
or arch == 'x86'
55+
then
56+
return '386'
57+
elseif arch == 'ppc64le' then
58+
return 'ppc64le'
59+
elseif arch == 's390x' then
60+
return 's390x'
61+
else
62+
return 'unknown'
63+
end
64+
end
65+
66+
local function get_shell_name()
67+
local shell = (vim.env.SHELL or passwd.shell or ''):lower()
68+
if shell:match('bash') then
69+
return 'bash'
70+
elseif shell:match('zsh') then
71+
return 'zsh'
72+
end
73+
74+
if vim.env.PSMODULEPATH then
75+
return 'powershell'
76+
end
77+
78+
if vim.env.COMSPEC and vim.env.PROMPT then
79+
return 'cmd'
80+
end
81+
82+
return 'unknown'
83+
end
84+
85+
local server_info = {
86+
os = get_os(),
87+
arch = get_arch(),
88+
username = passwd.username,
89+
homedir = passwd.homedir,
90+
tmpdir = vim.uv.os_tmpdir(),
91+
shell_name = get_shell_name(),
92+
}
93+
94+
return vim.json.encode(server_info)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local session_id, channel_id, socket_path, browser_script_path, should_map_ports, nvrh_server_info =
2+
...
3+
4+
local should_initialize = _G._nvrh == nil
5+
6+
---vim.print("Preparing remote nvim", {
7+
--- session_id = session_id,
8+
--- channel_id = channel_id,
9+
--- socket_path = socket_path,
10+
--- browser_script_path = browser_script_path,
11+
--- should_map_ports = should_map_ports,
12+
--- should_initialize = should_initialize,
13+
---})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ if should_initialize then
1111
_G._nvrh = {
1212
---@type { [string]: boolean }
1313
mapped_ports = {},
14+
15+
---@type NvrhServerInfo
16+
server_info = vim.json.decode(nvrh_server_info),
1417
}
1518

1619
function _G._nvrh.get_nvrh_channels()

src/bridge_files/lua/setup_browser_script.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ if should_initialize then
44
]]
55

66
vim.fn.writefile(vim.fn.split(script_contents, '\n'), browser_script_path)
7-
os.execute('chmod +x ' .. browser_script_path)
7+
8+
if _G._nvrh.server_info.os ~= 'windows' then
9+
os.execute('chmod +x ' .. browser_script_path)
10+
end
811
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
---@meta
22

3+
---@class NvrhServerInfo
4+
---@field os string The operating system, e.g., "linux", "macos", "windows"
5+
---@field arch string The architecture, e.g., "amd64", "arm64", "386"
6+
---@field username string The username of the user
7+
---@field homedir string The home directory of the user
8+
---@field tmpdir string The temporary directory path
9+
---@field shell_name string The shell name, e.g., "bash", "zsh", "cmd", "powershell"
10+
311
--- These are bridged from the Go code
412
session_id = ''
513
channel_id = -1
614
socket_path = ''
715
browser_script_path = ''
816
should_map_ports = false
17+
nvrh_server_info = ''
918

1019
should_initialize = false

src/bridge_files/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"log/slog"
66
)
77

8-
//go:embed lua/*.lua shell/*
8+
//go:embed lua/* shell/*
99
var luaFolder embed.FS
1010

1111
func ReadFileWithoutError(filename string) string {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
set SOCKET_PATH=%s
3+
4+
start "" nvim --server "%%SOCKET_PATH%%" --remote-expr "v:lua._nvrh.open_url('%%1')" >nul 2>&1

0 commit comments

Comments
 (0)