From 0f77dba1f8d74c1c77134dd24cb8d0a74f77c018 Mon Sep 17 00:00:00 2001 From: Ren-B-7 <127970608+Ren-B-7@users.noreply.github.com> Date: Fri, 1 Aug 2025 15:28:10 +0200 Subject: [PATCH 1/3] feat(add port options): Adding port options and file options to start and pick command --- lua/livepreview/init.lua | 10 +++++++--- plugin/livepreview.lua | 30 +++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lua/livepreview/init.lua b/lua/livepreview/init.lua index 3adc3aba..9cc1348a 100644 --- a/lua/livepreview/init.lua +++ b/lua/livepreview/init.lua @@ -92,7 +92,11 @@ function M.start(filepath, port) return true end -function M.pick() +--- Create picker object and open the picker +---@param port number [Port number to open on, this is optional] +function M.pick(port) + -- Adding port fallback if the parameter is nil + port = port or config.config.port local picker = require("livepreview.picker") local pick_callback = function(pick_value) @@ -101,12 +105,12 @@ function M.pick() vim.notify("No file picked", vim.log.levels.INFO) return end - M.start(filepath, config.config.port) + M.start(filepath, port) vim.cmd.edit(filepath) utils.open_browser( string.format( "http://localhost:%d/%s", - config.config.port, + port, config.config.dynamic_root and vim.fs.basename(filepath) or filepath ), config.config.browser diff --git a/plugin/livepreview.lua b/plugin/livepreview.lua index f4eb6b8e..a46d62a8 100644 --- a/plugin/livepreview.lua +++ b/plugin/livepreview.lua @@ -35,7 +35,21 @@ api.nvim_create_user_command(cmd, function(cmd_opts) if subcommand == "start" then local filepath - if cmd_opts.fargs[2] ~= nil then + local port = Config.port + + -- determines if the options given is a port or filepath + for i = 2, 3 do + local arg = cmd_opts.fargs[i] + if arg then + if tonumber(arg) then + port = tonumber(arg) + else + filepath = arg + end + end + end + + if filepath ~= nil then filepath = cmd_opts.fargs[2] if not utils.is_absolute_path(filepath) then filepath = fs.joinpath(vim.uv.cwd(), filepath) @@ -54,7 +68,7 @@ api.nvim_create_user_command(cmd, function(cmd_opts) end end filepath = fs.normalize(filepath) - if not lp.start(filepath, Config.port) then + if not lp.start(filepath, port) then return end @@ -69,7 +83,17 @@ api.nvim_create_user_command(cmd, function(cmd_opts) lp.close() print("Live preview stopped") elseif subcommand == "pick" then - lp.pick() + -- Create port option, with the default port, then if arguments were + -- given it checks that it is a valid number and then changes the port + -- option to the given argument + local port = Config.port + local opt = cmd_opts.fargs[2] + if opt ~= nil then + if tonumber(opt) then + port = tonumber(opt) + end + end + lp.pick(port) else lp.help() end From b7418d700477dc8e530dbaac6cc2dd88d6623777 Mon Sep 17 00:00:00 2001 From: Ren-B-7 <127970608+Ren-B-7@users.noreply.github.com> Date: Mon, 11 Aug 2025 01:54:22 +0200 Subject: [PATCH 2/3] feat(Adding ++port): Adding to and options --- plugin/livepreview.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plugin/livepreview.lua b/plugin/livepreview.lua index a46d62a8..f4c15e78 100644 --- a/plugin/livepreview.lua +++ b/plugin/livepreview.lua @@ -3,6 +3,7 @@ if vim.g.loaded_livepreview then end vim.g.loaded_livepreview = true +local PORT_PREFIX = "++port=" local health = require("livepreview.health") local cmd = "LivePreview" @@ -41,8 +42,9 @@ api.nvim_create_user_command(cmd, function(cmd_opts) for i = 2, 3 do local arg = cmd_opts.fargs[i] if arg then - if tonumber(arg) then - port = tonumber(arg) + if arg:sub(1, #PORT_PREFIX) == PORT_PREFIX then + local numb = arg:sub(#PORT_PREFIX + 1) + port = tonumber(numb) and tonumber(numb) else filepath = arg end @@ -87,11 +89,10 @@ api.nvim_create_user_command(cmd, function(cmd_opts) -- given it checks that it is a valid number and then changes the port -- option to the given argument local port = Config.port - local opt = cmd_opts.fargs[2] - if opt ~= nil then - if tonumber(opt) then - port = tonumber(opt) - end + local opt = cmd_opts.fargs[2] or "" + if opt:sub(1, #PORT_PREFIX) == PORT_PREFIX then + local numb = opt:sub(#PORT_PREFIX + 1) + port = tonumber(numb) and tonumber(numb) end lp.pick(port) else From 083baf900f42c5ecdbe17ebe4c81f761ba3a3985 Mon Sep 17 00:00:00 2001 From: Ren-B-7 <127970608+Ren-B-7@users.noreply.github.com> Date: Mon, 11 Aug 2025 12:01:38 +0200 Subject: [PATCH 3/3] feat(Adding ++port): Adding errors for incorrect parameters --- plugin/livepreview.lua | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/plugin/livepreview.lua b/plugin/livepreview.lua index f4c15e78..d5a43ef1 100644 --- a/plugin/livepreview.lua +++ b/plugin/livepreview.lua @@ -43,8 +43,16 @@ api.nvim_create_user_command(cmd, function(cmd_opts) local arg = cmd_opts.fargs[i] if arg then if arg:sub(1, #PORT_PREFIX) == PORT_PREFIX then - local numb = arg:sub(#PORT_PREFIX + 1) - port = tonumber(numb) and tonumber(numb) + -- If the substring is not a number, this returns nil + local numb = tonumber(arg:sub(#PORT_PREFIX + 1)) + if numb then + port = numb + else + vim.notify( + "Error: LivePreview couldnt parse ++port option. Invalid Number.", + vim.log.levels.WARN + ) + end else filepath = arg end @@ -89,10 +97,21 @@ api.nvim_create_user_command(cmd, function(cmd_opts) -- given it checks that it is a valid number and then changes the port -- option to the given argument local port = Config.port + -- Must set opt to "" as default else the sub statement returns an error local opt = cmd_opts.fargs[2] or "" if opt:sub(1, #PORT_PREFIX) == PORT_PREFIX then - local numb = opt:sub(#PORT_PREFIX + 1) - port = tonumber(numb) and tonumber(numb) + -- If the substring is not a number, this returns nil + local numb = tonumber(opt:sub(#PORT_PREFIX + 1)) + if numb then + port = numb + else + vim.notify("Error: LivePreview couldnt parse ++port option. Invalid Number.", vim.log.levels.WARN) + end + else + vim.notify( + "Error: LivePreview couldnt parse parameter. Parameters should start be [++port=number].", + vim.log.levels.WARN + ) end lp.pick(port) else