Skip to content

Commit

Permalink
Merge pull request #104 from brianhuster/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
brianhuster authored Oct 9, 2024
2 parents 6a350c9 + e02cdab commit 00e6182
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ You can customize the plugin by passing a table to the `opts` variable or the fu
```lua
{
commands = {
start = 'LivePreview', -- Command to start the live preview server and open the default browser. Default is 'LivePreview'
stop = 'StopPreview', -- Command to stop the live preview. Default is 'StopPreview'
start = 'LivePreview', -- Command to start the live preview server and open the default browser.
stop = 'StopPreview', -- Command to stop the live preview.
},
port = 5500, -- Port to run the live preview server on. Default is 5500
browser = "default", -- Browser to open the live preview in. Default is 'default', meaning the default browser of your system will be used
port = 5500, -- Port to run the live preview server on.
browser = 'default', -- Terminal command to open the browser for live-previewing (eg. 'firefox', 'flatpak run com.vivaldi.Vivaldi'). By default, it will use the default browser.
}
```

Expand Down
3 changes: 2 additions & 1 deletion README.vi.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ https://github.com/user-attachments/assets/e9a64709-8758-44d8-9e3c-9c15e0bf2a0e
## Yêu cầu

- Neovim >=0.10.0
(khuyến nghị >=0.10.2)
- Một trình duyệt web

## Cài đặt
Expand Down Expand Up @@ -68,7 +69,7 @@ Bạn có thể tùy chỉnh plugin bằng cách đưa 1 bảng vào biến `opt
stop = 'StopPreview', -- Lệnh để dừng máy chủ live-preview.
},
port = 5500, -- Cổng để chạy máy chủ live-preview
browser = "default", -- Trình duyệt để xem kết quả live-preview. Mặc định "default" sẽ mở trình duyệt mặc định của hệ điều hành
browser = 'default', -- Lệnh để mở trình duyệt (ví dụ 'firefox', 'flatpak run com.vivaldi.Vivaldi'. Giá trị 'default' là trình duyệt mặc định của hệ điều hành.
}
```

Expand Down
8 changes: 6 additions & 2 deletions doc/livepreview.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,13 @@ kill_port({port}) *livepreview.utils.kill_port()*
open_browser({path}, {browser}) *livepreview.utils.open_browser()*
Open URL in the browser

Example: ```lua open_browser("https://neovim.io/", "firefox")
open_browser("https://neovim.io/", "flatpak run com.microsoft.Edge") >
<

Parameters: ~
{path} (`string`)
{browser} (`string`)
{browser} (`string?`)

sha1({val}) *livepreview.utils.sha1()*
Compute the SHA1 hash of a string.
Expand Down Expand Up @@ -297,7 +301,7 @@ term_cmd({cmd}, {callback}) *livepreview.utils.term_cmd()*
Parameters: ~
{cmd} (`string`) terminal command to execute. Term_cmd will use
sh or pwsh depending on the OS
{callback} (`function`) function to call when the command finishes.
{callback} (`function?`) function to call when the command finishes.
• code: the exit code of the command
• signal: the signal that killed the process
• stdout: the standard output of the command
Expand Down
42 changes: 11 additions & 31 deletions lua/livepreview/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ end

--- Execute a shell commands
---@param cmd string: terminal command to execute. Term_cmd will use sh or pwsh depending on the OS
---@param callback function: function to call when the command finishes.
---@param callback function|nil: function to call when the command finishes.
--- - code: the exit code of the command
--- - signal: the signal that killed the process
--- - stdout: the standard output of the command
Expand Down Expand Up @@ -258,39 +258,19 @@ function M.sha1(val)
end

--- Open URL in the browser
---@param path string
---@param browser string
---
--- Example: ```lua
--- open_browser("https://neovim.io/", "firefox")
--- open_browser("https://neovim.io/", "flatpak run com.microsoft.Edge")
--- ```
--- @param path string
--- @param browser string|nil
function M.open_browser(path, browser)
vim.validate({
path = { path, 'string' },
})
local is_uri = path:match('%w+:')
if not is_uri then
path = vim.fn.expand(path)
end

local cmd
if browser ~= 'default' then
cmd = { browser, path }
elseif vim.fn.has('mac') == 1 then
cmd = { 'open', path }
elseif vim.fn.has('win32') == 1 then
if vim.fn.executable('rundll32') == 1 then
cmd = { 'rundll32', 'url.dll,FileProtocolHandler', path }
elseif vim.fn.executable("start") == 1 then
cmd = { 'start', path }
else
return nil, 'vim.ui.open: rundll32 and start not found'
end
elseif vim.fn.executable('wslview') == 1 then
cmd = { 'wslview', path }
elseif vim.fn.executable('xdg-open') == 1 then
cmd = { 'xdg-open', path }
if browser == "default" or #browser == 0 or browser == nil then
vim.ui.open(path)
else
return nil, 'no handler found (tried: wslview, xdg-open)'
M.term_cmd(browser .. " " .. path)
end

vim.system(cmd, { text = true, detach = true })
end

--- Kill a process which is not Neovim running on a port
Expand Down

0 comments on commit 00e6182

Please sign in to comment.