Skip to content

Commit

Permalink
feat: support custom port [#34]
Browse files Browse the repository at this point in the history
  • Loading branch information
floaterest committed Jul 18, 2024
1 parent 27aac31 commit 3cb5de8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ require 'typst-preview'.setup {
-- Setting this true will enable printing debug information with print()
debug = false,

-- The port to use for the preview server
-- Setting this to `0` will use a random port
port = 0,

-- Custom format string to open the output link provided with %s
-- Example: open_cmd = 'firefox %s -P typst-preview --class typst-preview'
open_cmd = nil,
Expand Down
5 changes: 5 additions & 0 deletions doc/typst-preview.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ Provide a custom format string to open the output link in `%s`.
Example value for open_cmd: `'firefox %s -P typst-preview --class typst-preview'`.
Type: `string`, Default: `nil`

*typst-preview.port*
Provide a custom port to open the output link.
Set to `0` to use a random port.
Type: `number`, Default: `0`

*typst-preview.invert_colors*
Can be used to invert colors in preview.
Set to `'never'` to disable.
Expand Down
1 change: 1 addition & 0 deletions lua/typst-preview/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {
opts = {
debug = false,
port = 0,
open_cmd = nil,
invert_colors = 'never',
follow_cursor = true,
Expand Down
12 changes: 8 additions & 4 deletions lua/typst-preview/servers/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ local M = {}
---Spawn the server and connect to it using the websocat process
---@param path string
---@param mode mode
---@param link static file host
---@param callback fun(close: fun(), write: fun(data: string), read: fun(on_read: fun(data: string)), link: string)
---Called after server spawn completes
local function spawn(path, mode, callback)
local function spawn(path, mode, link, callback)
local server_stdout = assert(vim.loop.new_pipe())
local server_stderr = assert(vim.loop.new_pipe())
local typst_preview_bin = config.opts.dependencies_bin['typst-preview']
Expand All @@ -27,7 +28,7 @@ local function spawn(path, mode, callback)
'--control-plane-host',
'127.0.0.1:0',
'--static-file-host',
'127.0.0.1:0',
link,
'--root',
config.opts.get_root(path),
config.opts.get_main_file(path),
Expand Down Expand Up @@ -151,9 +152,12 @@ end
---create a new Server
---@param path string
---@param mode mode
---@param link static file host
---@param callback fun(server: Server)
function M.new(path, mode, callback)
spawn(path, mode, function(close, write, read, link)
function M.new(path, mode, link, callback)
-- link might differ with the one from the callback argument
-- (e.g. in the case when port is 0)
spawn(path, mode, link, function(close, write, read, link)
---@type Server
local server = {
path = path,
Expand Down
21 changes: 20 additions & 1 deletion lua/typst-preview/servers/manager.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local factory = require 'typst-preview.servers.factory'
local utils = require 'typst-preview.utils'
local config = require 'typst-preview.config'
local M = {}

---There can not be `servers[path]` that's empty and not nil
Expand All @@ -9,6 +10,10 @@ local servers = {}
---@type { [string]: mode }
local last_modes = {}

--- Used static file links
---@type { [string]: string }
local links = {}

---Get last mode that init is called with
---@param path string
---@return mode?
Expand All @@ -32,10 +37,23 @@ function M.init(path, mode, callback)
servers[path] == nil or servers[path][mode] == nil,
'Server with path ' .. path .. ' and mode ' .. mode .. ' already exist.'
)
factory.new(path, mode, function(server)

local port = config.opts.port
local link = '127.0.0.1:' .. tostring(port)
if links[link] ~= nil then
utils.notify(
'Port ' .. port .. ' is already used by ' .. links[link],
vim.log.levels.ERROR
)
return
end

factory.new(path, mode, link, function(server)
servers[path] = servers[path] or {}
utils.debug(vim.inspect(links))
servers[path][mode] = server
last_modes[path] = mode
links[server.link] = server.path
callback(servers[path][mode])
end)
end
Expand Down Expand Up @@ -78,6 +96,7 @@ function M.remove(path)
utils.debug(
'Server with path ' .. path .. ' and mode ' .. mode .. ' closed.'
)
links[servers[path][mode].link] = nil
servers[path][mode] = nil
removed = true
end
Expand Down

0 comments on commit 3cb5de8

Please sign in to comment.