Skip to content

Commit

Permalink
configure find_files and live_grep to use current host
Browse files Browse the repository at this point in the history
  • Loading branch information
nosduco committed Apr 4, 2023
1 parent c2393b4 commit 09915ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
13 changes: 11 additions & 2 deletions lua/remote-sshfs/connections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ local config = {}
local hosts = {}
local ssh_configs = {}
local sshfs_args = {}

-- Current connection
local sshfs_job_id = nil
local mount_point = nil
local current_host = nil

local M = {}

Expand All @@ -20,7 +23,7 @@ M.setup = function(opts)
end

M.is_connected = function()
if sshfs_job_id and mount_point then
if sshfs_job_id and mount_point and current_host then
return true
end
return false
Expand All @@ -34,6 +37,10 @@ M.list_ssh_configs = function()
return ssh_configs
end

M.get_current_host = function()
return current_host
end

M.get_current_mount_point = function()
return mount_point
end
Expand Down Expand Up @@ -125,6 +132,7 @@ M.mount_host = function(host, mount_dir, ask_pass)
vim.notify("Connecting to host (" .. remote_host .. ")...")
local skip_clean = false
mount_point = mount_dir .. "/"
current_host = host
sshfs_job_id = vim.fn.jobstart(sshfs_cmd_local, {
cwd = mount_dir,
on_stdout = function(_, data)
Expand All @@ -146,6 +154,8 @@ M.mount_host = function(host, mount_dir, ask_pass)
on_exit = function(_, _, data)
handler.on_exit_handler(data, mount_dir, skip_clean, function()
sshfs_job_id = nil
mount_point = nil
current_host = nil
end)
end,
})
Expand All @@ -158,7 +168,6 @@ M.unmount_host = function()
-- Kill the SSHFS process
vim.fn.jobstop(sshfs_job_id)
end
mount_point = nil
end

return M
18 changes: 10 additions & 8 deletions lua/telescope/_extensions/remote-sshfs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ local function find_files(opts)
end

-- Check that a connection exists
if not connections.get_current_mount_point() then
if not connections.is_connected() then
vim.notify "You are not currently connected to a remote host."
return
end
Expand All @@ -150,8 +150,9 @@ local function find_files(opts)
end

-- Setup
local find_command = { "ssh", "tux", "-C", "fdfind", "--type", "f", "--color", "never" }
local mount_point = opts.mount_point or connections.get_current_mount_point()
local current_host = connections.get_current_host()
local find_command = { "ssh", current_host["Name"], "-C", "fdfind", "--type", "f", "--color", "never" }

-- Core find_files functionality
local command = find_command[1]
Expand Down Expand Up @@ -224,10 +225,10 @@ local function find_files(opts)
local cwd = opts.cwd or vim.loop.cwd()
pickers
.new(opts, {
prompt_title = "Find Files",
prompt_title = "Remote Find Files",
finder = finders.new_oneshot_job(find_command, opts),
previewer = previewers.new_buffer_previewer {
title = "File Preview",
title = "Remote File Preview",
dyn_title = function(_, entry)
return Path:new(from_entry.path(entry, false, false)):normalize(cwd)
end,
Expand Down Expand Up @@ -295,7 +296,7 @@ local function live_grep(opts)
end

-- Check that a connection exists
if not connections.get_current_mount_point() then
if not connections.is_connected() then
vim.notify "You are not currently connected to a remote host."
return
end
Expand All @@ -322,10 +323,11 @@ local function live_grep(opts)
end

-- Setup
local current_host = connections.get_current_host()
local mount_point = opts.mount_point or connections.get_current_mount_point()
local vimgrep_arguments = {
"ssh",
"tux",
current_host["Name"],
"-C",
"rg",
"--color=never",
Expand Down Expand Up @@ -393,11 +395,11 @@ local function live_grep(opts)

pickers
.new(opts, {
prompt_title = "Live Grep",
prompt_title = "Remote Live Grep",
finder = live_grepper,
-- previewer = conf.grep_previewer(opts),
previewer = previewers.new_buffer_previewer {
title = "Grep Preview",
title = "Remote Grep Preview",
dyn_title = function(_, entry)
return Path:new(from_entry.path(entry, false, false)):normalize(opts.cwd)
end,
Expand Down

0 comments on commit 09915ed

Please sign in to comment.