feat: 🎸 create a toggle function
This commit is contained in:
parent
a9901e8e0d
commit
146c1b6211
1 changed files with 26 additions and 6 deletions
|
|
@ -23,11 +23,26 @@ local function find_cached_dir(dir)
|
||||||
return cur
|
return cur
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function is_running(dir)
|
||||||
|
local cached_dir = find_cached_dir(dir)
|
||||||
|
return cached_dir and job_cache[cached_dir]
|
||||||
|
end
|
||||||
|
|
||||||
M.config = {
|
M.config = {
|
||||||
-- 8080 default is commonly used
|
-- 8080 default is commonly used
|
||||||
args = { '--port=5555' },
|
args = { '--port=5555' },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
M.toggle = function()
|
||||||
|
local dir = vim.fn.expand '%:p:h'
|
||||||
|
local running = is_running(dir)
|
||||||
|
if not running then
|
||||||
|
M.start()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
M.stop()
|
||||||
|
end
|
||||||
|
|
||||||
M.setup = function(user_config)
|
M.setup = function(user_config)
|
||||||
M.config = vim.tbl_deep_extend('force', M.config, user_config or {})
|
M.config = vim.tbl_deep_extend('force', M.config, user_config or {})
|
||||||
|
|
||||||
|
|
@ -41,13 +56,14 @@ M.setup = function(user_config)
|
||||||
|
|
||||||
vim.api.nvim_create_user_command('LiveServerStart', M.start, {})
|
vim.api.nvim_create_user_command('LiveServerStart', M.start, {})
|
||||||
vim.api.nvim_create_user_command('LiveServerStop', M.stop, {})
|
vim.api.nvim_create_user_command('LiveServerStop', M.stop, {})
|
||||||
|
vim.api.nvim_create_user_command('LiveServerToggle', M.toggle, {})
|
||||||
end
|
end
|
||||||
|
|
||||||
M.start = function()
|
M.start = function()
|
||||||
local dir = vim.fn.expand '%:p:h'
|
local dir = vim.fn.expand '%:p:h'
|
||||||
local cached_dir = find_cached_dir(dir)
|
local running = is_running(dir)
|
||||||
|
|
||||||
if cached_dir then
|
if running then
|
||||||
log('live-server already running', 'INFO')
|
log('live-server already running', 'INFO')
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -82,11 +98,15 @@ end
|
||||||
|
|
||||||
M.stop = function()
|
M.stop = function()
|
||||||
local dir = vim.fn.expand '%:p:h'
|
local dir = vim.fn.expand '%:p:h'
|
||||||
local cached_dir = find_cached_dir(dir)
|
local running = is_running(dir)
|
||||||
|
|
||||||
if cached_dir then
|
if running then
|
||||||
vim.fn.jobstop(job_cache[cached_dir])
|
local cached_dir = find_cached_dir(dir)
|
||||||
job_cache[cached_dir] = nil
|
if cached_dir then
|
||||||
|
vim.fn.jobstop(job_cache[cached_dir])
|
||||||
|
job_cache[cached_dir] = nil
|
||||||
|
log('live-server stopped', 'INFO')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue