Skip to content
Open
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
20 changes: 12 additions & 8 deletions lua/nvterm/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ local function get_last(list)
if list then
return not vim.tbl_isempty(list) and list[#list] or nil
end
return terminals[#terminals] or nil
terminals = util.verify_terminals(terminals)
return terminals.list[#terminals.list] or nil
end

local function get_type(type, list)
list = list or terminals.list
if not list then
terminals = util.verify_terminals(terminals)
list = terminals.list
end
return vim.tbl_filter(function(t)
return t.type == type
end, list)
end

local function get_still_open()
terminals = util.verify_terminals(terminals)
if not terminals.list then
return {}
end
Expand All @@ -35,22 +40,22 @@ local function get_type_last(type)
end

local function get_term(key, value)
terminals = util.verify_terminals(terminals)
-- assumed to be unique, will only return 1 term regardless
return vim.tbl_filter(function(t)
return t[key] == value
end, terminals.list)[1]
end

local create_term_window = function(type)
local existing = terminals.list and #get_type(type, get_still_open()) > 0
local existing = #get_type(type, get_still_open()) > 0
util.execute_type_cmd(type, terminals, existing)
vim.wo.relativenumber = false
vim.wo.number = false
return a.nvim_get_current_win()
end

local ensure_and_send = function(cmd, type)
terminals = util.verify_terminals(terminals)
local function select_term()
if not type then
return get_last_still_open() or nvterm.new "horizontal"
Expand Down Expand Up @@ -107,8 +112,7 @@ nvterm.hide = function(type)
end

nvterm.show = function(type)
terminals = util.verify_terminals(terminals)
local term = type and get_type_last(type) or terminals.last
local term = type and get_type_last(type) or get_last()
nvterm.show_term(term)
end

Expand All @@ -120,6 +124,7 @@ nvterm.new = function(type)
a.nvim_win_set_buf(win, buf)

local job_id = vim.fn.termopen(terminals.shell or vim.o.shell)
terminals = util.verify_terminals(terminals)
local id = #terminals.list + 1
local term = { id = id, win = win, buf = buf, open = true, type = type, job_id = job_id }
terminals.list[id] = term
Expand All @@ -128,7 +133,6 @@ nvterm.new = function(type)
end

nvterm.toggle = function(type)
terminals = util.verify_terminals(terminals)
local term = get_type_last(type)

if not term then
Expand All @@ -152,7 +156,6 @@ nvterm.toggle_all_terms = function()
end
end


nvterm.close_all_terms = function()
for _, buf in ipairs(nvterm.list_active_terms "buf") do
vim.cmd("bd! " .. tostring(buf))
Expand All @@ -170,6 +173,7 @@ nvterm.list_active_terms = function(property)
end

nvterm.list_terms = function()
terminals = util.verify_terminals(terminals)
return terminals.list
end

Expand Down