Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lua/FTerm/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
local Term = require('FTerm.terminal')
local A = vim.api

local M = {}

local t = Term:new()

-- Precreate the default terminal after Neovim starts
local precreate_augroup = A.nvim_create_augroup('FTermPrecreate', { clear = true })
A.nvim_create_autocmd('VimEnter', {
group = precreate_augroup,
callback = function()
-- Use vim.schedule to ensure we're in the main event loop
vim.schedule(function()
t:precreate()
end)
end,
once = true,
})

---Creates a custom terminal
---@param cfg Config
---@return Term
Expand Down
28 changes: 26 additions & 2 deletions lua/FTerm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ function Term:prompt()
end

---Term:term opens a terminal inside a buffer
---@private
---@param with_prompt? boolean Whether to enter insert mode after opening (default: true)
---@return Term
function Term:open_term()
function Term:open_term(with_prompt)
-- NOTE: `termopen` will fails if the current buffer is modified
self.terminal = fn.termopen(U.is_cmd(self.config.cmd), {
clear_env = self.config.clear_env,
Expand All @@ -156,7 +158,29 @@ function Term:open_term()
-- This prevents the filetype being changed to `term` instead of `FTerm` when closing the floating window
A.nvim_buf_set_option(self.buf, 'filetype', self.config.ft)

return self:prompt()
if with_prompt ~= false then
return self:prompt()
end

return self
end

---Term:precreate creates the terminal buffer and job in background without opening a window
---@return Term
function Term:precreate()
if self.buf and self.terminal then
return self
end

local buf = self:create_buf()
self.buf = buf

-- Use nvim_buf_call to avoid switching current buffer/window
A.nvim_buf_call(buf, function()
self:open_term(false)
end)

return self
end

---Term:open does all the magic of opening terminal
Expand Down