Skip to content

Commit

Permalink
feature: toggle option.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Apr 11, 2023
1 parent 8a9ec53 commit 175275b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
18 changes: 16 additions & 2 deletions lua/vfiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ local function open(configs, dirpath)
return true
end

local function toggle(configs, dirpath)
local vfilers = VFiler.get_visible_in_tabpage(0)
if #vfilers > 0 then
for _, vfiler in ipairs(vfilers) do
vfiler:quit()
end
return true
end
return open(configs, dirpath)
end

local M = {}

--- Start vfiler from command line arguments
Expand Down Expand Up @@ -59,15 +70,18 @@ function M.start(dirpath, configs)

VFiler.cleanup()

local options = merged_configs.options

-- Correction of option values
local options = merged_configs.options
if not core.is_nvim then
if options.layout == 'floating' then
core.message.warning('Vim does not support floating windows.')
options.layout = 'none'
end
end

if options.toggle then
return toggle(merged_configs, dirpath)
end
return open(merged_configs, dirpath)
end

Expand Down
54 changes: 37 additions & 17 deletions lua/vfiler/vfiler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local core = require('vfiler/libs/core')
local status = require('vfiler/status')
local vim = require('vfiler/libs/vim')

local vfilers = {}
local vfiler_objects = {}

local VFiler = {}
VFiler.__index = VFiler
Expand Down Expand Up @@ -30,7 +30,7 @@ local function generate_bufname(name)
end

local maxnr = -1
for _, vfiler in pairs(vfilers) do
for _, vfiler in pairs(vfiler_objects) do
local object = vfiler.object
if name == object._context.options.name then
maxnr = math.max(vfiler.number, maxnr)
Expand All @@ -48,20 +48,20 @@ end
--- Cleanup vfiler buffers
function VFiler.cleanup()
local valid_filers = {}
for bufnr, vfiler in pairs(vfilers) do
for bufnr, vfiler in pairs(vfiler_objects) do
local exists = vim.fn.bufexists(bufnr) and vim.fn.bufloaded(bufnr)
if exists then
valid_filers[bufnr] = vfiler
else
vim.command('bwipeout ' .. bufnr)
end
end
vfilers = valid_filers
vfiler_objects = valid_filers
end

--- Exists vfiler buffer
function VFiler.exists(bufnr)
return vfilers[bufnr] ~= nil
return vfiler_objects[bufnr] ~= nil
end

--- Find the currently valid filer by name
Expand All @@ -78,7 +78,7 @@ end
---@param name string
function VFiler.find_hidden(name)
-- in hidden buffers
for bufnr, vfiler in pairs(vfilers) do
for bufnr, vfiler in pairs(vfiler_objects) do
local object = vfiler.object
local options = object._context.options
local infos = vim.list.from(vim.fn.getbufinfo(bufnr))
Expand All @@ -93,7 +93,7 @@ end
---@param name string
function VFiler.find_visible(name)
-- in tabpage
for bufnr, vfiler in pairs(vfilers) do
for bufnr, vfiler in pairs(vfiler_objects) do
local object = vfiler.object
assert(bufnr == object._view:bufnr())
local options = object._context.options
Expand Down Expand Up @@ -122,25 +122,45 @@ function VFiler.foreach(action, ...)
end

--- Get the filer from the buffer number
---@param bufnr number Buffer number
---@param bufnr number: Buffer number
function VFiler.get(bufnr)
if not vfilers[bufnr] then
if not vfiler_objects[bufnr] then
return nil
end
return vfilers[bufnr].object
return vfiler_objects[bufnr].object
end

--- Get the filer that is currently visible
function VFiler.get_visible()
local filers = {}
for bufnr, filer in pairs(vfilers) do
local object = filer.object
local visibilities = {}
for bufnr, vfiler in pairs(vfiler_objects) do
local object = vfiler.object
assert(bufnr == object._view:bufnr())
if object:visible() then
table.insert(filers, object)
table.insert(visibilities, object)
end
end
return visibilities
end

--- Get the filer that is currently visible in tabpage
---@param tabpage number: Tabpage number.
-- If '0' is specified, the current tabpage.
function VFiler.get_visible_in_tabpage(tabpage)
tabpage = (tabpage > 0) and tabpage or vim.fn.tabpagenr()
print('tabpage:', tabpage)
local visibilities = {}
for _, bufnr in ipairs(vim.fn.tabpagebuflist(tabpage)) do
local vfiler = vfiler_objects[bufnr]
if vfiler then
local object = vfiler.object
assert(bufnr == object._view:bufnr())
if object:visible() then
table.insert(visibilities, object)
end
end
end
return filers
return visibilities
end

--- Create a filer obuject
Expand All @@ -161,7 +181,7 @@ function VFiler.new(context)
}, VFiler)

-- add vfiler resource
vfilers[buffer.number] = {
vfiler_objects[buffer.number] = {
object = self,
number = number,
}
Expand Down Expand Up @@ -328,7 +348,7 @@ function VFiler:wipeout()
return
end
self._buffer:wipeout()
vfilers[bufnr] = nil
vfiler_objects[bufnr] = nil
end

function VFiler:_define_mappings()
Expand Down

0 comments on commit 175275b

Please sign in to comment.