Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better user experience for the filemanager plugin #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions filemanager-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ If the directory is expanded, there will be a `+` to the left of it. If it is co
| `filemanager-compressparent` | Collapse the parent dir when left is pressed on a child file | `true` |
| `filemanager-foldersfirst` | Sorts folders above any files | `true` |
| `filemanager-openonstart` | Automatically open the file tree when starting Micro | `false` |
| `filemanager-openontab` | Open the selected file in a new tab instead of a split | `false` |

### Commands and Keybindings

Expand All @@ -36,6 +37,7 @@ If you want to [keybind](https://github.com/zyedidia/micro/blob/master/runtime/h
| :------- | :------------------------- | :------------------------------------------------------------------------------------------ | :------------------------------------ |
| `tree` | - | Open/close the tree | `filemanager.toggle_tree` |
| - | <kbd>Tab</kbd> & MouseLeft | Open a file, or go into the directory. Goes back a dir if on `..` | `filemanager.try_open_at_cursor` |
| - | <kbd>Enter</kbd> | If `openontab` is true, open selected file in new tab | `filemanager.try_open_at_cursor` |
| - | <kbd>→</kbd> | Expand directory in tree listing | `filemanager.uncompress_at_cursor` |
| - | <kbd>←</kbd> | Collapse directory listing | `filemanager.compress_at_cursor` |
| - | <kbd>Shift ⬆</kbd> | Go to the target's parent directory | `filemanager.goto_parent_dir` |
Expand Down
113 changes: 106 additions & 7 deletions filemanager-plugin/filemanager.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
VERSION = "3.5.1"
VERSION = "3.5.3"

local micro = import("micro")
local config = import("micro/config")
local shell = import("micro/shell")
local buffer = import("micro/buffer")
local uutil = import("micro/util")
local os = import("os")
local filepath = import("path/filepath")
local time = import("time")

-- Clear out all stuff in Micro's messenger
local function clear_messenger()
Expand All @@ -21,6 +23,12 @@ local current_dir = os.Getwd()
local highest_visible_indent = 0
-- Holds a table of paths -- objects from new_listobj() calls
local scanlist = {}
-- Holds the selected cursor y pos inbetween closes and opens
local s_cursy = 2
-- Holds the current temporal filter
local quick_filter = ""
-- Holds the timer that's erasing the quick filter upon no activity
local quick_timer = nil

-- Get a new object used when adding to scanlist
local function new_listobj(p, d, o, i)
Expand Down Expand Up @@ -272,9 +280,9 @@ local function refresh_view()
-- Current dir
tree_view.Buf.EventHandler:Insert(buffer.Loc(0, 0), current_dir .. "\n")
-- An ASCII separator
tree_view.Buf.EventHandler:Insert(buffer.Loc(0, 1), repeat_str("─", tree_view:GetView().Width) .. "\n")
tree_view.Buf.EventHandler:Insert(buffer.Loc(0, 1), repeat_str("─", tree_view:GetView().Width) .. '\n' .. quick_filter)
-- The ".." and use a newline if there are things in the current dir
tree_view.Buf.EventHandler:Insert(buffer.Loc(0, 2), (#scanlist > 0 and "..\n" or ".."))
tree_view.Buf.EventHandler:Insert(buffer.Loc(0, 2), "📁" .. (#scanlist > 0 and "..\n" or ".."))

-- Holds the current basename of the path (purely for display)
local display_content
Expand All @@ -285,11 +293,15 @@ local function refresh_view()
if scanlist[i].dirmsg ~= "" then
-- Add the + or - to the left to signify if it's compressed or not
-- Add a forward slash to the right to signify it's a dir
display_content = scanlist[i].dirmsg .. " " .. get_basename(scanlist[i].abspath) .. "/"
if scanlist[i].dirmsg == "+" then
display_content = "📁 " .. get_basename(scanlist[i].abspath) .. "/"
else
display_content = "📂 " .. get_basename(scanlist[i].abspath) .. "/"
end
else
-- Use the basename from the full path for display
-- Two spaces to align with any directories, instead of being "off"
display_content = " " .. get_basename(scanlist[i].abspath)
-- Three runes to align with any directories, instead of being "off"
display_content = "🗎 " .. get_basename(scanlist[i].abspath)
end

if scanlist[i].owner > 0 then
Expand Down Expand Up @@ -525,8 +537,17 @@ local function try_open_at_y(y)
else
-- If it's a file, then open it
micro.InfoBar():Message("Filemanager opened ", scanlist[y].abspath)
-- Opens the absolute path in new vertical view
-- Opens the absolute path in new tab or a new split
if config.GetGlobalOption("filemanager.openontab") then
-- CurView():VSplitIndex(NewBufferFromFile(scanlist[y].abspath), 1)
local nfile = scanlist[y].abspath
local rpath = shell.RunCommand('realpath \'' .. nfile .. '\' --relative-to=\'' .. os.Getwd() .. '\'')
toggle_tree()
micro.CurPane():NewTabCmd({string.sub(rpath, 1, -2)})
toggle_tree()
else
micro.CurPane():VSplitIndex(buffer.NewBufferFromFile(scanlist[y].abspath), true)
end
-- Resizes all views after opening a file
-- tabs[curTab + 1]:Resize()
end
Expand Down Expand Up @@ -849,12 +870,19 @@ local function open_tree()
tree_view.Buf:SetOptionNative("scrollbar", false)

-- Fill the scanlist, and then print its contents to tree_view
if 0 == #scanlist then
update_current_dir(os.Getwd())
else
refresh_view()
end
tree_view.Cursor.Loc.Y = s_cursy
select_line(s_cursy)
end

-- close_tree will close the tree plugin view and release memory.
local function close_tree()
if tree_view ~= nil then
s_cursy = tree_view.Cursor.Loc.Y
tree_view:Quit()
tree_view = nil
clear_messenger()
Expand Down Expand Up @@ -1001,11 +1029,71 @@ local function clearselection_if_tree(view)
end
end

local function cancel_filtering()
quick_filter = ""
-- There is a bug in micro that doesn't redraw the screen if called in the timer callback.
-- TODO: Wait for this bug to be fixed so this works.
micro.InfoBar():ClearGutter()
quick_timer:Stop()
quick_timer = nil
end

local function find_quick_filter()
for i = 1, #scanlist do
local bn = get_basename(scanlist[i].abspath)
local qf = "^" .. quick_filter
if bn:find(qf) ~= nil then
return i
end
end
return -1
end

local function set_quick_filter(qf)
quick_filter = qf
micro.InfoBar():Message("Filtering : " .. quick_filter)

-- Try to find the first file matching the filter
local y = find_quick_filter()
if y ~= -1 then
select_line(y+2)
else
-- Don't display a cursor if not found
move_cursor_top()
end

if quick_timer ~= nil then
quick_timer:Stop()
end

quick_timer = time.AfterFunc(time.ParseDuration("2s"), cancel_filtering)
end


-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- All the events for certain Micro keys go below here
-- Other than things we flat-out fail
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function onRune(view, r)
if view ~= tree_view then
return false
end
if uutil.IsWordChar(r) or r == "." then
set_quick_filter(quick_filter .. r)
end
return false
end

function onBackspace(view)
if view ~= tree_view then
return false
end
set_quick_filter(quick_filter:sub(1, -2))
return false
end


-- Close current
function preQuit(view)
if view == tree_view then
Expand Down Expand Up @@ -1155,6 +1243,12 @@ function preInsertTab(view)
end
function preInsertNewline(view)
if view == tree_view then
if config.GetGlobalOption("filemanager.openontab") then
-- Simulate the pressing Tab so it opens with Enter key
tab_pressed = true
try_open_at_y(tree_view.Cursor.Loc.Y)
tab_pressed = false
end
return false
end
return true
Expand Down Expand Up @@ -1340,6 +1434,7 @@ function preSelectAll(view)
return false_if_tree(view)
end


function init()
-- Let the user disable showing of dotfiles like ".editorconfig" or ".DS_STORE"
config.RegisterCommonOption("filemanager", "showdotfiles", true)
Expand All @@ -1352,6 +1447,9 @@ function init()
-- Lets the user have the filetree auto-open any time Micro is opened
-- false by default, as it's a rather noticable user-facing change
config.RegisterCommonOption("filemanager", "openonstart", false)
-- Lets the user open file in new tab or in a new vsplit
-- false by default, as it's a rather noticable user-facing change
config.RegisterCommonOption("filemanager", "openontab", false)

-- Open/close the tree view
config.MakeCommand("tree", toggle_tree, config.NoComplete)
Expand Down Expand Up @@ -1384,4 +1482,5 @@ function init()
)
end
end

end
14 changes: 14 additions & 0 deletions filemanager-plugin/repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@
"Require": {
"micro": ">=2.0.0-1"
}
},
{
"Version": "3.5.2",
"Url": "https://github.com/micro-editor/updated-plugins/releases/download/v1.0.0/filemanager-3.5.2.zip",
"Require": {
"micro": ">=2.0.0-1"
}
},
{
"Version": "3.5.3",
"Url": "https://github.com/micro-editor/updated-plugins/releases/download/v1.0.0/filemanager-3.5.3.zip",
"Require": {
"micro": ">=2.0.0-1"
}
}
]
}
Expand Down