Skip to content

Commit

Permalink
feature: add jump_to_history_directory action
Browse files Browse the repository at this point in the history
  • Loading branch information
amedama41 committed Jul 23, 2023
1 parent 5c4ba7b commit 436bbf6
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/vfiler.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,9 @@ Jump to root directory.
#### jump_to_directory
Jump to specified directory.

#### jump_to_history_directory
Jump to specified history directory.

---

### Action to select
Expand Down
17 changes: 17 additions & 0 deletions lua/vfiler/actions/directory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ function M.jump_to_directory(vfiler, context, view)
api.cd(vfiler, context, view, dirpath)
end

function M.jump_to_history_directory(vfiler, context, view)
local history = context:directory_history()
if #history == 0 then
return
end

local Menu = require('vfiler/extensions/menu')
local menu = Menu.new(vfiler, 'Select History Directory', {
initial_items = history,

on_selected = function(filer, ctx, v, path)
api.cd(filer, ctx, v, path)
end,
})
api.start_extension(vfiler, context, view, menu)
end

function M.jump_to_home(vfiler, context, view)
local dirpath = vim.fn.expand('~')
api.cd(vfiler, context, view, dirpath)
Expand Down
80 changes: 80 additions & 0 deletions lua/vfiler/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,66 @@ function ItemAttribute.new(name)
}, ItemAttribute)
end

------------------------------------------------------------------------------
-- History class
------------------------------------------------------------------------------
local History = {}
History.__index = History

function History.new(max_size)
return setmetatable({
_current_index = 1,
_max_size = max_size,
_history = {},
}, History)
end

function History:copy()
local new = History.new(self._max_size)
new._current_index = self._current_index
new._history = core.table.copy(self._history)
return new
end

--- Save the path in the directory history
---@param path string
function History:save(path)
local last_item = self:_last_item()
if path == last_item then
-- This case happnes when reload action
return
end
self._history[self._current_index] = path
self._current_index = self._current_index + 1
if self._current_index > self._max_size then
self._current_index = 1
end
end

--- Get the directory history
function History:items()
local history = {}
for i = 1, #self._history do
local index = self._current_index - i
if index <= 0 then
index = index + #self._history
end
table.insert(history, self._history[index])
end
return history
end

function History:_last_item()
if #self._history == 0 then
return nil
end
local index = self._current_index - 1
if index == 0 then
index = #self._history
end
return self._history[index]
end

------------------------------------------------------------------------------
-- Session class
------------------------------------------------------------------------------
Expand All @@ -54,6 +114,7 @@ Session.__index = Session

local shared_attributes = {}
local shared_drives = {}
local shared_history = History.new(100)

function Session.new(type)
local attributes
Expand All @@ -70,10 +131,18 @@ function Session.new(type)
drives = {}
end

local history
if type == 'share' then
history = shared_history
else
history = History.new(100)
end

return setmetatable({
_type = type,
_attributes = attributes,
_drives = drives,
_history = history,
}, Session)
end

Expand All @@ -97,6 +166,7 @@ function Session:copy()
local new = Session.new(self._type)
if new._type ~= 'share' then
new._drives = core.table.copy(self._drives)
new._history = self._history:copy()
end

if new._type == 'buffer' then
Expand All @@ -122,6 +192,7 @@ function Session:save(root, path)
end

function Session:load(root)
self._history:save(root.path)
if not self._attributes then
return nil
end
Expand All @@ -141,6 +212,10 @@ function Session:get_path_in_drive(drive)
return dirpath
end

function Session:directory_history()
return self._history:items()
end

------------------------------------------------------------------------------
-- Context class
------------------------------------------------------------------------------
Expand Down Expand Up @@ -307,6 +382,11 @@ function Context:update(context)
self._git_enabled = self:_check_git_enabled()
end

--- Get directory history
function Context:directory_history()
return self._session:directory_history()
end

function Context:_check_git_enabled()
if not self.options.git.enabled or vim.fn.executable('git') ~= 1 then
return false
Expand Down

0 comments on commit 436bbf6

Please sign in to comment.